Skip to content

Commit

Permalink
Merge pull request #638 from TomHAnderson/hotfix/tests-feature-phpcs
Browse files Browse the repository at this point in the history
Hotfix/tests feature phpcs
  • Loading branch information
TomHAnderson authored Oct 28, 2024
2 parents a62a443 + 57f6ed6 commit dab04b8
Show file tree
Hide file tree
Showing 49 changed files with 935 additions and 1,071 deletions.
1 change: 1 addition & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

<!-- Directories to be checked -->
<file>src</file>
<file>tests</file>

<!-- Include full Doctrine Coding Standard -->
<rule ref="Doctrine"/>
Expand Down
9 changes: 9 additions & 0 deletions tests/Assets/InvalidDoctrineExtender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Assets;

class InvalidDoctrineExtender
{
}
19 changes: 19 additions & 0 deletions tests/Assets/MyDoctrineExtender.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Assets;

use Doctrine\Common\EventManager;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Configuration;
use LaravelDoctrine\ORM\DoctrineExtender;
use LaravelDoctrineTest\ORM\Feature\DoctrineManagerTest;

class MyDoctrineExtender implements DoctrineExtender
{
public function extend(Configuration $configuration, Connection $connection, EventManager $eventManager): void
{
(new DoctrineManagerTest())->assertExtendedCorrectly($configuration, $connection, $eventManager);
}
}
32 changes: 32 additions & 0 deletions tests/Assets/Testing/EntityStub.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Assets\Testing;

use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\InverseJoinColumn;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\JoinTable;
use Doctrine\ORM\Mapping\ManyToMany;

#[Entity]
class EntityStub
{
#[Id]
#[GeneratedValue]
#[Column(type: 'integer')]
public mixed $id;

#[Column(type: 'string')]
public mixed $name;

#[ManyToMany(targetEntity: 'EntityStub')]
#[JoinTable(name: 'stub_stubs')]
#[JoinColumn(name: 'owner_id', referencedColumnName: 'id')]
#[InverseJoinColumn(name: 'owned_id', referencedColumnName: 'id')]
public mixed $others;
}
88 changes: 36 additions & 52 deletions tests/Feature/Auth/DoctrineUserProviderTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Feature\Auth;

use Doctrine\ORM\EntityManagerInterface;
Expand All @@ -10,100 +12,84 @@
use LaravelDoctrineTest\ORM\Assets\Auth\AuthenticableWithNonEmptyConstructorMock;
use LaravelDoctrineTest\ORM\TestCase;
use Mockery as m;
use Mockery\Mock;

