MockObjectComparatorTest.php 5.4 KB

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