Skip to content

Commit ee7bcaa

Browse files
authored
Merge pull request #65 from jrushlow/refactor/test-suite
minor refactoring of the tests suite
2 parents 2a82449 + 65d0e82 commit ee7bcaa

12 files changed

+91
-100
lines changed

phpunit.xml.dist

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,18 @@
1515
</php>
1616

1717
<testsuites>
18-
<testsuite name="All-Tests">
18+
<testsuite name="all">
1919
<directory>./tests</directory>
2020
</testsuite>
21+
<testsuite name="unit">
22+
<directory>./tests/UnitTests</directory>
23+
</testsuite>
24+
<testsuite name="functional">
25+
<directory>./tests/FunctionalTests</directory>
26+
</testsuite>
27+
<testsuite name="integration">
28+
<directory>./tests/IntegratonTests</directory>
29+
</testsuite>
2130
</testsuites>
2231

2332
<filter>

tests/UnitTests/Command/ResetPasswordRemoveExpiredCommandTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Command;
1111

12+
use PHPUnit\Framework\TestCase;
1213
use Symfony\Component\Console\Input\InputInterface;
1314
use Symfony\Component\Console\Output\OutputInterface;
1415
use SymfonyCasts\Bundle\ResetPassword\Command\ResetPasswordRemoveExpiredCommand;
15-
use PHPUnit\Framework\TestCase;
1616
use SymfonyCasts\Bundle\ResetPassword\Util\ResetPasswordCleaner;
1717

