|
13 | 13 |
|
14 | 14 | use PHPUnit\Framework\TestCase;
|
15 | 15 | use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
|
| 16 | +use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation; |
16 | 17 | use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
|
17 | 18 |
|
18 | 19 | class ConfigurationTest extends TestCase
|
19 | 20 | {
|
| 21 | + private $files; |
| 22 | + |
20 | 23 | public function testItThrowsOnStringishValue()
|
21 | 24 | {
|
22 | 25 | $this->expectException(\InvalidArgumentException::class);
|
@@ -244,4 +247,169 @@ private function buildGroups($counts)
|
244 | 247 |
|
245 | 248 | return $groups;
|
246 | 249 | }
|
| 250 | + |
| 251 | + public function testBaselineGenerationEmptyFile() |
| 252 | + { |
| 253 | + $filename = $this->createFile(); |
| 254 | + $configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile=' . urlencode($filename)); |
| 255 | + $this->assertTrue($configuration->isGeneratingBaseline()); |
| 256 | + $trace = debug_backtrace(); |
| 257 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, ''))); |
| 258 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 2', $trace, ''))); |
| 259 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, ''))); |
| 260 | + $configuration->writeBaseline(); |
| 261 | + $this->assertEquals($filename, $configuration->getBaselineFile()); |
| 262 | + $expected_baseline = [ |
| 263 | + [ |
| 264 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 265 | + 'message' => 'Test message 1', |
| 266 | + 'count' => 2, |
| 267 | + ], |
| 268 | + [ |
| 269 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 270 | + 'message' => 'Test message 2', |
| 271 | + 'count' => 1, |
| 272 | + ], |
| 273 | + ]; |
| 274 | + $this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename)); |
| 275 | + } |
| 276 | + |
| 277 | + public function testBaselineGenerationNoFile() |
| 278 | + { |
| 279 | + $filename = $this->createFile(); |
| 280 | + $configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile=' . urlencode($filename)); |
| 281 | + $this->assertTrue($configuration->isGeneratingBaseline()); |
| 282 | + $trace = debug_backtrace(); |
| 283 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, ''))); |
| 284 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 2', $trace, ''))); |
| 285 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 2', $trace, ''))); |
| 286 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, ''))); |
| 287 | + $configuration->writeBaseline(); |
| 288 | + $this->assertEquals($filename, $configuration->getBaselineFile()); |
| 289 | + $expected_baseline = [ |
| 290 | + [ |
| 291 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 292 | + 'message' => 'Test message 1', |
| 293 | + 'count' => 2, |
| 294 | + ], |
| 295 | + [ |
| 296 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 297 | + 'message' => 'Test message 2', |
| 298 | + 'count' => 2, |
| 299 | + ], |
| 300 | + ]; |
| 301 | + $this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename)); |
| 302 | + |
| 303 | + } |
| 304 | + |
| 305 | + public function testExistingBaseline() |
| 306 | + { |
| 307 | + $filename = $this->createFile(); |
| 308 | + $baseline = [ |
| 309 | + [ |
| 310 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 311 | + 'message' => 'Test message 1', |
| 312 | + 'count' => 1, |
| 313 | + ], |
| 314 | + [ |
| 315 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 316 | + 'message' => 'Test message 2', |
| 317 | + 'count' => 1, |
| 318 | + ], |
| 319 | + ]; |
| 320 | + file_put_contents($filename, json_encode($baseline)); |
| 321 | + |
| 322 | + $configuration = Configuration::fromUrlEncodedString('baselineFile=' . urlencode($filename)); |
| 323 | + $this->assertFalse($configuration->isGeneratingBaseline()); |
| 324 | + $trace = debug_backtrace(); |
| 325 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 1', $trace, ''))); |
| 326 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 2', $trace, ''))); |
| 327 | + $this->assertFalse($configuration->isBaselineDeprecation(new Deprecation('Test message 3', $trace, ''))); |
| 328 | + $this->assertEquals($filename, $configuration->getBaselineFile()); |
| 329 | + } |
| 330 | + |
| 331 | + public function testExistingBaselineAndGeneration() |
| 332 | + { |
| 333 | + $filename = $this->createFile(); |
| 334 | + $baseline = [ |
| 335 | + [ |
| 336 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 337 | + 'message' => 'Test message 1', |
| 338 | + 'count' => 1, |
| 339 | + ], |
| 340 | + [ |
| 341 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 342 | + 'message' => 'Test message 2', |
| 343 | + 'count' => 1, |
| 344 | + ], |
| 345 | + ]; |
| 346 | + file_put_contents($filename, json_encode($baseline)); |
| 347 | + $configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile=' . urlencode($filename)); |
| 348 | + $this->assertTrue($configuration->isGeneratingBaseline()); |
| 349 | + $trace = debug_backtrace(); |
| 350 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 2', $trace, ''))); |
| 351 | + $this->assertTrue($configuration->isBaselineDeprecation(new Deprecation('Test message 3', $trace, ''))); |
| 352 | + $configuration->writeBaseline(); |
| 353 | + $this->assertEquals($filename, $configuration->getBaselineFile()); |
| 354 | + $expected_baseline = [ |
| 355 | + [ |
| 356 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 357 | + 'message' => 'Test message 2', |
| 358 | + 'count' => 1, |
| 359 | + ], |
| 360 | + [ |
| 361 | + 'location' => 'Symfony\Bridge\PhpUnit\Tests\DeprecationErrorHandler\ConfigurationTest::runTest', |
| 362 | + 'message' => 'Test message 3', |
| 363 | + 'count' => 1, |
| 364 | + ], |
| 365 | + ]; |
| 366 | + $this->assertEquals(json_encode($expected_baseline, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES), file_get_contents($filename)); |
| 367 | + } |
| 368 | + |
| 369 | + public function testBaselineArgumentException() |
| 370 | + { |
| 371 | + $this->expectException(\InvalidArgumentException::class); |
| 372 | + $this->expectExceptionMessage('You cannot use the "generateBaseline" configuration option without providing a "baselineFile" configuration option.'); |
| 373 | + Configuration::fromUrlEncodedString('generateBaseline=true'); |
| 374 | + } |
| 375 | + |
| 376 | + public function testBaselineFileException() |
| 377 | + { |
| 378 | + $filename = $this->createFile(); |
| 379 | + unlink($filename); |
| 380 | + $this->expectException(\InvalidArgumentException::class); |
| 381 | + $this->expectExceptionMessage(sprintf('The baselineFile "%s" does not exist.', $filename)); |
| 382 | + Configuration::fromUrlEncodedString('baselineFile=' . urlencode($filename)); |
| 383 | + } |
| 384 | + |
| 385 | + public function testBaselineFileWriteError() |
| 386 | + { |
| 387 | + $filename = $this->createFile(); |
| 388 | + chmod($filename, 0444); |
| 389 | + $this->expectError(); |
| 390 | + $this->expectErrorMessageMatches('/failed to open stream: Permission denied/'); |
| 391 | + $configuration = Configuration::fromUrlEncodedString('generateBaseline=true&baselineFile=' . urlencode($filename)); |
| 392 | + $configuration->writeBaseline(); |
| 393 | + } |
| 394 | + |
| 395 | + protected function setUp(): void |
| 396 | + { |
| 397 | + $this->files = []; |
| 398 | + } |
| 399 | + |
| 400 | + protected function tearDown(): void |
| 401 | + { |
| 402 | + foreach ($this->files as $file) { |
| 403 | + if (file_exists($file)) { |
| 404 | + unlink($file); |
| 405 | + } |
| 406 | + } |
| 407 | + } |
| 408 | + |
| 409 | + private function createFile() { |
| 410 | + $filename = tempnam(sys_get_temp_dir(), 'sf-'); |
| 411 | + $this->files[] = $filename; |
| 412 | + return $filename; |
| 413 | + } |
| 414 | + |
247 | 415 | }
|
0 commit comments