• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

adbrosaci / controller-tester / 4275909065

pending completion
4275909065

push

github

Martin Brettschneider
feat: support middlewares testing

8 of 8 new or added lines in 1 file covered. (100.0%)

69 of 79 relevant lines covered (87.34%)

0.87 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

67.74
/src/ControllerTester.php
1
<?php declare(strict_types = 1);
2

3
namespace Adbros\Tester\ControllerTester;
4

5
use Apitte\Core\Dispatcher\DispatchError;
6
use Apitte\Core\Dispatcher\IDispatcher;
7
use Apitte\Core\ErrorHandler\IErrorHandler;
8
use Apitte\Core\Http\ApiRequest;
9
use Apitte\Core\Http\ApiResponse;
10
use Contributte\Middlewares\Application\IApplication;
11
use Contributte\Psr7\Psr7Response;
12
use Contributte\Psr7\Psr7ServerRequest;
13
use Exception;
14
use Psr\Http\Message\ResponseInterface;
15
use Throwable;
16

17
class ControllerTester
18
{
19

20
        private IDispatcher $dispatcher;
21

22
        private IErrorHandler $errorHandler;
23

24
        private ?IApplication $middlewaresApplication = null;
25

26
        public function __construct(IDispatcher $dispatcher, IErrorHandler $errorHandler, ?IApplication $middlewaresApplication = null)
27
        {
28
                $this->dispatcher = $dispatcher;
1✔
29
                $this->errorHandler = $errorHandler;
1✔
30
                $this->middlewaresApplication = $middlewaresApplication;
1✔
31
        }
1✔
32

33
        public function createRequest(string $uri): TestControllerRequest
34
        {
35
                return new TestControllerRequest($uri);
1✔
36
        }
37

38
        public function execute(TestControllerRequest $testControllerRequest): TestControllerResult
39
        {
40
                $request = $this->createApiRequest($testControllerRequest);
1✔
41
                $response = new ApiResponse(new Psr7Response());
1✔
42

43
                if ($this->middlewaresApplication !== null && method_exists($this->middlewaresApplication, 'runWith')) {
1✔
44
                        ob_start();
×
45
                        $response = $this->middlewaresApplication->runWith($request);
×
46
                        ob_end_clean();
×
47

48
                        if (!$response instanceof ResponseInterface) {
×
49
                                throw new Exception(sprintf('Unsupported type of response: \'%s\'.', gettype($response)));
×
50
                        }
51

52
                        return new TestControllerResult(new ApiResponse($response));
×
53
                }
54

55
                try {
56
                        $response = $this->dispatcher->dispatch($request, $response);
1✔
57
                } catch (Throwable $exception) {
×
58
                        $response = $this->errorHandler->handle(new DispatchError($exception, $request));
×
59
                }
60

61
                return new TestControllerResult($response);
1✔
62
        }
63

64
        protected function createApiRequest(TestControllerRequest $testControllerRequest): ApiRequest
65
        {
66
                $request = (new Psr7ServerRequest(
1✔
67
                        $testControllerRequest->getMethod(),
1✔
68
                        $testControllerRequest->getUri(),
1✔
69
                        $testControllerRequest->getHeaders(),
1✔
70
                        $testControllerRequest->getRawBody(),
1✔
71
                        $testControllerRequest->getProtocolVersion(),
1✔
72
                        $testControllerRequest->getServerParams()
1✔
73
                ))->withQueryParams($testControllerRequest->getParameters());
1✔
74

75
                if (count($testControllerRequest->getFiles()) > 0) {
1✔
76
                        $request = $request->withUploadedFiles($testControllerRequest->getFiles());
×
77
                }
78

79
                if ($testControllerRequest->getParsedBody() !== null) {
1✔
80
                        $request = $request->withParsedBody($testControllerRequest->getParsedBody());
×
81
                }
82

83
                return new ApiRequest($request);
1✔
84
        }
85

86
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc