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

adbrosaci / controller-tester / 4164093905

pending completion
4164093905

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
                                $mock = Mockery::mock($this->middlewaresApplication)
×
53
                                        ->shouldAllowMockingProtectedMethods();
×
54
                                $mock->shouldReceive('createInitialRequest')->andReturn($request);
×
55

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

59
                        ob_end_clean();
×
60

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

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

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

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

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

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

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

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