Skip to content

Commit

Permalink
Deprecate HttpTester; refactor remaining tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DumitracheAdrian committed Sep 25, 2024
1 parent 9ff2145 commit 4da494d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 89 deletions.
35 changes: 12 additions & 23 deletions packages/security/Tests/Http/Authenticator/JwtAuthenticatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Draw\Component\Security\Http\Authenticator\JwtAuthenticator;
use Draw\Component\Security\Http\Authenticator\Passport\Badge\JwtPayloadBadge;
use Draw\Component\Security\Jwt\JwtEncoder;
use Draw\Component\Security\Tests\Mock\MockableUserInterface;
use Draw\Component\Tester\MockTrait;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
Expand Down Expand Up @@ -39,10 +40,10 @@ class JwtAuthenticatorTest extends TestCase

private string $userIdentifierPayloadKey;

private string $userIdentifierGetter;

private TranslatorInterface&MockObject $translator;

private static string $userIdentifierGetter = 'getIdentifierForJwtAuthenticatorTest';

protected function setUp(): void
{
$this->userProvider = $this->createMock(UserProviderInterface::class);
Expand All @@ -51,7 +52,7 @@ protected function setUp(): void
$this->jwtEncoder = $this->createMock(JwtEncoder::class),
$this->userProvider,
$this->userIdentifierPayloadKey = uniqid('key'),
$this->userIdentifierGetter = uniqid('get'),
self::$userIdentifierGetter,
$this->translator = $this->createMock(TranslatorInterface::class)
);
}
Expand Down Expand Up @@ -92,14 +93,11 @@ public function testSupportsNoToken(): void

public function testGenerateToken(): void
{
$user = $this->createMockWithExtraMethods(
UserInterface::class,
[$this->userIdentifierGetter]
);
$user = $this->createMock(MockableUserInterface::class);

$user
->expects(static::once())
->method($this->userIdentifierGetter)
->method(self::$userIdentifierGetter)
->willReturn($userId = uniqid('id'));

$this->jwtEncoder
Expand All @@ -125,14 +123,11 @@ public function testGenerateTokenDefaultNull(): void
null
);

$user = $this->createMockWithExtraMethods(
UserInterface::class,
[$this->userIdentifierGetter]
);
$user = $this->createMock(MockableUserInterface::class);

$user
->expects(static::once())
->method($this->userIdentifierGetter)
->method(self::$userIdentifierGetter)
->willReturn($userId = uniqid('id'));

$this->jwtEncoder
Expand All @@ -151,14 +146,11 @@ public function testGenerateTokenDefaultNull(): void

public function testGenerateTokenWithExpiration(): void
{
$user = $this->createMockWithExtraMethods(
UserInterface::class,
[$this->userIdentifierGetter]
);
$user = $this->createMock(MockableUserInterface::class);

$user
->expects(static::once())
->method($this->userIdentifierGetter)
->method(self::$userIdentifierGetter)
->willReturn($userId = uniqid('id'));

$this->jwtEncoder
Expand All @@ -180,14 +172,11 @@ public function testGenerateTokenWithExpiration(): void

public function testGenerateTokenWithExtraPayload(): void
{
$user = $this->createMockWithExtraMethods(
UserInterface::class,
[$this->userIdentifierGetter]
);
$user = $this->createMock(MockableUserInterface::class);

$user
->expects(static::once())
->method($this->userIdentifierGetter)
->method(self::$userIdentifierGetter)
->willReturn($userId = uniqid('id'));

$extraPayload = [
Expand Down
12 changes: 12 additions & 0 deletions packages/security/Tests/Mock/MockableUserInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Draw\Component\Security\Tests\Mock;

use Symfony\Component\Security\Core\User\UserInterface;

interface MockableUserInterface extends UserInterface
{
public function getIdentifierForJwtAuthenticatorTest(): ?string;
}
7 changes: 1 addition & 6 deletions packages/tester/.phpstorm.meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

namespace PHPSTORM_META;

override(
\Draw\Component\Tester\MockTrait::createMockWithExtraMethods(),
map(["" => "@"])
);

override(
\Draw\Component\Tester\MockTrait::mockProperty(2),
map([""=>"$2"])
);
);
3 changes: 3 additions & 0 deletions packages/tester/HttpTesterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Draw\Component\Tester\Http\ClientInterface;
use PHPUnit\Framework\Attributes\BeforeClass;

/**
* @deprecated
*/
trait HttpTesterTrait
{
protected static ?ClientInterface $httpTesterClient = null;
Expand Down
19 changes: 0 additions & 19 deletions packages/tester/MockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,6 @@ abstract public function getMockBuilder(string $className): MockBuilder;

abstract protected function createMock(string $originalClassName): MockObject;

/**
* @template T of object
*
* @phpstan-param class-string<T> $originalClassName
*
* @return MockObject&T
*/
protected function createMockWithExtraMethods(string $originalClassName, array $methods): MockObject
{
return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->onlyMethods(get_class_methods($originalClassName))
->addMethods($methods)
->getMock();
}

/**
* @template T of object
*
Expand Down
37 changes: 0 additions & 37 deletions packages/tester/Tests/Http/ClientObserverTest.php

This file was deleted.

3 changes: 1 addition & 2 deletions phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ parameters:
- packages/fixer/Tests/fixtures/*
ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag\(\).#'
- '#Trying to mock an undefined method [a-zA-Z0-9\\_]#'
#- '#provideTestArgument\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#provideTestOption\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#getHandledMessages\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Test::getDefaultConfiguration\(\) return type has no value type specified in iterable type array.#'
#- '#Test::getConfiguration\(\) return type has no value type specified in iterable type array.#'
#- '#Test::provide[a-zA-Z0-9]*\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#'
3 changes: 1 addition & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ parameters:
- packages/fixer/Tests/fixtures/*
ignoreErrors:
- '#Call to an undefined method Symfony\\Component\\HttpFoundation\\Session\\SessionInterface::getFlashBag\(\).#'
- '#Trying to mock an undefined method [a-zA-Z0-9\\_]#'
#- '#provideTestArgument\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#provideTestOption\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#getHandledMessages\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Test::getDefaultConfiguration\(\) return type has no value type specified in iterable type array.#'
#- '#Test::getConfiguration\(\) return type has no value type specified in iterable type array.#'
#- '#Test::provide[a-zA-Z0-9]*\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#'
#- '#Method [a-zA-Z0-9\\]*::getForEmails\(\) return type has no value type specified in iterable type (iterable|array).#'

0 comments on commit 4da494d

Please sign in to comment.