1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
<?php declare(strict_types=1);
/*
* This file is part of sebastian/comparator.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace SebastianBergmann\Comparator;
use function assert;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\MockObject\Generator\Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use stdClass;
#[CoversClass(MockObjectComparator::class)]
#[UsesClass(Comparator::class)]
#[UsesClass(ComparisonFailure::class)]
#[UsesClass(Factory::class)]
final class MockObjectComparatorTest extends TestCase
{
private MockObjectComparator $comparator;
/**
* @return non-empty-list<array{0: MockObject, 1: MockObject}>
*/
public static function acceptsSucceedsProvider(): array
{
$testmock = self::createMockForComparatorTest(TestClass::class);
$stdmock = self::createMockForComparatorTest(stdClass::class);
return [
[$testmock, $testmock],
[$stdmock, $stdmock],
[$stdmock, $testmock],
];
}
/**
* @return non-empty-list<array{0: ?MockObject, 1: ?MockObject}>
*/
public static function acceptsFailsProvider(): array
{
$stdmock = self::createMockForComparatorTest(stdClass::class);
return [
[$stdmock, null],
[null, $stdmock],
[null, null],
];
}
/**
* @return non-empty-list<array{0: MockObject, 1: MockObject, 2?: float}>
*/
public static function assertEqualsSucceedsProvider(): array
{
// cyclic dependencies
$book1 = self::createMockForComparatorTest(Book::class);
$book1->author = self::createMockForComparatorTest(Author::class, ['Terry Pratchett']);
$book1->author->books[] = $book1;
$book2 = self::createMockForComparatorTest(Book::class);
$book2->author = self::createMockForComparatorTest(Author::class, ['Terry Pratchett']);
$book2->author->books[] = $book2;
$object1 = self::createMockForComparatorTest(SampleClass::class, [4, 8, 15]);
$object2 = self::createMockForComparatorTest(SampleClass::class, [4, 8, 15]);
return [
[$object1, $object1],
[$object1, $object2],
[$book1, $book1],
[$book1, $book2],
[
self::createMockForComparatorTest(Struct::class, [2.3]),
self::createMockForComparatorTest(Struct::class, [2.5]),
0.5,
],
];
}
/**
* @return non-empty-list<array{0: MockObject, 1: MockObject, 2?: non-empty-string, 3?: float}>
*/
public static function assertEqualsFailsProvider(): array
{
$typeMessage = 'is not instance of expected class';
$equalMessage = 'Failed asserting that two objects are equal.';
// cyclic dependencies
$book1 = self::createMockForComparatorTest(Book::class);
$book1->author = self::createMockForComparatorTest(Author::class, ['Terry Pratchett']);
$book1->author->books[] = $book1;
$book2 = self::createMockForComparatorTest(Book::class);
$book2->author = self::createMockForComparatorTest(Author::class, ['Terry Pratch']);
$book2->author->books[] = $book2;
$book3 = self::createMockForComparatorTest(Book::class);
$book3->author = new Author('Terry Pratchett');
$book4 = self::createMockForComparatorTest(stdClass::class);
$book4->author = new Author('Terry Pratchett');
$object1 = self::createMockForComparatorTest(SampleClass::class, [4, 8, 15]);
$object2 = self::createMockForComparatorTest(SampleClass::class, [16, 23, 42]);
return [
[
self::createMockForComparatorTest(SampleClass::class, [4, 8, 15]),
self::createMockForComparatorTest(SampleClass::class, [16, 23, 42]),
$equalMessage,
],
[$object1, $object2, $equalMessage],
[$book1, $book2, $equalMessage],
[$book3, $book4, $typeMessage],
[
self::createMockForComparatorTest(Struct::class, [2.3]),
self::createMockForComparatorTest(Struct::class, [4.2]),
$equalMessage,
0.5,
],
];
}
protected function setUp(): void
{
$this->comparator = new MockObjectComparator;
$this->comparator->setFactory(new Factory);
}
#[DataProvider('acceptsSucceedsProvider')]
public function testAcceptsSucceeds(MockObject $expected, MockObject $actual): void
{
$this->assertTrue(
$this->comparator->accepts($expected, $actual),
);
}
#[DataProvider('acceptsFailsProvider')]
public function testAcceptsFails(?MockObject $expected, ?MockObject $actual): void
{
$this->assertFalse(
$this->comparator->accepts($expected, $actual),
);
}
#[DataProvider('assertEqualsSucceedsProvider')]
public function testAssertEqualsSucceeds(MockObject $expected, MockObject $actual, float $delta = 0.0): void
{
$exception = null;
try {
$this->comparator->assertEquals($expected, $actual, $delta);
} catch (ComparisonFailure $exception) {
}
$this->assertNull($exception, 'Unexpected ComparisonFailure');
}
#[DataProvider('assertEqualsFailsProvider')]
public function testAssertEqualsFails(MockObject $expected, MockObject $actual, string $message, float $delta = 0.0): void
{
$this->expectException(ComparisonFailure::class);
$this->expectExceptionMessage($message);
$this->comparator->assertEquals($expected, $actual, $delta);
}
/**
* @template RealInstanceType of object
*
* @param class-string<RealInstanceType> $type
* @param list<mixed> $constructorArguments
*
* @return MockObject&RealInstanceType
*/
private static function createMockForComparatorTest(string $type, array $constructorArguments = []): MockObject
{
$generator = new Generator;
$mockObject = $generator->testDouble($type, true, true, null, $constructorArguments);
assert($mockObject instanceof MockObject);
assert($mockObject instanceof $type);
return $mockObject;
}
}
|