SplObjectStorageComparatorTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * @coversDefaultClass SebastianBergmann\Comparator\SplObjectStorageComparator
  16. *
  17. * @uses SebastianBergmann\Comparator\Comparator
  18. * @uses SebastianBergmann\Comparator\Factory
  19. * @uses SebastianBergmann\Comparator\ComparisonFailure
  20. */
  21. class SplObjectStorageComparatorTest extends TestCase
  22. {
  23. private $comparator;
  24. protected function setUp()
  25. {
  26. $this->comparator = new SplObjectStorageComparator;
  27. }
  28. public function acceptsFailsProvider()
  29. {
  30. return [
  31. [new SplObjectStorage, new stdClass],
  32. [new stdClass, new SplObjectStorage],
  33. [new stdClass, new stdClass]
  34. ];
  35. }
  36. public function assertEqualsSucceedsProvider()
  37. {
  38. $object1 = new stdClass();
  39. $object2 = new stdClass();
  40. $storage1 = new SplObjectStorage();
  41. $storage2 = new SplObjectStorage();
  42. $storage3 = new SplObjectStorage();
  43. $storage3->attach($object1);
  44. $storage3->attach($object2);
  45. $storage4 = new SplObjectStorage();
  46. $storage4->attach($object2);
  47. $storage4->attach($object1);
  48. return [
  49. [$storage1, $storage1],
  50. [$storage1, $storage2],
  51. [$storage3, $storage3],
  52. [$storage3, $storage4]
  53. ];
  54. }
  55. public function assertEqualsFailsProvider()
  56. {
  57. $object1 = new stdClass;
  58. $object2 = new stdClass;
  59. $storage1 = new SplObjectStorage;
  60. $storage2 = new SplObjectStorage;
  61. $storage2->attach($object1);
  62. $storage3 = new SplObjectStorage;
  63. $storage3->attach($object2);
  64. $storage3->attach($object1);
  65. return [
  66. [$storage1, $storage2],
  67. [$storage1, $storage3],
  68. [$storage2, $storage3],
  69. ];
  70. }
  71. /**
  72. * @covers ::accepts
  73. */
  74. public function testAcceptsSucceeds()
  75. {
  76. $this->assertTrue(
  77. $this->comparator->accepts(
  78. new SplObjectStorage,
  79. new SplObjectStorage
  80. )
  81. );
  82. }
  83. /**
  84. * @covers ::accepts
  85. * @dataProvider acceptsFailsProvider
  86. */
  87. public function testAcceptsFails($expected, $actual)
  88. {
  89. $this->assertFalse(
  90. $this->comparator->accepts($expected, $actual)
  91. );
  92. }
  93. /**
  94. * @covers ::assertEquals
  95. * @dataProvider assertEqualsSucceedsProvider
  96. */
  97. public function testAssertEqualsSucceeds($expected, $actual)
  98. {
  99. $exception = null;
  100. try {
  101. $this->comparator->assertEquals($expected, $actual);
  102. } catch (ComparisonFailure $exception) {
  103. }
  104. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  105. }
  106. /**
  107. * @covers ::assertEquals
  108. * @dataProvider assertEqualsFailsProvider
  109. */
  110. public function testAssertEqualsFails($expected, $actual)
  111. {
  112. $this->expectException(ComparisonFailure::class);
  113. $this->expectExceptionMessage('Failed asserting that two objects are equal.');
  114. $this->comparator->assertEquals($expected, $actual);
  115. }
  116. public function testAssertEqualsFails2()
  117. {
  118. $this->expectException(ComparisonFailure::class);
  119. $this->expectExceptionMessage('Failed asserting that two objects are equal.');
  120. $t = new SplObjectStorage();
  121. $t->attach(new \stdClass());
  122. $this->comparator->assertEquals($t, new \SplObjectStorage());
  123. }
  124. }