MockObjectComparatorTest.php 5.9 KB

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