ObjectComparatorTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 stdClass;
  13. /**
  14. * @coversDefaultClass SebastianBergmann\Comparator\ObjectComparator
  15. *
  16. * @uses SebastianBergmann\Comparator\Comparator
  17. * @uses SebastianBergmann\Comparator\Factory
  18. * @uses SebastianBergmann\Comparator\ComparisonFailure
  19. */
  20. class ObjectComparatorTest extends TestCase
  21. {
  22. private $comparator;
  23. protected function setUp()
  24. {
  25. $this->comparator = new ObjectComparator;
  26. $this->comparator->setFactory(new Factory);
  27. }
  28. public function acceptsSucceedsProvider()
  29. {
  30. return [
  31. [new TestClass, new TestClass],
  32. [new stdClass, new stdClass],
  33. [new stdClass, new TestClass]
  34. ];
  35. }
  36. public function acceptsFailsProvider()
  37. {
  38. return [
  39. [new stdClass, null],
  40. [null, new stdClass],
  41. [null, null]
  42. ];
  43. }
  44. public function assertEqualsSucceedsProvider()
  45. {
  46. // cyclic dependencies
  47. $book1 = new Book;
  48. $book1->author = new Author('Terry Pratchett');
  49. $book1->author->books[] = $book1;
  50. $book2 = new Book;
  51. $book2->author = new Author('Terry Pratchett');
  52. $book2->author->books[] = $book2;
  53. $object1 = new SampleClass(4, 8, 15);
  54. $object2 = new SampleClass(4, 8, 15);
  55. return [
  56. [$object1, $object1],
  57. [$object1, $object2],
  58. [$book1, $book1],
  59. [$book1, $book2],
  60. [new Struct(2.3), new Struct(2.5), 0.5]
  61. ];
  62. }
  63. public function assertEqualsFailsProvider()
  64. {
  65. $typeMessage = 'is not instance of expected class';
  66. $equalMessage = 'Failed asserting that two objects are equal.';
  67. // cyclic dependencies
  68. $book1 = new Book;
  69. $book1->author = new Author('Terry Pratchett');
  70. $book1->author->books[] = $book1;
  71. $book2 = new Book;
  72. $book2->author = new Author('Terry Pratch');
  73. $book2->author->books[] = $book2;
  74. $book3 = new Book;
  75. $book3->author = 'Terry Pratchett';
  76. $book4 = new stdClass;
  77. $book4->author = 'Terry Pratchett';
  78. $object1 = new SampleClass(4, 8, 15);
  79. $object2 = new SampleClass(16, 23, 42);
  80. return [
  81. [new SampleClass(4, 8, 15), new SampleClass(16, 23, 42), $equalMessage],
  82. [$object1, $object2, $equalMessage],
  83. [$book1, $book2, $equalMessage],
  84. [$book3, $book4, $typeMessage],
  85. [new Struct(2.3), new Struct(4.2), $equalMessage, 0.5]
  86. ];
  87. }
  88. /**
  89. * @covers ::accepts
  90. * @dataProvider acceptsSucceedsProvider
  91. */
  92. public function testAcceptsSucceeds($expected, $actual)
  93. {
  94. $this->assertTrue(
  95. $this->comparator->accepts($expected, $actual)
  96. );
  97. }
  98. /**
  99. * @covers ::accepts
  100. * @dataProvider acceptsFailsProvider
  101. */
  102. public function testAcceptsFails($expected, $actual)
  103. {
  104. $this->assertFalse(
  105. $this->comparator->accepts($expected, $actual)
  106. );
  107. }
  108. /**
  109. * @covers ::assertEquals
  110. * @dataProvider assertEqualsSucceedsProvider
  111. */
  112. public function testAssertEqualsSucceeds($expected, $actual, $delta = 0.0)
  113. {
  114. $exception = null;
  115. try {
  116. $this->comparator->assertEquals($expected, $actual, $delta);
  117. } catch (ComparisonFailure $exception) {
  118. }
  119. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  120. }
  121. /**
  122. * @covers ::assertEquals
  123. * @dataProvider assertEqualsFailsProvider
  124. */
  125. public function testAssertEqualsFails($expected, $actual, $message, $delta = 0.0)
  126. {
  127. $this->expectException(ComparisonFailure::class);
  128. $this->expectExceptionMessage($message);
  129. $this->comparator->assertEquals($expected, $actual, $delta);
  130. }
  131. }