77use Codeception \TestInterface ;
88use DEVizzent \CodeceptionMockServerHelper \Client \MockServer ;
99use DEVizzent \CodeceptionMockServerHelper \Config \CleanUpBefore ;
10+ use DEVizzent \CodeceptionMockServerHelper \Config \ExpectationsPath ;
1011use DEVizzent \CodeceptionMockServerHelper \Config \NotMatchedRequest ;
1112use GuzzleHttp \Client ;
12- use GuzzleHttp \Psr7 \Request ;
1313use PHPUnit \Framework \Assert ;
14+ use PHPUnit \Framework \AssertionFailedError ;
1415use PHPUnit \Framework \ExpectationFailedException ;
1516
1617class MockServerHelper extends Module
1718{
1819 private const CONFIG_NOT_MATCHED_REQUEST = 'notMatchedRequest ' ;
1920 private const CONFIG_URL = 'url ' ;
2021 private const CONFIG_CLEANUP_BEFORE = 'cleanupBefore ' ;
22+ private const CONFIG_EXPECTATIONS_PATH = 'expectationsPath ' ;
2123 public const NOT_MATCHED_REQUEST_ID = 'not-matched-request ' ;
2224 private MockServer $ mockserver ;
2325 private CleanUpBefore $ cleanUpBefore ;
2426 private NotMatchedRequest $ notMatchedRequest ;
27+ private ExpectationsPath $ expectationPath ;
2528 /** @param array<string, string>|null $config */
2629 public function __construct (ModuleContainer $ moduleContainer , ?array $ config = null )
2730 {
2831 $ this ->requiredFields = [self ::CONFIG_URL ];
2932 $ this ->cleanUpBefore = new CleanUpBefore (CleanUpBefore::TEST );
3033 $ this ->notMatchedRequest = new NotMatchedRequest (NotMatchedRequest::ENABLED );
34+ $ this ->expectationPath = new ExpectationsPath ();
3135 parent ::__construct ($ moduleContainer , $ config );
3236 }
3337
@@ -41,17 +45,20 @@ public function _initialize(): void
4145 if (is_string ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ] ?? null )) {
4246 $ this ->cleanUpBefore = new CleanUpBefore ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ]);
4347 }
48+ if (is_string ($ this ->config [self ::CONFIG_EXPECTATIONS_PATH ] ?? null )) {
49+ $ this ->expectationPath = new ExpectationsPath ($ this ->config [self ::CONFIG_EXPECTATIONS_PATH ]);
50+ }
4451 $ this ->mockserver = new MockServer (new Client ([
4552 'base_uri ' => $ this ->config [self ::CONFIG_URL ]
4653 ]));
4754 if ($ this ->notMatchedRequest ->isEnabled ()) {
48- $ expectationJson = file_get_contents (__DIR__ . '/not-matched-request.json ' );
49- Assert::assertIsString ($ expectationJson );
50- $ this ->createMockRequest ($ expectationJson );
55+ $ this ->createMockRequestFromJsonFile (__DIR__ . '/not-matched-request.json ' );
5156 return ;
5257 }
5358
54- $ this ->deactivateNotMatchedRequest ();
59+ try {
60+ $ this ->deactivateNotMatchedRequest ();
61+ } catch (AssertionFailedError $ exception ) {}
5562 }
5663
5764 public function _beforeSuite ($ settings = []): void
@@ -60,6 +67,9 @@ public function _beforeSuite($settings = []): void
6067 if ($ this ->cleanUpBefore ->isSuite ()) {
6168 $ this ->mockserver ->clearLogs ();
6269 }
70+ foreach ($ this ->expectationPath ->getExpectationsFiles () as $ expectationFile ) {
71+ $ this ->createMockRequestFromJsonFile ($ expectationFile );
72+ }
6373 }
6474
6575 public function _before (TestInterface $ test ): void
@@ -105,6 +115,11 @@ public function removeMockRequest(string $mockRequestId): void
105115 $ this ->mockserver ->removeById ($ mockRequestId );
106116 }
107117
118+ public function removeAllMockRequest (): void
119+ {
120+ $ this ->mockserver ->removeAllExpectations ();
121+ }
122+
108123 public function clearMockServerLogs (): void
109124 {
110125 $ this ->mockserver ->clearLogs ();
@@ -114,4 +129,11 @@ public function deactivateNotMatchedRequest(): void
114129 {
115130 $ this ->mockserver ->removeById (self ::NOT_MATCHED_REQUEST_ID );
116131 }
132+
133+ public function createMockRequestFromJsonFile ($ expectationFile ): void
134+ {
135+ $ expectationJson = file_get_contents ($ expectationFile );
136+ Assert::assertIsString ($ expectationJson );
137+ $ this ->createMockRequest ($ expectationJson );
138+ }
117139}
0 commit comments