Skip to content

Commit

Permalink
Fix PHPUnit deprecations
Browse files Browse the repository at this point in the history
  • Loading branch information
malarzm committed Aug 8, 2022
1 parent 9012b6c commit 457b513
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
14 changes: 14 additions & 0 deletions tests/Doctrine/Tests/Common/Proxy/InvokationSpy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Doctrine\Tests\Common\Proxy;

class InvokationSpy
{
public $invokations = [];

public function __invoke($proxy, $method, $parameters)
{
$this->invokations[] = [$proxy, $method, $parameters];
}
}
29 changes: 16 additions & 13 deletions tests/Doctrine/Tests/Common/Proxy/ProxyLogicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,14 @@ public function testNoticeWhenReadingNonExistentPublicProperties()
$this->configureInitializerMock(0);

$class = get_class($this->lazyObject);
$this->expectException(Notice::class);
$this->expectExceptionMessage('Undefined property: ' . $class . '::$non_existing_property');
// @todo drop condition when PHPUnit 9.x becomes lowest
if (method_exists($this, 'expectNotice')) {
$this->expectNotice();
$this->expectNoticeMessage('Undefined property: ' . $class . '::$non_existing_property');
} else {
$this->expectException(Notice::class);
$this->expectExceptionMessage('Undefined property: ' . $class . '::$non_existing_property');
}

$this->lazyObject->non_existing_property;
}
Expand Down Expand Up @@ -610,8 +616,7 @@ public function testCallingVariadicMethodCausesLazyLoading()
require_once $proxyGenerator->getProxyFileName($metadata->getName());
}

/** @var callable&MockObject $invocationMock */
$invocationMock = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock();
$invocationMock = new InvokationSpy();

/** @var VariadicTypeHintClass $lazyObject */
$lazyObject = new $proxyClassName(
Expand All @@ -622,19 +627,17 @@ static function () {
}
);

$invocationMock
->expects($this->at(0))
->method('__invoke')
->with($lazyObject, 'addType', [['type1', 'type2']]);
$invocationMock
->expects($this->at(1))
->method('__invoke')
->with($lazyObject, 'addTypeWithMultipleParameters', ['foo', 'bar', ['baz1', 'baz2']]);

$lazyObject->addType('type1', 'type2');
self::assertCount(1, $invocationMock->invokations);
self::assertSame([$lazyObject, 'addType', [['type1', 'type2']]], $invocationMock->invokations[0]);
self::assertSame(['type1', 'type2'], $lazyObject->types);

$lazyObject->addTypeWithMultipleParameters('foo', 'bar', 'baz1', 'baz2');
self::assertCount(2, $invocationMock->invokations);
self::assertSame(
[$lazyObject, 'addTypeWithMultipleParameters', ['foo', 'bar', ['baz1', 'baz2']]],
$invocationMock->invokations[1]
);
self::assertSame('foo', $lazyObject->foo);
self::assertSame('bar', $lazyObject->bar);
self::assertSame(['baz1', 'baz2'], $lazyObject->baz);
Expand Down
31 changes: 17 additions & 14 deletions tests/Doctrine/Tests/Common/Proxy/ProxyLogicTypedPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,15 @@ public function testNoticeWhenReadingNonExistentPublicProperties()
$this->configureInitializerMock(0);

$class = get_class($this->lazyObject);
$this->expectException(Notice::class);
$this->expectExceptionMessage('Undefined property: ' . $class . '::$non_existing_property');

// @todo drop condition when PHPUnit 9.x becomes lowest
if (method_exists($this, 'expectNotice')) {
$this->expectNotice();
$this->expectNoticeMessage('Undefined property: ' . $class . '::$non_existing_property');
} else {
$this->expectException(Notice::class);
$this->expectExceptionMessage('Undefined property: ' . $class . '::$non_existing_property');
}

$this->lazyObject->non_existing_property;
}

Expand Down Expand Up @@ -613,8 +619,7 @@ public function testCallingVariadicMethodCausesLazyLoading()
require_once $proxyGenerator->getProxyFileName($metadata->getName());
}

/** @var callable&MockObject $invocationMock */
$invocationMock = $this->getMockBuilder(stdClass::class)->setMethods(['__invoke'])->getMock();
$invocationMock = new InvokationSpy();

/** @var VariadicTypeHintClass $lazyObject */
$lazyObject = new $proxyClassName(
Expand All @@ -625,19 +630,17 @@ static function () {
}
);

$invocationMock
->expects($this->at(0))
->method('__invoke')
->with($lazyObject, 'addType', [['type1', 'type2']]);
$invocationMock
->expects($this->at(1))
->method('__invoke')
->with($lazyObject, 'addTypeWithMultipleParameters', ['foo', 'bar', ['baz1', 'baz2']]);

$lazyObject->addType('type1', 'type2');
self::assertCount(1, $invocationMock->invokations);
self::assertSame([$lazyObject, 'addType', [['type1', 'type2']]], $invocationMock->invokations[0]);
self::assertSame(['type1', 'type2'], $lazyObject->types);

$lazyObject->addTypeWithMultipleParameters('foo', 'bar', 'baz1', 'baz2');
self::assertCount(2, $invocationMock->invokations);
self::assertSame(
[$lazyObject, 'addTypeWithMultipleParameters', ['foo', 'bar', ['baz1', 'baz2']]],
$invocationMock->invokations[1]
);
self::assertSame('foo', $lazyObject->foo);
self::assertSame('bar', $lazyObject->bar);
self::assertSame(['baz1', 'baz2'], $lazyObject->baz);
Expand Down

0 comments on commit 457b513

Please sign in to comment.