MockObjectComparatorTest.php 5.8 KB

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