1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace Yiisoft\Yii\Testing; |
||||
6 | |||||
7 | use Exception; |
||||
8 | use Psr\Container\ContainerInterface; |
||||
9 | use RuntimeException; |
||||
10 | |||||
11 | final class FunctionalTester |
||||
12 | { |
||||
13 | private ?TestApplicationRunner $application = null; |
||||
14 | private ?MockServiceProvider $mockServiceProvider = null; |
||||
15 | |||||
16 | public function __construct( |
||||
17 | private readonly array $defaultMocks = [ |
||||
18 | 'Yiisoft\Session\SessionInterface' => 'Yiisoft\Session\NullSession', |
||||
19 | ] |
||||
20 | ) { |
||||
21 | } |
||||
22 | |||||
23 | public function mockService(string $id, mixed $definition): void |
||||
24 | { |
||||
25 | $this->getMockServiceProvider()->setDefinition($id, $definition); |
||||
26 | } |
||||
27 | |||||
28 | public function bootstrapApplication(?string $projectRootPath = null): void |
||||
29 | { |
||||
30 | if ($this->application !== null) { |
||||
31 | return; |
||||
32 | } |
||||
33 | |||||
34 | if ($projectRootPath === null) { |
||||
35 | $projectRootPath = dirname(__DIR__, 5); |
||||
36 | } |
||||
37 | |||||
38 | $this->application = new TestApplicationRunner( |
||||
39 | responseGrabber: new ResponseGrabber(), |
||||
40 | rootPath: $projectRootPath, |
||||
41 | debug: (bool)$_ENV['YII_DEBUG'], |
||||
42 | checkEvents: false, |
||||
43 | environment: $_ENV['YII_ENV'] ?? null, |
||||
44 | ); |
||||
45 | $this->application->addProviders([$this->getMockServiceProvider()]); |
||||
0 ignored issues
–
show
|
|||||
46 | } |
||||
47 | |||||
48 | public function doRequest(string $method, string $url): ResponseAccessor |
||||
49 | { |
||||
50 | $this->ensureApplicationLoaded(); |
||||
51 | |||||
52 | $this->application?->withRequest($method, $url); |
||||
53 | $this->application?->run(); |
||||
54 | |||||
55 | return $this->application?->responseGrabber?->getResponse() ?? throw new RuntimeException( |
||||
56 | 'Either $application or $response is null' |
||||
57 | ); |
||||
58 | } |
||||
59 | |||||
60 | /** |
||||
61 | * @psalm-suppress NullableReturnStatement |
||||
62 | */ |
||||
63 | public function getContainer(): ContainerInterface |
||||
64 | { |
||||
65 | $this->ensureApplicationLoaded(); |
||||
66 | |||||
67 | $this->application?->preloadContainer(); |
||||
68 | |||||
69 | return $this->application->container ?? throw new Exception('Container was not set.'); |
||||
70 | } |
||||
71 | |||||
72 | private function ensureApplicationLoaded(): void |
||||
73 | { |
||||
74 | if ($this->application === null) { |
||||
75 | throw new RuntimeException( |
||||
76 | 'The application was not initialized. Initialize the application before the request: `$this->bootstrapApplication(\'web\')`.' |
||||
77 | ); |
||||
78 | } |
||||
79 | } |
||||
80 | |||||
81 | private function getMockServiceProvider(): MockServiceProvider |
||||
82 | { |
||||
83 | if ($this->mockServiceProvider === null) { |
||||
84 | $this->mockServiceProvider = new MockServiceProvider(); |
||||
85 | $this->fillDefaultMocks($this->mockServiceProvider); |
||||
0 ignored issues
–
show
$this->mockServiceProvider of type null is incompatible with the type Yiisoft\Yii\Testing\MockServiceProvider expected by parameter $mockServiceProvider of Yiisoft\Yii\Testing\Func...ter::fillDefaultMocks() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
86 | } |
||||
87 | return $this->mockServiceProvider; |
||||
0 ignored issues
–
show
|
|||||
88 | } |
||||
89 | |||||
90 | private function fillDefaultMocks(MockServiceProvider $mockServiceProvider): void |
||||
91 | { |
||||
92 | foreach ($this->defaultMocks as $key => $value) { |
||||
93 | if (interface_exists($key) || class_exists($key)) { |
||||
94 | $mockServiceProvider->setDefinition($key, $value); |
||||
95 | } |
||||
96 | } |
||||
97 | } |
||||
98 | } |
||||
99 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.