@@ -14,6 +14,20 @@ class CheckCommandTest extends TestCase
1414
1515 const ERROR_CODE = 1 ;
1616
17+ /** @var string */
18+ private $ customBaselineFilename = __DIR__ .'/my-baseline.json ' ;
19+ private $ defaultBaselineFilename = 'phparkitect-baseline.json ' ;
20+
21+ protected function tearDown (): void
22+ {
23+ if (file_exists ($ this ->customBaselineFilename )) {
24+ unlink ($ this ->customBaselineFilename );
25+ }
26+ if (file_exists ($ this ->defaultBaselineFilename )) {
27+ unlink ($ this ->defaultBaselineFilename );
28+ }
29+ }
30+
1731 public function test_app_returns_error_with_multiple_violations (): void
1832 {
1933 $ cmdTester = $ this ->runCheck (__DIR__ .'/../_fixtures/configMvc.php ' );
@@ -86,15 +100,77 @@ public function test_bug_yield(): void
86100 $ this ->assertCheckHasErrors ($ cmdTester , $ expectedErrors );
87101 }
88102
89- protected function runCheck ($ configFilePath = null , bool $ stopOnFailure = null ): ApplicationTester
103+ public function test_baseline (): void
104+ {
105+ $ configFilePath = __DIR__ .'/../_fixtures/configMvcForYieldBug.php ' ;
106+
107+ // Produce the baseline
108+
109+ $ this ->runCheck ($ configFilePath , null , null , $ this ->customBaselineFilename );
110+
111+ // Check it detects error if baseline is not used
112+
113+ $ cmdTester = $ this ->runCheck ($ configFilePath , null , null );
114+
115+ $ this ->assertCheckHasErrors ($ cmdTester );
116+
117+ // Check it ignores error if baseline is used
118+
119+ $ cmdTester = $ this ->runCheck ($ configFilePath , null , $ this ->customBaselineFilename );
120+ $ this ->assertCheckHasSuccess ($ cmdTester );
121+ }
122+
123+ public function test_baseline_with_default_filename_is_enabled_automatically (): void
90124 {
125+ $ configFilePath = __DIR__ .'/../_fixtures/configMvcForYieldBug.php ' ;
126+
127+ // Produce the baseline
128+
129+ $ this ->runCheck ($ configFilePath , null , null , null );
130+
131+ // Check it ignores error if baseline is used
132+
133+ $ cmdTester = $ this ->runCheck ($ configFilePath , null , null );
134+ $ this ->assertCheckHasSuccess ($ cmdTester );
135+ }
136+
137+ public function test_you_can_ignore_the_default_baseline (): void
138+ {
139+ $ configFilePath = __DIR__ .'/../_fixtures/configMvcForYieldBug.php ' ;
140+
141+ // Produce the baseline
142+ $ this ->runCheck ($ configFilePath , null , null , null );
143+
144+ // Check it ignores the default baseline
145+ $ cmdTester = $ this ->runCheck ($ configFilePath , null , null , false , true );
146+ $ this ->assertCheckHasErrors ($ cmdTester );
147+ }
148+
149+ protected function runCheck (
150+ $ configFilePath = null ,
151+ bool $ stopOnFailure = null ,
152+ ?string $ useBaseline = null ,
153+ $ generateBaseline = false ,
154+ bool $ skipBaseline = false
155+ ): ApplicationTester {
91156 $ input = ['check ' ];
92157 if (null !== $ configFilePath ) {
93158 $ input ['--config ' ] = $ configFilePath ;
94159 }
95160 if (null !== $ stopOnFailure ) {
96161 $ input ['--stop-on-failure ' ] = true ;
97162 }
163+ if (null !== $ useBaseline ) {
164+ $ input ['--use-baseline ' ] = $ useBaseline ;
165+ }
166+ if ($ skipBaseline ) {
167+ $ input ['--skip-baseline ' ] = true ;
168+ }
169+
170+ // false = option not set, null = option set but without value, string = option with value
171+ if (false !== $ generateBaseline ) {
172+ $ input ['--generate-baseline ' ] = $ generateBaseline ;
173+ }
98174
99175 $ app = new PhpArkitectApplication ();
100176 $ app ->setAutoExit (false );
@@ -127,7 +203,7 @@ protected function assertCheckHasNoErrorsLike(ApplicationTester $applicationTest
127203
128204 protected function assertCheckHasSuccess (ApplicationTester $ applicationTester ): void
129205 {
130- $ this ->assertEquals (self ::SUCCESS_CODE , $ applicationTester ->getStatusCode ());
131- $ this ->assertStringNotContainsString ('ERRORS! ' , $ applicationTester ->getDisplay ());
206+ $ this ->assertEquals (self ::SUCCESS_CODE , $ applicationTester ->getStatusCode (), ' Command failed: ' . $ applicationTester -> getDisplay () );
207+ $ this ->assertStringNotContainsString ('ERRORS! ' , $ applicationTester ->getDisplay (), ' Error message not expected in successful execution ' );
132208 }
133209}
0 commit comments