55use Codeception \Lib \ModuleContainer ;
66use Codeception \Module ;
77use Codeception \TestInterface ;
8+ use DEVizzent \CodeceptionMockServerHelper \Config \CleanUpBefore ;
9+ use DEVizzent \CodeceptionMockServerHelper \Config \NotMatchedRequest ;
810use GuzzleHttp \Client ;
911use GuzzleHttp \Psr7 \Request ;
1012use PHPUnit \Framework \Assert ;
1113
1214class MockServerHelper extends Module
1315{
14- const CONFIG_NOT_MATCHED_REQUEST = 'not-matched-request ' ;
15- const NOT_MATCHED_REQUEST_ACTIVATED = 'activated ' ;
16- const CONFIG_URL = 'url ' ;
16+ private const CONFIG_NOT_MATCHED_REQUEST = 'notMatchedRequest ' ;
17+ private const CONFIG_URL = 'url ' ;
18+ private const CONFIG_CLEANUP_BEFORE = 'cleanupBefore ' ;
19+ public const NOT_MATCHED_REQUEST_ID = 'not-matched-request ' ;
1720 private Client $ mockserverClient ;
21+ private CleanUpBefore $ cleanUpBefore ;
22+ private NotMatchedRequest $ notMatchedRequest ;
23+ /** @param array<string, string>|null $config */
1824 public function __construct (ModuleContainer $ moduleContainer , ?array $ config = null )
1925 {
2026 $ this ->requiredFields = [self ::CONFIG_URL ];
21- $ this ->config [ ' cleanupBefore ' ] = ' test ' ;
22- $ this ->config [ self :: CONFIG_NOT_MATCHED_REQUEST ] = self :: NOT_MATCHED_REQUEST_ACTIVATED ;
27+ $ this ->cleanUpBefore = new CleanUpBefore (CleanUpBefore:: TEST ) ;
28+ $ this ->notMatchedRequest = new NotMatchedRequest (NotMatchedRequest:: ENABLED ) ;
2329 parent ::__construct ($ moduleContainer , $ config );
2430 }
2531
2632
2733 public function _initialize (): void
2834 {
35+ parent ::_initialize ();
36+ if (is_string ($ this ->config [self ::CONFIG_NOT_MATCHED_REQUEST ] ?? null )) {
37+ $ this ->notMatchedRequest = new NotMatchedRequest ($ this ->config [self ::CONFIG_NOT_MATCHED_REQUEST ]);
38+ }
39+ if (is_string ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ] ?? null )) {
40+ $ this ->cleanUpBefore = new CleanUpBefore ($ this ->config [self ::CONFIG_CLEANUP_BEFORE ]);
41+ }
2942 $ this ->mockserverClient = new Client ([
3043 'base_uri ' => $ this ->config [self ::CONFIG_URL ]
3144 ]);
32- if (self ::NOT_MATCHED_REQUEST_ACTIVATED === $ this ->config [self ::CONFIG_NOT_MATCHED_REQUEST ]) {
33- $ this ->createMockRequest (file_get_contents (__DIR__ .'/not-matched-request.json ' ));
45+ if ($ this ->notMatchedRequest ->isEnabled ()) {
46+ $ expectationJson = file_get_contents (__DIR__ . '/not-matched-request.json ' );
47+ Assert::assertIsString ($ expectationJson );
48+ $ this ->createMockRequest ($ expectationJson );
3449 }
3550 }
3651
3752 public function _beforeSuite (array $ settings = []): void
3853 {
39- if ('suite ' === $ this ->config ['cleanupBefore ' ]) {
54+ parent ::_beforeSuite ($ settings );
55+ if ($ this ->cleanUpBefore ->isSuite ()) {
4056 $ this ->clearMockServerLogs ();
4157 }
4258 }
4359
44- public function _before (TestInterface $ test ):void
60+ public function _before (TestInterface $ test ): void
4561 {
46- if ('test ' === $ this ->config ['cleanupBefore ' ]) {
62+ parent ::_before ($ test );
63+ if ($ this ->cleanUpBefore ->isTest ()) {
4764 $ this ->clearMockServerLogs ();
4865 }
4966 }
@@ -94,21 +111,20 @@ public function clearMockServerLogs(): void
94111 $ response ->getStatusCode (),
95112 $ response ->getBody ()->getContents ()
96113 );
97-
98114 }
99115
100116 public function deactivateNotMatchedRequest (): void
101117 {
102118 $ body = json_encode ([
103- 'id ' => self ::CONFIG_NOT_MATCHED_REQUEST
119+ 'id ' => self ::NOT_MATCHED_REQUEST_ID
104120 ]);
121+ Assert::assertIsString ($ body );
105122 $ request = new Request ('PUT ' , '/mockserver/clear?type=expectations ' , [], $ body );
106123 $ response = $ this ->mockserverClient ->sendRequest ($ request );
107124 Assert::assertEquals (
108125 200 ,
109126 $ response ->getStatusCode (),
110127 $ response ->getBody ()->getContents ()
111128 );
112-
113129 }
114- }
130+ }
0 commit comments