MockObjectComparatorTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. * @coversDefaultClass SebastianBergmann\Comparator\MockObjectComparator
  15. *
  16. * @uses SebastianBergmann\Comparator\Comparator
  17. * @uses SebastianBergmann\Comparator\Factory
  18. * @uses SebastianBergmann\Comparator\ComparisonFailure
  19. */
  20. class MockObjectComparatorTest extends 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->createMock(TestClass::class);
  31. $stdmock = $this->createMock(stdClass::class);
  32. return [
  33. [$testmock, $testmock],
  34. [$stdmock, $stdmock],
  35. [$stdmock, $testmock]
  36. ];
  37. }
  38. public function acceptsFailsProvider()
  39. {
  40. $stdmock = $this->createMock(stdClass::class);
  41. return [
  42. [$stdmock, null],
  43. [null, $stdmock],
  44. [null, null]
  45. ];
  46. }
  47. public function assertEqualsSucceedsProvider()
  48. {
  49. // cyclic dependencies
  50. $book1 = $this->getMockBuilder(Book::class)->setMethods(null)->getMock();
  51. $book1->author = $this->getMockBuilder(Author::class)->setMethods(null)->setConstructorArgs(['Terry Pratchett'])->getMock();
  52. $book1->author->books[] = $book1;
  53. $book2 = $this->getMockBuilder(Book::class)->setMethods(null)->getMock();
  54. $book2->author = $this->getMockBuilder(Author::class)->setMethods(null)->setConstructorArgs(['Terry Pratchett'])->getMock();
  55. $book2->author->books[] = $book2;
  56. $object1 = $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([4, 8, 15])->getMock();
  57. $object2 = $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([4, 8, 15])->getMock();
  58. return [
  59. [$object1, $object1],
  60. [$object1, $object2],
  61. [$book1, $book1],
  62. [$book1, $book2],
  63. [
  64. $this->getMockBuilder(Struct::class)->setMethods(null)->setConstructorArgs([2.3])->getMock(),
  65. $this->getMockBuilder(Struct::class)->setMethods(null)->setConstructorArgs([2.5])->getMock(),
  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->getMockBuilder(Book::class)->setMethods(null)->getMock();
  76. $book1->author = $this->getMockBuilder(Author::class)->setMethods(null)->setConstructorArgs(['Terry Pratchett'])->getMock();
  77. $book1->author->books[] = $book1;
  78. $book2 = $this->getMockBuilder(Book::class)->setMethods(null)->getMock();
  79. $book1->author = $this->getMockBuilder(Author::class)->setMethods(null)->setConstructorArgs(['Terry Pratch'])->getMock();
  80. $book2->author->books[] = $book2;
  81. $book3 = $this->getMockBuilder(Book::class)->setMethods(null)->getMock();
  82. $book3->author = 'Terry Pratchett';
  83. $book4 = $this->createMock(stdClass::class);
  84. $book4->author = 'Terry Pratchett';
  85. $object1 = $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([4, 8, 15])->getMock();
  86. $object2 = $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([16, 23, 42])->getMock();
  87. return [
  88. [
  89. $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([4, 8, 15])->getMock(),
  90. $this->getMockBuilder(SampleClass::class)->setMethods(null)->setConstructorArgs([16, 23, 42])->getMock(),
  91. $equalMessage
  92. ],
  93. [$object1, $object2, $equalMessage],
  94. [$book1, $book2, $equalMessage],
  95. [$book3, $book4, $typeMessage],
  96. [
  97. $this->getMockBuilder(Struct::class)->setMethods(null)->setConstructorArgs([2.3])->getMock(),
  98. $this->getMockBuilder(Struct::class)->setMethods(null)->setConstructorArgs([4.2])->getMock(),
  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. } catch (ComparisonFailure $exception) {
  134. }
  135. $this->assertNull($exception, 'Unexpected ComparisonFailure');
  136. }
  137. /**
  138. * @covers ::assertEquals
  139. * @dataProvider assertEqualsFailsProvider
  140. */
  141. public function testAssertEqualsFails($expected, $actual, $message, $delta = 0.0)
  142. {
  143. $this->expectException(ComparisonFailure::class);
  144. $this->expectExceptionMessage($message);
  145. $this->comparator->assertEquals($expected, $actual, $delta);
  146. }
  147. }