1818
/**
@@ -39,8 +39,7 @@ public function testCommandCallsForcedGarbageCollection(): void
3939

4040
private function getCommandFixture($mockCleaner)
4141
{
42-
return new class ($mockCleaner) extends ResetPasswordRemoveExpiredCommand
43-
{
42+
return new class($mockCleaner) extends ResetPasswordRemoveExpiredCommand {
4443
public function callExecute(InputInterface $input, OutputInterface $output): void
4544
{
4645
$this->execute($input, $output);

tests/UnitTests/Controller/ResetPasswordControllerTraitTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ResetPasswordControllerTraitTest extends TestCase
3232
private $mockSession;
3333

3434
/**
35-
* @inheritDoc
35+
* {@inheritdoc}
3636
*/
3737
protected function setUp(): void
3838
{
@@ -135,8 +135,7 @@ private function getFixture(): object
135135
{
136136
$container = $this->getConfiguredMockContainer();
137137

138-
return new class ($container)
139-
{
138+
return new class($container) {
140139
use ResetPasswordControllerTrait;
141140

142141
private $container;

tests/UnitTests/Exception/ResetPasswordExceptionTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ public function exceptionDataProvider(): \Generator
2626
{
2727
yield [
2828
ExpiredResetPasswordTokenException::class,
29-
'The link in your email is expired. Please try to reset your password again.'
29+
'The link in your email is expired. Please try to reset your password again.',
3030
];
3131
yield [
3232
InvalidResetPasswordTokenException::class,
33-
'The reset password link is invalid. Please try to reset your password again.'
33+
'The reset password link is invalid. Please try to reset your password again.',
3434
];
3535
yield [
3636
TooManyPasswordRequestsException::class,
37-
'You have already requested a reset password email. Please check your email or try again soon.'
37+
'You have already requested a reset password email. Please check your email or try again soon.',
3838
];
3939
yield [
4040
FakeRepositoryException::class,
41-
'Please update the request_password_repository configuration in config/packages/reset_password.yaml to point to your "request password repository` service.'
41+
'Please update the request_password_repository configuration in config/packages/reset_password.yaml to point to your "request password repository` service.',
4242
];
4343
}
4444

@@ -56,7 +56,7 @@ public function testIsReason(string $exception, string $message): void
5656
*/
5757
public function testImplementsResetPasswordExceptionInterface(string $exception): void
5858
{
59-
$interfaces = class_implements($exception);
59+
$interfaces = \class_implements($exception);
6060
self::assertArrayHasKey(ResetPasswordExceptionInterface::class, $interfaces);
6161
}
6262
}

tests/UnitTests/Generator/ResetPasswordRandomGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Generator;
1111

12-
use SymfonyCasts\Bundle\ResetPassword\Generator\ResetPasswordRandomGenerator;
1312
use PHPUnit\Framework\TestCase;
13+
use SymfonyCasts\Bundle\ResetPassword\Generator\ResetPasswordRandomGenerator;
1414

1515
/**
1616
* @author Jesse Rushlow <[email protected]>
@@ -23,7 +23,7 @@ public function testIsProvidedLength(): void
2323
$generator = new ResetPasswordRandomGenerator();
2424
$result = $generator->getRandomAlphaNumStr(100);
2525

26-
self::assertSame(100, strlen($result));
26+
self::assertSame(100, \strlen($result));
2727
}
2828

2929
public function testIsRandom(): void

tests/UnitTests/Generator/ResetPasswordTokenGeneratorTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Generator;
1111

12+
use PHPUnit\Framework\TestCase;
1213
use SymfonyCasts\Bundle\ResetPassword\Generator\ResetPasswordRandomGenerator;
1314
use SymfonyCasts\Bundle\ResetPassword\Generator\ResetPasswordTokenGenerator;
14-
use PHPUnit\Framework\TestCase;
1515

1616
/**
1717
* @author Jesse Rushlow <[email protected]>
@@ -30,7 +30,7 @@ class ResetPasswordTokenGeneratorTest extends TestCase
3030
private $mockExpiresAt;
3131

3232
/**
33-
* @inheritDoc
33+
* {@inheritdoc}
3434
*/
3535
protected function setUp(): void
3636
{

tests/UnitTests/Model/ResetPasswordRequestTraitTest.php

Lines changed: 33 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -19,52 +19,9 @@
1919
*/
2020
class ResetPasswordRequestTraitTest extends TestCase
2121
{
22-
private const SUT = ResetPasswordRequestTrait::class;
23-
24-
/**
25-
* @var \DateTimeImmutable
26-
*/
27-
private $expiresAt;
28-
29-
/**
30-
* @inheritDoc
31-
*/
32-
protected function setUp(): void
33-
{
34-
$this->expiresAt = $this->createMock(\DateTimeImmutable::class);
35-
}
36-
37-
private function getFixture(): ResetPasswordRequestInterface
38-
{
39-
return new class ($this->expiresAt, '', '') implements ResetPasswordRequestInterface
40-
{
41-
use ResetPasswordRequestTrait;
42-
43-
public function __construct($expiresAt, $selector, $token)
44-
{
45-
$this->initialize($expiresAt, $selector, $token);
46-
}
47-
48-
/**
49-
* getUser() is intentionally left out of the trait.
50-
* it is created via maker under App\Entity\PasswordResetRequest
51-
* as the user property, specifically its target entity,
52-
* is unknown to the ResetPassword bundle. Although getUser()
53-
* could be added to the trait within the bundle, for clarity
54-
* sake, the maker creates the method.
55-
**/
56-
57-
public function getUser(): object
58-
{
59-
}
60-
};
61-
}
62-
6322
public function testIsCompatibleWithInterface(): void
6423
{
65-
$sut = $this->getFixture();
66-
67-
self::assertInstanceOf(ResetPasswordRequestInterface::class, $sut);
24+
self::assertInstanceOf(ResetPasswordRequestInterface::class, $this->getFixture(new \DateTimeImmutable()));
6825
}
6926

7027
public function propertyDataProvider(): \Generator
@@ -80,33 +37,50 @@ public function propertyDataProvider(): \Generator
8037
*/
8138
public function testORMAnnotationSetOnProperty(string $propertyName, string $expectedAnnotation): void
8239
{
83-
$property = new \ReflectionProperty(self::SUT, $propertyName);
40+
$property = new \ReflectionProperty(ResetPasswordRequestTrait::class, $propertyName);
8441
$result = $property->getDocComment();
8542

86-
self::assertStringContainsString($expectedAnnotation, $result, sprintf('%s::%s does not contain "%s" in the docBlock.', self::SUT, $propertyName, $expectedAnnotation));
43+
self::assertStringContainsString($expectedAnnotation, $result, \sprintf('%s::%s does not contain "%s" in the docBlock.', ResetPasswordRequestTrait::class, $propertyName, $expectedAnnotation));
8744
}
8845

89-
public function testIsExpiredReturnsFalseWithTimeInFuture(): void
46+
public function isExpiredDataProvider(): \Generator
9047
{
91-
$this->expiresAt
48+
yield 'Is expired' => [(\time() + (360)), false];
49+
yield 'Is Not Expired' => [(\time() - (360)), true];
50+
}
51+
52+
/**
53+
* @dataProvider isExpiredDataProvider
54+
*/
55+
public function testIsExpiredMethod(int $timestamp, bool $expected): void
56+
{
57+
$expiresAt = $this->createMock(\DateTimeImmutable::class);
58+
$expiresAt
9259
->expects($this->once())
9360
->method('getTimestamp')
94-
->willReturn(time() + (360))
61+
->willReturn($timestamp)
9562
;
9663

97-
$trait = $this->getFixture();
98-
self::assertFalse($trait->isExpired());
64+
$trait = $this->getFixture($expiresAt);
65+
self::assertSame($expected, $trait->isExpired());
9966
}
10067

101-
public function testIsExpiredReturnsTrueWithTimeInPast(): void
68+
private function getFixture($expiresAt): ResetPasswordRequestInterface
10269
{
103-
$this->expiresAt
104-
->expects($this->once())
105-
->method('getTimestamp')
106-
->willReturn(time() - (360))
107-
;
70+
return new class($expiresAt, '', '') implements ResetPasswordRequestInterface {
71+
use ResetPasswordRequestTrait;
72+
73+
public function __construct($expiresAt, $selector, $token)
74+
{
75+
$this->initialize($expiresAt, $selector, $token);
76+
}
10877

109-
$trait = $this->getFixture();
110-
self::assertTrue($trait->isExpired());
78+
/*
79+
* getUser() is intentionally left out of the trait.
80+
*/
81+
public function getUser(): object
82+
{
83+
}
84+
};
11185
}
11286
}

tests/UnitTests/Model/ResetPasswordTokenComponentsTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Model;
1111

12-
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordTokenComponents;
1312
use PHPUnit\Framework\TestCase;
13+
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordTokenComponents;
1414

1515
/**
1616
* @author Jesse Rushlow <[email protected]>
1717
* @author Ryan Weaver <[email protected]>
1818
*/
1919
class ResetPasswordTokenComponentsTest extends TestCase
2020
{
21-
public function testReturnsSelectAndVerifierAsString(): void
21+
public function testGetPublicTokenReturnsConcatenatedSelectorAndVerifier(): void
2222
{
2323
$tokenComponents = new ResetPasswordTokenComponents(
2424
'selector',

tests/UnitTests/Persistence/FakeResetPasswordInternalRepositoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Persistence;
1111

12+
use PHPUnit\Framework\TestCase;
1213
use SymfonyCasts\Bundle\ResetPassword\Exception\FakeRepositoryException;
1314
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
1415
use SymfonyCasts\Bundle\ResetPassword\Persistence\Fake\FakeResetPasswordInternalRepository;
15-
use PHPUnit\Framework\TestCase;
1616

1717
/**
1818
* @author Jesse Rushlow <[email protected]>

tests/UnitTests/Persistence/ResetPasswordRequestRepositoryTraitTest.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\UnitTests\Persistence;
1111

12-
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
13-
use SymfonyCasts\Bundle\ResetPassword\Persistence\ResetPasswordRequestRepositoryInterface;
1412
use PHPUnit\Framework\TestCase;
13+
use SymfonyCasts\Bundle\ResetPassword\Model\ResetPasswordRequestInterface;
1514
use SymfonyCasts\Bundle\ResetPassword\Persistence\Repository\ResetPasswordRequestRepositoryTrait;
15+
use SymfonyCasts\Bundle\ResetPassword\Persistence\ResetPasswordRequestRepositoryInterface;
1616

1717
/**
1818
* @author Jesse Rushlow <[email protected]>
@@ -22,19 +22,18 @@ class ResetPasswordRequestRepositoryTraitTest extends TestCase
2222
{
2323
public function testTraitIsCompatibleWithInterface(): void
2424
{
25-
$sut = new class implements ResetPasswordRequestRepositoryInterface{
25+
$fixture = new class() implements ResetPasswordRequestRepositoryInterface {
2626
use ResetPasswordRequestRepositoryTrait;
2727

2828
public function createResetPasswordRequest(
2929
object $user,
3030
\DateTimeInterface $expiresAt,
3131
string $selector,
3232
string $hashedToken
33-
): ResetPasswordRequestInterface {}
33+
): ResetPasswordRequestInterface {
34+
}
3435
};
3536

36-
self::assertInstanceOf(ResetPasswordRequestRepositoryInterface::class, $sut);
37+
self::assertInstanceOf(ResetPasswordRequestRepositoryInterface::class, $fixture);
3738
}
38-
39-
//@TODO Add unit tests for trait methods in separate PR
4039
}

0 commit comments

Comments
 (0)