菜谱项目

ObjectComparatorTest.php 4.0KB

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