|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace WebProject\Codeception\Module\Tests\Unit; |
| 6 | + |
| 7 | +use Codeception\Exception\ModuleConfigException; |
| 8 | +use Codeception\Lib\ModuleContainer; |
| 9 | +use Codeception\Module\PhpBrowser; |
| 10 | +use Codeception\Module\REST; |
| 11 | +use Codeception\Test\Unit; |
| 12 | +use function in_array; |
| 13 | +use PHPUnit\Framework\MockObject\MockObject; |
| 14 | +use ReflectionClass; |
| 15 | +use WebProject\Codeception\Module\OpenApiServerMock; |
| 16 | +use WebProject\Codeception\Module\Tests\Support\UnitTester; |
| 17 | +use WebProject\PhpOpenApiMockServer\Middleware\MockMiddleware\OpenApiMockMiddleware; |
| 18 | + |
| 19 | +class OpenApiServerMockTest extends Unit |
| 20 | +{ |
| 21 | + protected UnitTester $tester; |
| 22 | + |
| 23 | + private OpenApiServerMock $module; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var ModuleContainer&MockObject |
| 27 | + */ |
| 28 | + private $moduleContainer; |
| 29 | + |
| 30 | + protected function _before(): void |
| 31 | + { |
| 32 | + $this->moduleContainer = $this->createMock(ModuleContainer::class); |
| 33 | + $this->module = new OpenApiServerMock($this->moduleContainer); |
| 34 | + } |
| 35 | + |
| 36 | + public function testGetOpenApiMockServerUrl(): void |
| 37 | + { |
| 38 | + // Act |
| 39 | + $url = $this->module->getOpenApiMockServerUrl(); |
| 40 | + |
| 41 | + // Assert |
| 42 | + self::assertSame('http://localhost:8080', $url); |
| 43 | + } |
| 44 | + |
| 45 | + public function testAutoDetectPath(): void |
| 46 | + { |
| 47 | + // Arrange |
| 48 | + $reflection = new ReflectionClass($this->module); |
| 49 | + $method = $reflection->getMethod('autoDetectPath'); |
| 50 | + $method->setAccessible(true); |
| 51 | + |
| 52 | + // Act |
| 53 | + $path = $method->invoke($this->module); |
| 54 | + |
| 55 | + // Assert |
| 56 | + self::assertNotNull($path); |
| 57 | + self::assertDirectoryExists($path); |
| 58 | + self::assertFileExists($path . '/bin/openapi-mock-server'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testValidatePathSuccess(): void |
| 62 | + { |
| 63 | + // Arrange |
| 64 | + $reflection = new ReflectionClass($this->module); |
| 65 | + $method = $reflection->getMethod('validatePath'); |
| 66 | + $method->setAccessible(true); |
| 67 | + $path = $reflection->getMethod('autoDetectPath')->invoke($this->module); |
| 68 | + |
| 69 | + // Act & Assert (should not throw exception) |
| 70 | + $method->invoke($this->module, $path); |
| 71 | + } |
| 72 | + |
| 73 | + public function testValidatePathFailure(): void |
| 74 | + { |
| 75 | + // Arrange |
| 76 | + $reflection = new ReflectionClass($this->module); |
| 77 | + $method = $reflection->getMethod('validatePath'); |
| 78 | + $method->setAccessible(true); |
| 79 | + |
| 80 | + // Assert |
| 81 | + $this->expectException(ModuleConfigException::class); |
| 82 | + |
| 83 | + // Act |
| 84 | + $method->invoke($this->module, '/non/existent/path'); |
| 85 | + } |
| 86 | + |
| 87 | + public function testValidateSpecFailure(): void |
| 88 | + { |
| 89 | + // Arrange |
| 90 | + $reflection = new ReflectionClass($this->module); |
| 91 | + $method = $reflection->getMethod('validateSpec'); |
| 92 | + $method->setAccessible(true); |
| 93 | + |
| 94 | + // Assert |
| 95 | + $this->expectException(ModuleConfigException::class); |
| 96 | + $this->expectExceptionMessage('OpenAPI specification file not found'); |
| 97 | + |
| 98 | + // Act |
| 99 | + $method->invoke($this->module, 'non-existent-spec.yaml'); |
| 100 | + } |
| 101 | + |
| 102 | + public function testHaveOpenApiMockStatusCode(): void |
| 103 | + { |
| 104 | + // Arrange |
| 105 | + $rest = $this->createMock(REST::class); |
| 106 | + $phpBrowser = $this->createMock(PhpBrowser::class); |
| 107 | + |
| 108 | + $this->moduleContainer->expects(self::any())->method('hasModule')->willReturnCallback(static function ($name) { |
| 109 | + return in_array($name, ['REST', 'PhpBrowser'], true); |
| 110 | + }); |
| 111 | + |
| 112 | + $this->moduleContainer->expects(self::any())->method('getModule')->willReturnCallback(static function ($name) use ($rest, $phpBrowser) { |
| 113 | + return 'REST' === $name ? $rest : $phpBrowser; |
| 114 | + }); |
| 115 | + |
| 116 | + $rest->expects(self::once()) |
| 117 | + ->method('haveHttpHeader') |
| 118 | + ->with(OpenApiMockMiddleware::HEADER_OPENAPI_MOCK_STATUSCODE, '404'); |
| 119 | + |
| 120 | + $phpBrowser->expects(self::once()) |
| 121 | + ->method('setHeader') |
| 122 | + ->with(OpenApiMockMiddleware::HEADER_OPENAPI_MOCK_STATUSCODE, '404'); |
| 123 | + |
| 124 | + // Act |
| 125 | + $this->module->haveOpenApiMockStatusCode(404); |
| 126 | + } |
| 127 | + |
| 128 | + public function testHaveOpenApiMockExample(): void |
| 129 | + { |
| 130 | + // Arrange |
| 131 | + $rest = $this->createMock(REST::class); |
| 132 | + $this->moduleContainer->expects(self::any())->method('hasModule')->willReturnMap([['REST', true], ['PhpBrowser', false]]); |
| 133 | + $this->moduleContainer->expects(self::any())->method('getModule')->with('REST')->willReturn($rest); |
| 134 | + |
| 135 | + $rest->expects(self::once()) |
| 136 | + ->method('haveHttpHeader') |
| 137 | + ->with(OpenApiMockMiddleware::HEADER_OPENAPI_MOCK_EXAMPLE, 'my-example'); |
| 138 | + |
| 139 | + // Act |
| 140 | + $this->module->haveOpenApiMockExample('my-example'); |
| 141 | + } |
| 142 | + |
| 143 | + public function testSetOpenApiMockActive(): void |
| 144 | + { |
| 145 | + // Arrange |
| 146 | + $rest = $this->createMock(REST::class); |
| 147 | + $this->moduleContainer->expects(self::any())->method('hasModule')->willReturnMap([['REST', true], ['PhpBrowser', false]]); |
| 148 | + $this->moduleContainer->expects(self::any())->method('getModule')->with('REST')->willReturn($rest); |
| 149 | + |
| 150 | + $rest->expects(self::once()) |
| 151 | + ->method('haveHttpHeader') |
| 152 | + ->with(OpenApiMockMiddleware::HEADER_OPENAPI_MOCK_ACTIVE, 'true'); |
| 153 | + |
| 154 | + // Act |
| 155 | + $this->module->setOpenApiMockActive(true); |
| 156 | + } |
| 157 | + |
| 158 | + public function testInitializeWithDefaults(): void |
| 159 | + { |
| 160 | + // Arrange |
| 161 | + $reflection = new ReflectionClass($this->module); |
| 162 | + $configProp = $reflection->getProperty('config'); |
| 163 | + $configProp->setAccessible(true); |
| 164 | + $config = $configProp->getValue($this->module); |
| 165 | + $config['spec'] = 'tests/Support/Data/openapi.yaml'; |
| 166 | + $configProp->setValue($this->module, $config); |
| 167 | + |
| 168 | + // Act |
| 169 | + $this->module->_initialize(); |
| 170 | + |
| 171 | + // Assert |
| 172 | + $config = $configProp->getValue($this->module); |
| 173 | + |
| 174 | + self::assertNotNull($config['path']); |
| 175 | + self::assertDirectoryExists($config['path']); |
| 176 | + } |
| 177 | +} |
0 commit comments