暫無描述

MockObjectComparatorTest.php 5.6KB

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