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,22 @@ 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 ) {
62+ return ;
63+ }
5564 }
5665
5766 public function _beforeSuite ($ settings = []): void
@@ -60,6 +69,9 @@ public function _beforeSuite($settings = []): void
6069 if ($ this ->cleanUpBefore ->isSuite ()) {
6170 $ this ->mockserver ->clearLogs ();
6271 }
72+ foreach ($ this ->expectationPath ->getExpectationsFiles () as $ expectationFile ) {
73+ $ this ->createMockRequestFromJsonFile ($ expectationFile );
74+ }
6375 }
6476
6577 public function _before (TestInterface $ test ): void
@@ -105,6 +117,11 @@ public function removeMockRequest(string $mockRequestId): void
105117 $ this ->mockserver ->removeById ($ mockRequestId );
106118 }
107119
120+ public function removeAllMockRequest (): void
121+ {
122+ $ this ->mockserver ->removeAllExpectations ();
123+ }
124+
108125 public function clearMockServerLogs (): void
109126 {
110127 $ this ->mockserver ->clearLogs ();
@@ -114,4 +131,11 @@ public function deactivateNotMatchedRequest(): void
114131 {
115132 $ this ->mockserver ->removeById (self ::NOT_MATCHED_REQUEST_ID );
116133 }
134+
135+ public function createMockRequestFromJsonFile (string $ expectationFile ): void
136+ {
137+ $ expectationJson = file_get_contents ($ expectationFile );
138+ Assert::assertIsString ($ expectationJson );
139+ $ this ->createMockRequest ($ expectationJson );
140+ }
117141}
0 commit comments