|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Test\DEVizzent\CodeceptionMockServerHelper\Integration; |
| 4 | + |
| 5 | +use Codeception\Lib\ModuleContainer; |
| 6 | +use DEVizzent\CodeceptionMockServerHelper\Config\NotMatchedRequest; |
| 7 | +use DEVizzent\CodeceptionMockServerHelper\MockServerHelper; |
| 8 | +use GuzzleHttp\Client; |
| 9 | +use PHPUnit\Framework\ExpectationFailedException; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | + |
| 12 | +class SeeAllRequestWereMatchedTest extends TestCase |
| 13 | +{ |
| 14 | + private MockServerHelper $sot; |
| 15 | + private Client $client; |
| 16 | + |
| 17 | + protected function initialize(string $notMatchedRequest): void |
| 18 | + { |
| 19 | + $moduleContainer = $this->createMock(ModuleContainer::class); |
| 20 | + $config = ['url' => 'http://mockserver:1080', 'notMatchedRequest' => $notMatchedRequest]; |
| 21 | + $this->sot = new MockServerHelper($moduleContainer, $config); |
| 22 | + $this->sot->_initialize(); |
| 23 | + $this->client = new Client(['proxy' => 'http://mockserver:1080', 'verify' => false]); |
| 24 | + $this->sot->clearMockServerLogs(); |
| 25 | + } |
| 26 | + |
| 27 | + public function testAllRequestWereMatchedWhenConfigDisabledThrowException(): void |
| 28 | + { |
| 29 | + $this->initialize(NotMatchedRequest::DISABLED); |
| 30 | + $this->expectException(ExpectationFailedException::class); |
| 31 | + $this->expectExceptionMessage( |
| 32 | + '\'seeAllRequestWereMatched\' can\'t be used without enable notMatchedRequest in config.' |
| 33 | + ); |
| 34 | + $this->sot->seeAllRequestWereMatched(); |
| 35 | + } |
| 36 | + |
| 37 | + public function testAllRequestWereMatched(): void |
| 38 | + { |
| 39 | + $this->initialize(NotMatchedRequest::ENABLED); |
| 40 | + $this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1'); |
| 41 | + $this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/2'); |
| 42 | + $this->sot->seeAllRequestWereMatched(); |
| 43 | + } |
| 44 | + |
| 45 | + public function testNotAllRequestWereMatchedThrowException(): void |
| 46 | + { |
| 47 | + $this->initialize(NotMatchedRequest::ENABLED); |
| 48 | + $this->expectException(ExpectationFailedException::class); |
| 49 | + $this->expectExceptionMessageMatches('/REQUEST NOT MATCHED was:.*/'); |
| 50 | + $this->client->request('GET', 'https://jsonplaceholder.typicode.com/posts/1'); |
| 51 | + $this->client->request( |
| 52 | + 'GET', |
| 53 | + 'https://jsonplaceholder.typicode.com/posts/3', |
| 54 | + ['http_errors' => false] |
| 55 | + ); |
| 56 | + $this->sot->seeAllRequestWereMatched(); |
| 57 | + } |
| 58 | +} |
0 commit comments