No Description

ObjectComparatorTest.php 4.3KB

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