class DoctrineUserProviderTest extends TestCase
{
/**
* @var Mock
*/
protected $hasher;

/**
* @var Mock
*/
protected $em;

/**
* @var DoctrineUserProvider
*/
protected $provider;

/**
* @var DoctrineUserProvider
*/
protected $providerNonEmpty;

/**
* @var Mock
*/
protected $repo;
protected Hasher $hasher;

protected EntityManagerInterface $em;

protected DoctrineUserProvider $provider;

protected DoctrineUserProvider $providerNonEmpty;

protected EntityRepository $repo;

protected function setUp(): void
{
$this->hasher = m::mock(Hasher::class);
$this->em = m::mock(EntityManagerInterface::class);
$this->repo = m::mock(EntityRepository::class);

$this->provider = new DoctrineUserProvider(
$this->provider = new DoctrineUserProvider(
$this->hasher,
$this->em,
AuthenticableMock::class
AuthenticableMock::class,
);
$this->providerNonEmpty = new DoctrineUserProvider(
$this->hasher,
$this->em,
AuthenticableWithNonEmptyConstructorMock::class
AuthenticableWithNonEmptyConstructorMock::class,
);

parent::setUp();
}

public function test_can_retrieve_by_id()
public function testCanRetrieveById(): void
{
$this->mockGetRepository();

$user = new AuthenticableMock;
$user = new AuthenticableMock();
$this->repo->shouldReceive('find')
->once()->with(1)
->andReturn($user);

$this->assertEquals($user, $this->provider->retrieveById(1));
}

public function test_can_retrieve_by_token()
public function testCanRetrieveByToken(): void
{
$this->mockGetRepository();

$user = new AuthenticableMock;
$user = new AuthenticableMock();
$this->repo->shouldReceive('findOneBy')
->with([
'id' => 1,
'rememberToken' => 'myToken'
'rememberToken' => 'myToken',
])
->once()->andReturn($user);

$this->assertEquals($user, $this->provider->retrieveByToken(1, 'myToken'));
}

public function test_can_retrieve_by_token_with_non_empty_constructor()
public function testCanRetrieveByTokenWithNonEmptyConstructor(): void
{
$this->mockGetRepository(AuthenticableWithNonEmptyConstructorMock::class);

$user = new AuthenticableWithNonEmptyConstructorMock(['myPassword']);
$this->repo->shouldReceive('findOneBy')
->with([
'id' => 1,
'rememberToken' => 'myToken'
'rememberToken' => 'myToken',
])
->once()->andReturn($user);

$this->assertEquals($user, $this->providerNonEmpty->retrieveByToken(1, 'myToken'));
}

public function test_can_update_remember_token()
public function testCanUpdateRememberToken(): void
{
$user = new AuthenticableMock;
$user = new AuthenticableMock();

$this->em->shouldReceive('persist')->once()->with($user);
$this->em->shouldReceive('flush')->once()->withNoArgs();
Expand All @@ -113,40 +99,38 @@ public function test_can_update_remember_token()
$this->assertEquals('newToken', $user->getRememberToken());
}

public function test_can_retrieve_by_credentials()
public function testCanRetrieveByCredentials(): void
{
$this->mockGetRepository();

$user = new AuthenticableMock;
$user = new AuthenticableMock();
$this->repo->shouldReceive('findOneBy')
->with([
'email' => 'email',
])
->with(['email' => 'email'])
->once()->andReturn($user);

$this->assertEquals($user, $this->provider->retrieveByCredentials([
'email' => 'email',
'password' => 'password'
'password' => 'password',
]));
}

public function test_can_validate_credentials()
public function testCanValidateCredentials(): void
{
$user = new AuthenticableMock;
$user = new AuthenticableMock();

$this->hasher->shouldReceive('check')->once()
->with('myPassword', 'myPassword')
->andReturn(true);

$this->assertTrue($this->provider->validateCredentials(
$user,
['password' => 'myPassword']
['password' => 'myPassword'],
));
}

public function test_rehash_password_if_required_rehash()
public function testRehashPasswordIfRequriredRehash(): void
{
$user = new AuthenticableMock;
$user = new AuthenticableMock();

$this->hasher->shouldReceive('needsRehash')->once()->andReturn(true);
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
Expand All @@ -157,9 +141,9 @@ public function test_rehash_password_if_required_rehash()
$this->assertEquals('hashedPassword', $user->getPassword());
}

public function test_rehash_password_if_required_rehash_force()
public function testRehashPasswordIfRequiredRehashForce(): void
{
$user = new AuthenticableMock;
$user = new AuthenticableMock();

$this->hasher->shouldReceive('needsRehash')->once()->andReturn(false);
$this->hasher->shouldReceive('make')->once()->andReturn('hashedPassword');
Expand All @@ -170,7 +154,7 @@ public function test_rehash_password_if_required_rehash_force()
$this->assertEquals('hashedPassword', $user->getPassword());
}

public function test_rehash_password_if_required_rehash_norehash_needed()
public function testRehashPasswordIfRequiredRehashNorehashNeeded(): void
{
$user = new AuthenticableMock();
$user->setPassword('originalPassword');
Expand All @@ -181,7 +165,7 @@ public function test_rehash_password_if_required_rehash_norehash_needed()
$this->assertEquals('originalPassword', $user->getPassword());
}

protected function mockGetRepository($class = AuthenticableMock::class)
protected function mockGetRepository(string $class = AuthenticableMock::class): void
{
$this->em->shouldReceive('getRepository')
->with($class)
Expand Down
Loading

0 comments on commit dab04b8

Please sign in to comment.