Level 56
Sep 6, 2021
2
Level 3
How to write unit test for middleware?
I am new to writing unit tests. I have a created middleware and I need to write unit tests for that using Mockery. Here is my middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
class SetupSentryContext
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
config(['optimus.heimdal.reporters.sentry.config.sentry_options.add_context' => function () {
return $this->addSentryContext();
}]);
return $next($request);
}
/**
* @return array
*/
protected function addSentryContext(): array
{
$context = [];
$context['environment'] = app()->environment();
$context = $this->getVersionContext($context);
$context = $this->getUserContext($context);
// When running in console request is not available
$context = $this->getNoneCLIContext($context);
if (isset($context['extra']['exceptions_in_exception_handler'])) {
$context['tags']['exception'] = 'true';
}
return $context;
}
/**
* @param array $context
* @return array
*/
protected function getVersionContext(array $context): array
{
try {
$context['release'] = \Tremby\LaravelGitVersion\GitVersionHelper::getVersion();
} catch (\Tremby\LaravelGitVersion\Exception\CouldNotGetVersionException $exception) {
$context['release'] = '';
} catch (\Throwable $throwable) {
$context['release'] = '';
$context['extra']['exceptions_in_exception_handler'][] = (string)$throwable;
}
return $context;
}
/**
* @param array $context
* @return array
*/
protected function getUserContext(array $context): array
{
try {
$user = \User::current();
} catch (\Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException $exception) {
$user = null;
} catch (\Throwable $throwable) {
$user = null;
$context['extra']['exceptions_in_exception_handler'][] = (string)$throwable;
}
if ($user) {
$context['user'] = [
'id' => $user->id,
'email' => $user->username,
];
} else {
$context['user'] = [];
}
return $context;
}
/**
* @param array $context
* @return array
*/
protected function getNoneCLIContext(array $context): array
{
if (substr(php_sapi_name(), 0, 3) !== 'cli') {
$request = app('request');
if (!isset($context['extra'])) {
$context['extra'] = [];
}
$context['extra']['request_data'] = json_encode($request->all());
$context = $this->getApiVersionContext($context);
$context = $this->getUserIP($context);
}
return $context;
}
/**
* @param array $context
* @return array
*/
protected function getApiVersionContext(array $context): array
{
try {
$context['extra']['api_version'] = app()->get("ApiVersion")->version();
} catch (\Throwable $throwable) {
$context['extra']['api_version'] = "unknown";
}
return $context;
}
/**
* @param array $context
* @return array
*/
protected function getUserIP(array $context): array
{
try {
$context['user']['ip_address'] = \Request::getClientIp();
} catch (\Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException $exception) {
$context['user']['ip_address'] = '0.0.0.0';
} catch (\Throwable $throwable) {
$context['user']['ip_address'] = '0.0.0.0';
$context['extra']['exceptions_in_exception_handler'][] = (string)$throwable;
}
return $context;
}
}
Please or to participate in this conversation.