Skip to content

Commit

Permalink
Merge pull request #837 from TomHAnderson/feature/phpunit-upgrade
Browse files Browse the repository at this point in the history
Feature/phpunit upgrade
  • Loading branch information
TomHAnderson authored Aug 6, 2024
2 parents ab21665 + fe4f98d commit 8f721bc
Show file tree
Hide file tree
Showing 27 changed files with 230 additions and 33 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@
/phpcs.xml
/phpunit.xml
/vendor/
/.idea
/coverage/
/.phpunit.cache/
16 changes: 6 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
convertDeprecationsToExceptions="true"
bootstrap="vendor/autoload.php"
colors="true">
<coverage processUncoveredFiles="false">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.3/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<testsuites>
<testsuite name="DoctrineModule Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
30 changes: 21 additions & 9 deletions tests/Authentication/Adapter/ObjectRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Laminas\Authentication\Adapter\Exception\InvalidArgumentException;
use Laminas\Authentication\Adapter\Exception\RuntimeException;
use Laminas\Authentication\Adapter\Exception\UnexpectedValueException;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase as BaseTestCase;
use stdClass;

Expand All @@ -22,6 +23,7 @@
*/
class ObjectRepositoryTest extends BaseTestCase
{
#[Test]
public function testWillRejectInvalidIdentityProperty(): void
{
$this->expectException(
Expand All @@ -34,6 +36,7 @@ public function testWillRejectInvalidIdentityProperty(): void
new ObjectRepositoryAdapter(['identity_property' => '']);
}

#[Test]
public function testWillRejectInvalidCredentialProperty(): void
{
$this->expectException(
Expand All @@ -45,6 +48,7 @@ public function testWillRejectInvalidCredentialProperty(): void
new ObjectRepositoryAdapter(['credential_property' => '']);
}

#[Test]
public function testWillRequireIdentityValue(): void
{
$this->expectException(
Expand All @@ -63,6 +67,7 @@ public function testWillRequireIdentityValue(): void
$adapter->authenticate();
}

#[Test]
public function testWillRequireCredentialValue(): void
{
$this->expectException(
Expand All @@ -81,6 +86,7 @@ public function testWillRequireCredentialValue(): void
$adapter->authenticate();
}

#[Test]
public function testWillRejectInvalidCredentialCallable(): void
{
$this->expectException(
Expand All @@ -99,6 +105,7 @@ public function testWillRejectInvalidCredentialCallable(): void
$adapter->authenticate();
}

#[Test]
public function testAuthentication(): void
{
$entity = new IdentityObject();
Expand All @@ -110,13 +117,13 @@ public function testAuthentication(): void
->expects($this->exactly(2))
->method('findOneBy')
->with($this->equalTo(['username' => 'a username']))
->will($this->returnValue($entity));
->willReturn($entity);

$objectManager = $this->createMock(ObjectManager::class);
$objectManager->expects($this->exactly(2))
->method('getRepository')
->with($this->equalTo(IdentityObject::class))
->will($this->returnValue($objectRepository));
->willReturn($objectRepository);

$adapter = new ObjectRepositoryAdapter();
$adapter->setOptions([
Expand All @@ -138,13 +145,14 @@ public function testAuthentication(): void
$result->getIdentity(),
);

$method->will($this->returnValue(null));
$method->willReturn(null);

$result = $adapter->authenticate();

$this->assertFalse($result->isValid());
}

#[Test]
public function testAuthenticationWithPublicProperties(): void
{
$entity = new PublicPropertiesIdentityObject();
Expand All @@ -156,7 +164,7 @@ public function testAuthenticationWithPublicProperties(): void
->expects($this->exactly(2))
->method('findOneBy')
->with($this->equalTo(['username' => 'a username']))
->will($this->returnValue($entity));
->willReturn($entity);

$adapter = new ObjectRepositoryAdapter();
$adapter->setOptions([
Expand All @@ -173,13 +181,14 @@ public function testAuthenticationWithPublicProperties(): void

$this->assertTrue($result->isValid());

$method->will($this->returnValue(null));
$method->willReturn(null);

$result = $adapter->authenticate();

$this->assertFalse($result->isValid());
}

#[Test]
public function testWillRefuseToAuthenticateWithoutGettersOrPublicMethods(): void
{
$this->expectException(UnexpectedValueException::class);
Expand All @@ -189,7 +198,7 @@ public function testWillRefuseToAuthenticateWithoutGettersOrPublicMethods(): voi
->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['username' => 'a username']))
->will($this->returnValue(new stdClass()));
->willReturn(new stdClass());

$adapter = new ObjectRepositoryAdapter();
$adapter->setOptions([
Expand All @@ -203,6 +212,7 @@ public function testWillRefuseToAuthenticateWithoutGettersOrPublicMethods(): voi
$adapter->authenticate();
}

#[Test]
public function testCanValidateWithSpecialCrypt(): void
{
$hash = '$2y$07$usesomesillystringforsalt$';
Expand All @@ -216,7 +226,7 @@ public function testCanValidateWithSpecialCrypt(): void
->expects($this->exactly(2))
->method('findOneBy')
->with($this->equalTo(['username' => 'username']))
->will($this->returnValue($entity));
->willReturn($entity);

$adapter = new ObjectRepositoryAdapter();
$adapter->setOptions([
Expand All @@ -240,6 +250,7 @@ public function testCanValidateWithSpecialCrypt(): void
$this->assertFalse($result->isValid());
}

#[Test]
public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound(): void
{
$this->expectException(UnexpectedValueException::class);
Expand All @@ -249,7 +260,7 @@ public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound(): void
->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['username' => 'a username']))
->will($this->returnValue(new stdClass()));
->willReturn(new stdClass());

$adapter = new ObjectRepositoryAdapter();
$adapter->setOptions([
Expand All @@ -264,6 +275,7 @@ public function testWillRefuseToAuthenticateWhenInvalidInstanceIsFound(): void
$adapter->authenticate();
}

#[Test]
public function testWillNotCastAuthCredentialValue(): void
{
$objectRepository = $this->createMock(ObjectRepository::class);
Expand All @@ -283,7 +295,7 @@ public function testWillNotCastAuthCredentialValue(): void
->expects($this->once())
->method('findOneBy')
->with($this->equalTo(['username' => 'a username']))
->will($this->returnValue($entity));
->willReturn($entity);

$this->assertFalse($adapter->authenticate()->isValid());
}
Expand Down
6 changes: 4 additions & 2 deletions tests/Authentication/Storage/ObjectRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
use DoctrineModule\Authentication\Storage\ObjectRepository as ObjectRepositoryStorage;
use DoctrineModuleTest\Authentication\Adapter\TestAsset\IdentityObject;
use Laminas\Authentication\Storage\NonPersistent as NonPersistentStorage;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase as BaseTestCase;

/**
* Tests for the ObjectRepository based authentication adapter
*/
class ObjectRepositoryTest extends BaseTestCase
{
#[Test]
public function testCanRetrieveEntityFromObjectRepositoryStorage(): void
{
// Identifier is considered to be username here
Expand All @@ -27,13 +29,13 @@ public function testCanRetrieveEntityFromObjectRepositoryStorage(): void
$objectRepository->expects($this->exactly(1))
->method('find')
->with($this->equalTo('a username'))
->will($this->returnValue($entity));
->willReturn($entity);

$metadata = $this->createMock(ClassMetadata::class);
$metadata->expects($this->exactly(1))
->method('getIdentifierValues')
->with($this->equalTo($entity))
->will($this->returnValue($entity->getUsername()));
->willReturn($entity->getUsername());

$storage = new ObjectRepositoryStorage([
'objectRepository' => $objectRepository,
Expand Down
Loading

0 comments on commit 8f721bc

Please sign in to comment.