|
7 | 7 | use PHPUnit\Framework\ExpectationFailedException;
|
8 | 8 | use PHPUnit\Framework\MockObject\MockObject;
|
9 | 9 | use PHPUnit\Framework\TestCase;
|
| 10 | +use ReflectionClass; |
10 | 11 | use Spatie\Snapshots\MatchesSnapshots;
|
11 | 12 |
|
12 | 13 | class MatchesSnapshotTest extends TestCase
|
@@ -462,30 +463,46 @@ private function expectFailedMatchesSnapshotTest()
|
462 | 463 | $this->expectException(ExpectationFailedException::class);
|
463 | 464 | }
|
464 | 465 |
|
465 |
| - /** |
466 |
| - * @return \PHPUnit\Framework\MockObject\MockObject|\Spatie\Snapshots\MatchesSnapshots |
467 |
| - */ |
468 | 466 | private function getMatchesSnapshotMock(bool $mockGetSnapshotId = true): MockObject
|
469 | 467 | {
|
470 |
| - $mockMethods = [ |
| 468 | + // Define a class name for our temporary trait user |
| 469 | + $className = 'TemporaryTraitClass' . md5(microtime()); |
| 470 | + |
| 471 | + // Create the class definition with the methods we need to mock |
| 472 | + $classDefinition = 'class ' . $className . ' { |
| 473 | + use \\Spatie\\Snapshots\\MatchesSnapshots; |
| 474 | +
|
| 475 | + public function markTestIncomplete($message = "") {} |
| 476 | + public function getSnapshotId() { return ""; } |
| 477 | + public function getSnapshotDirectory() { return ""; } |
| 478 | + public function getFileSnapshotDirectory() { return ""; } |
| 479 | + public function fail($message = "") {} |
| 480 | + public function assertTrue($condition, $message = "") {} |
| 481 | + }'; |
| 482 | + |
| 483 | + // Evaluate the class definition |
| 484 | + eval($classDefinition); |
| 485 | + |
| 486 | + // Create the mock builder |
| 487 | + $mockBuilder = $this->getMockBuilder($className); |
| 488 | + |
| 489 | + // Disable original constructor to avoid issues |
| 490 | + $mockBuilder->disableOriginalConstructor(); |
| 491 | + |
| 492 | + // For PHPUnit 9.x, use onlyMethods instead of setMethods |
| 493 | + $mockBuilder->onlyMethods([ |
471 | 494 | 'markTestIncomplete',
|
472 | 495 | 'getSnapshotId',
|
473 | 496 | 'getSnapshotDirectory',
|
474 | 497 | 'getFileSnapshotDirectory',
|
475 | 498 | 'fail',
|
476 | 499 | 'assertTrue',
|
477 |
| - ]; |
478 |
| - |
479 |
| - $matchesSnapshotMock = $this->getMockForTrait( |
480 |
| - MatchesSnapshots::class, |
481 |
| - [], |
482 |
| - '', |
483 |
| - true, |
484 |
| - true, |
485 |
| - true, |
486 |
| - $mockMethods |
487 |
| - ); |
| 500 | + ]); |
| 501 | + |
| 502 | + // Get the mock object |
| 503 | + $matchesSnapshotMock = $mockBuilder->getMock(); |
488 | 504 |
|
| 505 | + // Configure expected method calls |
489 | 506 | if ($mockGetSnapshotId) {
|
490 | 507 | $matchesSnapshotMock
|
491 | 508 | ->expects($this->any())
|
|
0 commit comments