Skip to content

Commit

Permalink
Merge pull request #672 from cakephp/3.next-phpunit11
Browse files Browse the repository at this point in the history
add phpunit 11 support
  • Loading branch information
ADmad authored Aug 10, 2024
2 parents 20c06dd + 76b9187 commit ece8c84
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 58 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
"psr/http-server-middleware": "^1.0"
},
"require-dev": {
"cakephp/cakephp": "^5.0",
"cakephp/cakephp": "dev-5.next as 5.1.0",
"cakephp/cakephp-codesniffer": "^5.0",
"firebase/php-jwt": "^6.2",
"phpunit/phpunit": "^10.1.0"
"phpunit/phpunit": "^10.5.5 || ^11.1.3"
},
"suggest": {
"ext-ldap": "Make sure this php extension is installed and enabled on your system if you want to use the built-in LDAP adapter for \"LdapIdentifier\".",
Expand Down
23 changes: 18 additions & 5 deletions tests/TestCase/Authenticator/AbstractAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
namespace Authentication\Test\TestCase\Authenticator;

use Authentication\Authenticator\AbstractAuthenticator;
use Authentication\Authenticator\Result;
use Authentication\Authenticator\ResultInterface;
use Authentication\Identifier\IdentifierInterface;
use Authentication\Test\TestCase\AuthenticationTestCase as TestCase;

Expand All @@ -30,7 +32,12 @@ class AbstractAuthenticatorTest extends TestCase
public function testGetIdentifier()
{
$identifier = $this->createMock(IdentifierInterface::class);
$authenticator = $this->getMockForAbstractClass(AbstractAuthenticator::class, [$identifier]);
$authenticator = new class ($identifier) extends AbstractAuthenticator {
public function authenticate($request): Result
{
return new Result([], ResultInterface::SUCCESS);
}
};

$this->assertSame($identifier, $authenticator->getIdentifier());
}
Expand All @@ -42,11 +49,17 @@ public function testGetIdentifier()
*/
public function testSetIdentifier()
{
$authenticator = $this->getMockForAbstractClass(AbstractAuthenticator::class, [], '', false);

$identifier = $this->createMock(IdentifierInterface::class);
$authenticator->setIdentifier($identifier);
$authenticator = new class ($identifier) extends AbstractAuthenticator {
public function authenticate($request): Result
{
return new Result([], ResultInterface::SUCCESS);
}
};

$this->assertSame($identifier, $authenticator->getIdentifier());
$otherIdentifier = $this->createMock(IdentifierInterface::class);
$authenticator->setIdentifier($otherIdentifier);

$this->assertSame($otherIdentifier, $authenticator->getIdentifier());
}
}
2 changes: 1 addition & 1 deletion tests/TestCase/Authenticator/JwtAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public function testAuthenticateInvalidPayloadEmpty()

$authenticator->expects($this->once())
->method('getPayLoad')
->will($this->returnValue(new stdClass()));
->willReturn(new stdClass());

$result = $authenticator->authenticate($request);
$this->assertInstanceOf(Result::class, $result);
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/Authenticator/SessionAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ public function testAuthenticateSuccess()
$this->sessionMock->expects($this->once())
->method('read')
->with('Auth')
->will($this->returnValue([
->willReturn([
'username' => 'mariano',
'password' => 'password',
]));
]);

$request = $request->withAttribute('session', $this->sessionMock);

Expand All @@ -106,7 +106,7 @@ public function testAuthenticateFailure()
$this->sessionMock->expects($this->once())
->method('read')
->with('Auth')
->will($this->returnValue(null));
->willReturn(null);

$request = $request->withAttribute('session', $this->sessionMock);

Expand All @@ -129,10 +129,10 @@ public function testVerifyByDatabaseSuccess()
$this->sessionMock->expects($this->once())
->method('read')
->with('Auth')
->will($this->returnValue([
->willReturn([
'username' => 'mariano',
'password' => 'h45h',
]));
]);

$request = $request->withAttribute('session', $this->sessionMock);

Expand All @@ -157,10 +157,10 @@ public function testVerifyByDatabaseFailure()
$this->sessionMock->expects($this->once())
->method('read')
->with('Auth')
->will($this->returnValue([
->willReturn([
'username' => 'does-not',
'password' => 'exist',
]));
]);

$request = $request->withAttribute('session', $this->sessionMock);

Expand Down
88 changes: 47 additions & 41 deletions tests/TestCase/Identifier/Resolver/ResolverAwareTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,31 @@ class ResolverAwareTraitTest extends TestCase
{
public function testBuildResolverFromClassName()
{
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn('Test');
public function getConfig()
{
return 'Test';
}
};

$resolver = $object->getResolver();
$this->assertInstanceOf(TestResolver::class, $resolver);
}

public function testBuildResolverFromArray()
{
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn([
'className' => 'Test',
]);
public function getConfig()
{
return [
'className' => 'Test',
];
}
};

$resolver = $object->getResolver();
$this->assertInstanceOf(TestResolver::class, $resolver);
Expand All @@ -57,13 +59,14 @@ public function testBuildResolverInvalid()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Resolver must implement `Authentication\Identifier\Resolver\ResolverInterface`.');
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn('Invalid');
public function getConfig()
{
return 'Invalid';
}
};

$object->getResolver();
}
Expand All @@ -72,13 +75,14 @@ public function testBuildResolverMissing()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Resolver class `Missing` does not exist.');
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn('Missing');
public function getConfig()
{
return 'Missing';
}
};

$object->getResolver();
}
Expand All @@ -87,13 +91,14 @@ public function testBuildResolverMissingClassNameOption()
{
$this->expectException('InvalidArgumentException');
$this->expectExceptionMessage('Option `className` is not present.');
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn([]);
public function getConfig()
{
return [];
}
};

$object->getResolver();
}
Expand All @@ -102,22 +107,23 @@ public function testGetResolverNotSet()
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('Resolver has not been set.');
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;

$object->expects($this->once())
->method('getConfig')
->willReturn(null);
public function getConfig()
{
return null;
}
};

$object->getResolver();
}

public function testSetResolver()
{
$object = $this->getMockBuilder(ResolverAwareTrait::class)
->addMethods(['getConfig'])
->getMockForTrait();
$object = new class {
use ResolverAwareTrait;
};

$resolver = $this->createMock(ResolverInterface::class);

Expand Down
8 changes: 6 additions & 2 deletions tests/TestCase/PasswordHasher/PasswordHasherTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class PasswordHasherTraitTest extends TestCase
*/
public function testGetPasswordHasher()
{
$object = $this->getMockForTrait(PasswordHasherTrait::class);
$object = new class {
use PasswordHasherTrait;
};

$defaultHasher = $object->getPasswordHasher();
$this->assertInstanceOf(DefaultPasswordHasher::class, $defaultHasher);
Expand All @@ -46,7 +48,9 @@ public function testGetPasswordHasher()
public function testSetPasswordHasher()
{
$hasher = $this->createMock(PasswordHasherInterface::class);
$object = $this->getMockForTrait(PasswordHasherTrait::class);
$object = new class {
use PasswordHasherTrait;
};

$object->setPasswordHasher($hasher);
$passwordHasher = $object->getPasswordHasher();
Expand Down

0 comments on commit ece8c84

Please sign in to comment.