Skip to content

Commit

Permalink
phpcs tests/Feature/Auth
Browse files Browse the repository at this point in the history
  • Loading branch information
TomHAnderson committed Oct 28, 2024
1 parent 806b106 commit c111bba
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 96 deletions.
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
72 changes: 28 additions & 44 deletions tests/Feature/Auth/Passwords/DoctrineTokenRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
<?php

declare(strict_types=1);

namespace LaravelDoctrineTest\ORM\Feature\Auth\Passwords;

use Carbon\Carbon;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder;
use Doctrine\DBAL\Result;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Illuminate\Contracts\Hashing\Hasher;
use LaravelDoctrine\ORM\Auth\Passwords\DoctrineTokenRepository;
Expand All @@ -15,35 +18,17 @@

class DoctrineTokenRepositoryTest extends TestCase
{
/**
* @var Mock
*/
protected $schema;

/**
* @var DoctrineTokenRepository
*/
protected $repository;

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

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

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

/**
* @var Mock
*/
protected $builder;
protected AbstractSchemaManager $schema;

protected DoctrineTokenRepository $repository;

protected Mock $registry;

protected Connection $connection;

protected Hasher $hasher;

protected QueryBuilder $builder;

protected function setUp(): void
{
Expand All @@ -64,13 +49,13 @@ protected function setUp(): void
$this->hasher,
'password_resets',
'hashkey',
60
60,
);

parent::setUp();
}

public function test_can_create_a_token()
public function testCanCreateAToken(): void
{
$this->connection->shouldReceive('createQueryBuilder')
->twice()
Expand Down Expand Up @@ -108,17 +93,17 @@ public function test_can_create_a_token()
->with([
'email' => ':email',
'token' => ':token',
'created_at' => ':date'
'created_at' => ':date',
])
->once()->andReturnSelf();

$this->builder->shouldReceive('setParameters')
->once()->andReturnSelf();

$this->assertNotNull($this->repository->create(new UserMock));
$this->assertNotNull($this->repository->create(new UserMock()));
}

public function test_can_check_if_exists()
public function testCanCheckIfExists(): void
{
$this->connection->shouldReceive('createQueryBuilder')
->once()
Expand Down Expand Up @@ -154,7 +139,7 @@ public function test_can_check_if_exists()
->with('email', 'user@mockery.mock')
->andReturnSelf();

$result = m::mock(\Doctrine\DBAL\Result::class);
$result = m::mock(Result::class);

$this->builder->shouldReceive('executeQuery')
->once()
Expand All @@ -165,13 +150,13 @@ public function test_can_check_if_exists()
->andReturn([
'email' => 'user@mockery.mock',
'token' => 'token',
'created_at' => Carbon::now()
'created_at' => Carbon::now(),
]);

$this->assertTrue($this->repository->exists(new UserMock, 'token'));
$this->assertTrue($this->repository->exists(new UserMock(), 'token'));
}

public function test_can_check_if_recently_created_token()
public function testCanCheckIfRecentlyCreatedToken(): void
{
$this->connection->shouldReceive('createQueryBuilder')
->once()
Expand All @@ -197,7 +182,7 @@ public function test_can_check_if_recently_created_token()
->with('email', 'user@mockery.mock')
->andReturnSelf();

$result = m::mock(\Doctrine\DBAL\Result::class);
$result = m::mock(Result::class);

$this->builder->shouldReceive('executeQuery')
->once()
Expand All @@ -208,13 +193,13 @@ public function test_can_check_if_recently_created_token()
->andReturn([
'email' => 'user@mockery.mock',
'token' => 'token',
'created_at' => Carbon::now()
'created_at' => Carbon::now(),
]);

$this->assertTrue($this->repository->recentlyCreatedToken(new UserMock));
$this->assertTrue($this->repository->recentlyCreatedToken(new UserMock()));
}

public function test_can_delete()
public function testCanDelete(): void
{
$this->connection->shouldReceive('createQueryBuilder')
->once()
Expand All @@ -235,7 +220,6 @@ public function test_can_delete()
->with('email', 'user@mockery.mock')
->andReturnSelf();


$this->builder->shouldReceive('executeStatement')
->once()
->andReturn(true);
Expand All @@ -245,7 +229,7 @@ public function test_can_delete()
$this->assertTrue(true);
}

public function test_can_delete_expired()
public function testCanDeleteExpired(): void
{
$this->connection->shouldReceive('createQueryBuilder')
->once()
Expand Down

0 comments on commit c111bba

Please sign in to comment.