Нема описа

ObjectComparatorTest.php 4.2KB

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