SplObjectStorageComparatorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of sebastian/comparator.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Comparator;
  11. use PHPUnit\Framework\TestCase;
  12. use SplObjectStorage;
  13. use stdClass;
  14. /**
  15. * @covers \SebastianBergmann\Comparator\SplObjectStorageComparator<extended>
  16. *
  17. * @uses \SebastianBergmann\Comparator\Comparator
  18. * @uses \SebastianBergmann\Comparator\Factory
  19. * @uses \SebastianBergmann\Comparator\ComparisonFailure
  20. */
  21. final class SplObjectStorageComparatorTest extends TestCase
  22. {
  23. /**
  24. * @var SplObjectStorageComparator
  25. */
  26. private $comparator;
  27. protected function setUp(): void
  28. {
  29. $this->comparator = new SplObjectStorageComparator;
  30. }
  31. public function acceptsFailsProvider()
  32. {
  33. return [
  34. [new SplObjectStorage, new stdClass],
  35. [new stdClass, new SplObjectStorage],
  36. [new stdClass, new stdClass]
  37. ];
  38. }
  39. public function assertEqualsSucceedsProvider()
  40. {
  41. $object1 = new stdClass();
  42. $object2 = new stdClass();
  43. $storage1 = new SplObjectStorage();
  44. $storage2 = new SplObjectStorage();
  45. $storage3 = new SplObjectStorage();
  46. $storage3->attach($object1);
  47. $storage3->attach($object2);
  48. $storage4 = new SplObjectStorage();
  49. $storage4->attach($object2);
  50. $storage4->attach($object1);
  51. return [
  52. [$storage1, $storage1],
  53. [$storage1, $storage2],
  54. [$storage3, $storage3],
  55. [$storage3, $storage4]
  56. ];
  57. }
  58. public function assertEqualsFailsProvider()
  59. {
  60. $object1 = new stdClass;
  61. $object2 = new stdClass;
  62. $storage1 = new SplObjectStorage;
  63. $storage2 = new SplObjectStorage;
  64. $storage2->attach($object1);
  65. $storage3 = new SplObjectStorage;
  66. $storage3->attach($object2);
  67. $storage3->attach($object1);
  68. return [
  69. [$storage1, $storage2],
  70. [$storage1, $storage3],
  71. [$storage2, $storage3],
  72. ];
  73. }
  74. public function testAcceptsSucceeds(): void
  75. {
  76. $this->assertTrue(
  77. $this->comparator->accepts(
  78. new SplObjectStorage,
  79. new SplObjectStorage
  80. )
  81. );
  82. }
  83. /**
  84. * @dataProvider acceptsFailsProvider
  85. */
  86. public function testAcceptsFails($expected, $actual): void
  87. {
  88. $this->assertFalse(
  89. $this->comparator->accepts($expected, $actual)
  90. );
  91. }
  92. /**
  93. * @dataProvider assertEqualsSucceedsProvider
  94. */
  95. public function testAssertEqualsSucceeds($expected, $actual): void
  96. {
  97. $exception = null;
  98. try {
  99. $this->comparator->assertEquals($expected, $actual);
  100. } catch (ComparisonFailure $exception) {
  101. }
  102. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  103. }
  104. /**
  105. * @dataProvider assertEqualsFailsProvider
  106. */
  107. public function testAssertEqualsFails($expected, $actual): void
  108. {
  109. $this->expectException(ComparisonFailure::class);
  110. $this->expectExceptionMessage('Failed asserting that two objects are equal.');
  111. $this->comparator->assertEquals($expected, $actual);
  112. }
  113. public function testAssertEqualsFails2(): void
  114. {
  115. $this->expectException(ComparisonFailure::class);
  116. $this->expectExceptionMessage('Failed asserting that two objects are equal.');
  117. $t = new SplObjectStorage();
  118. $t->attach(new \stdClass());
  119. $this->comparator->assertEquals($t, new \SplObjectStorage());
  120. }
  121. }