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

adbrosaci / controller-tester / 4164242036

pending completion
4164242036

push

github

Martin Brettschneider
feat: support middlewares testing

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

69 of 82 relevant lines covered (84.15%)

0.84 hits per line

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

61.76
/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 Mockery;
14
use Throwable;
15

16
class ControllerTester
17
{
18

19
        private IDispatcher $dispatcher;
20

21
        private IErrorHandler $errorHandler;
22

23
        private ?IApplication $middlewaresApplication = null;
24

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

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

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

42
                if ($this->middlewaresApplication !== null) {
1✔
43
                        ob_start();
×
44

45
                        /**
46
                         * @see: https://github.com/contributte/middlewares/pull/54
47
                         * Mockery and else statement can be removed after PR is merged
48
                         */
49
                        if (method_exists($this->middlewaresApplication, 'runWith')) {
×
50
                                $response = $this->middlewaresApplication->runWith($request);
×
51
                        } else {
52
                                /** @var Mockery\MockInterface|IApplication $mock */
53
                                $mock = Mockery::mock($this->middlewaresApplication)
×
54
                                        ->shouldAllowMockingProtectedMethods();
×
55
                                $mock->shouldReceive('createInitialRequest')->andReturn($request);
×
56

57
                                $response = $mock->run();
×
58
                        }
59

60
                        ob_end_clean();
×
61

62
                        return new TestControllerResult(new ApiResponse($response));
×
63
                }
64

65
                try {
66
                        $response = $this->dispatcher->dispatch($request, $response);
1✔
67
                } catch (Throwable $exception) {
×
68
                        $response = $this->errorHandler->handle(new DispatchError($exception, $request));
×
69
                }
70

71
                return new TestControllerResult($response);
1✔
72
        }
73

74
        protected function createApiRequest(TestControllerRequest $testControllerRequest): ApiRequest
75
        {
76
                $request = (new Psr7ServerRequest(
1✔
77
                        $testControllerRequest->getMethod(),
1✔
78
                        $testControllerRequest->getUri(),
1✔
79
                        $testControllerRequest->getHeaders(),
1✔
80
                        $testControllerRequest->getRawBody(),
1✔
81
                        $testControllerRequest->getProtocolVersion(),
1✔
82
                        $testControllerRequest->getServerParams()
1✔
83
                ))->withQueryParams($testControllerRequest->getParameters());
1✔
84

85
                if (count($testControllerRequest->getFiles()) > 0) {
1✔
86
                        $request = $request->withUploadedFiles($testControllerRequest->getFiles());
×
87
                }
88

89
                if ($testControllerRequest->getParsedBody() !== null) {
1✔
90
                        $request = $request->withParsedBody($testControllerRequest->getParsedBody());
×
91
                }
92

93
                return new ApiRequest($request);
1✔
94
        }
95

96
}
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