Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,12 @@
"doctrine/orm": "^2.9",
"symfony/browser-kit": "^6.4 || ^7.3",
"symfony/css-selector": "^6.4 || ^7.3",
"symfony/phpunit-bridge": "^6.4 || ^7.3",
"symfony/property-access": "^6.4 || ^7.3",
"symfony/validator": "^6.4 || ^7.3",
"symfony/stopwatch": "^6.4 || ^7.3",
"symfony/translation": "^6.4 || ^7.3",
"symfony/yaml": "^6.4 || ^7.3",
"phpunit/phpunit": "^9.6.11",
"phpunit/phpunit": "^12.3",
"friendsofphp/php-cs-fixer": "^3.23",
"symfony/monolog-bundle": "^3.4",
"phpstan/phpstan": "^1.10",
Expand Down
46 changes: 22 additions & 24 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./tests/bootstrap.php"
colors="true"
cacheDirectory=".phpunit.cache"
>
<php>
<env name="KERNEL_CLASS" value="HWI\Bundle\OAuthBundle\Tests\App\AppKernel"/>
</php>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="./tests/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<testsuites>
<testsuite name="HWIOAuthBundle test suite">
<directory>./tests</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory>./src/</directory>
</include>
<exclude>
<directory>./src/Resources</directory>
</exclude>
</coverage>

<php>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[direct]=5"/>
<env name="KERNEL_CLASS" value="HWI\Bundle\OAuthBundle\Tests\App\AppKernel"/>
</php>

<testsuites>
<testsuite name="HWIOAuthBundle test suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<source>
<include>
<directory>./src/</directory>
</include>
<exclude>
<directory>./src/Resources</directory>
</exclude>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/OAuth/Response/SensioConnectUserResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public function setData($data): void
{
$dom = new DOMDocument();
try {
if (!$dom->loadXML($data)) {
if (!@$dom->loadXML($data)) {
throw new ErrorException('Could not transform this xml to a \DOMDocument instance.');
}
} catch (Exception $e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use HWI\Bundle\OAuthBundle\Security\Helper\NonceGenerator;
use HWI\Bundle\OAuthBundle\Test\Fixtures\CustomUserResponse;
use InvalidArgumentException;
use PHPUnit\Framework\Attributes\DataProvider;
use ReflectionClass;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\Exception\ExceptionInterface;
Expand Down Expand Up @@ -206,9 +207,7 @@ public function testGetAuthorizationUrlWithEnabledCsrf(): void
$this->state = $state->encode();
}

/**
* @dataProvider provideAccessTokenData
*/
#[DataProvider('provideAccessTokenData')]
public function testGetAccessToken(string $response, string $contentType): void
{
$resourceOwner = $this->createResourceOwner(
Expand All @@ -227,7 +226,7 @@ public function testGetAccessToken(string $response, string $contentType): void
);
}

public function provideAccessTokenData(): iterable
public static function provideAccessTokenData(): iterable
{
yield 'plain text' => [
'access_token=code',
Expand Down Expand Up @@ -292,9 +291,7 @@ public function testGetAccessTokenErrorResponse(): void
$resourceOwner->getAccessToken($request, 'http://redirect.to/');
}

/**
* @dataProvider provideRefreshToken
*/
#[DataProvider('provideRefreshToken')]
public function testRefreshAccessToken($response, $contentType): void
{
$resourceOwner = $this->createResourceOwner(
Expand All @@ -311,17 +308,15 @@ public function testRefreshAccessToken($response, $contentType): void
$this->assertEquals(3600, $accessToken['expires_in']);
}

public function provideRefreshToken(): iterable
public static function provideRefreshToken(): iterable
{
yield 'correct token' => [
'{"access_token": "bar", "expires_in": 3600}',
'application/json',
];
}

/**
* @dataProvider provideInvalidRefreshToken
*/
#[DataProvider('provideInvalidRefreshToken')]
public function testRefreshAccessTokenInvalid(string $response, string $exceptionClass): void
{
$this->expectException($exceptionClass);
Expand All @@ -336,7 +331,7 @@ public function testRefreshAccessTokenInvalid(string $response, string $exceptio
$resourceOwner->refreshAccessToken('foo');
}

public function provideInvalidRefreshToken(): iterable
public static function provideInvalidRefreshToken(): iterable
{
yield 'invalid' => [
'invalid',
Expand Down Expand Up @@ -445,7 +440,6 @@ protected function createResourceOwner(

$reflection = new ReflectionClass($resourceOwner::class);
$stateProperty = $reflection->getProperty('state');
$stateProperty->setAccessible(true);
$stateProperty->setValue($resourceOwner, $state ?: new State($this->state));

return $resourceOwner;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
use Symfony\Component\Security\Http\HttpUtils;
use Twig\Environment;

abstract class AbstractConnectControllerTest extends TestCase
abstract class AbstractConnectControllerTestCase extends TestCase
{
protected ResourceOwnerMapLocator $resourceOwnerMapLocator;
protected Request $request;
Expand Down
34 changes: 25 additions & 9 deletions tests/Controller/Connect/ConnectControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

final class ConnectControllerTest extends AbstractConnectControllerTest
final class ConnectControllerTest extends AbstractConnectControllerTestCase
{
public function testNotEnabled(): void
{
Expand Down Expand Up @@ -119,12 +119,14 @@ public function testConnectSuccess(): void
->willReturn(new CustomOAuthToken())
;

$capturedDispatches = [];
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::CONNECT_CONFIRMED],
[$this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::CONNECT_COMPLETED]
);
->willReturnCallback(function ($event, $eventName) use (&$capturedDispatches) {
$capturedDispatches[] = [$event, $eventName];

return $event;
});

$this->twig->expects($this->once())
->method('render')
Expand All @@ -133,6 +135,12 @@ public function testConnectSuccess(): void

$controller = $this->createConnectController();
$controller->connectServiceAction($this->request, 'facebook');

$this->assertCount(2, $capturedDispatches);
$this->assertInstanceOf(GetResponseUserEvent::class, $capturedDispatches[0][0]);
$this->assertSame(HWIOAuthEvents::CONNECT_CONFIRMED, $capturedDispatches[0][1]);
$this->assertInstanceOf(FilterUserResponseEvent::class, $capturedDispatches[1][0]);
$this->assertSame(HWIOAuthEvents::CONNECT_COMPLETED, $capturedDispatches[1][1]);
}

public function testConnectNoConfirmation(): void
Expand All @@ -154,12 +162,14 @@ public function testConnectNoConfirmation(): void
->willReturn(new CustomOAuthToken())
;

$capturedDispatches = [];
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::CONNECT_CONFIRMED],
[$this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::CONNECT_COMPLETED]
);
->willReturnCallback(function ($event, $eventName) use (&$capturedDispatches) {
$capturedDispatches[] = [$event, $eventName];

return $event;
});

$this->twig->expects($this->once())
->method('render')
Expand All @@ -168,6 +178,12 @@ public function testConnectNoConfirmation(): void

$controller = $this->createConnectController(true, false);
$controller->connectServiceAction($this->request, 'facebook');

$this->assertCount(2, $capturedDispatches);
$this->assertInstanceOf(GetResponseUserEvent::class, $capturedDispatches[0][0]);
$this->assertSame(HWIOAuthEvents::CONNECT_CONFIRMED, $capturedDispatches[0][1]);
$this->assertInstanceOf(FilterUserResponseEvent::class, $capturedDispatches[1][0]);
$this->assertSame(HWIOAuthEvents::CONNECT_COMPLETED, $capturedDispatches[1][1]);
}

public function testResourceOwnerHandle(): void
Expand Down
28 changes: 19 additions & 9 deletions tests/Controller/Connect/RegistrationControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
use HWI\Bundle\OAuthBundle\Tests\App\Form\RegistrationFormType;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\User;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\IgnoreDeprecations;
use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Expand All @@ -27,7 +29,7 @@
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

final class RegistrationControllerTest extends AbstractConnectControllerTest
final class RegistrationControllerTest extends AbstractConnectControllerTestCase
{
public function testNotEnabled(): void
{
Expand Down Expand Up @@ -72,9 +74,8 @@ public function testCannotRegisterBadError(): void
$controller->registrationAction($this->request, $key);
}

/**
* @group legacy
*/
#[IgnoreDeprecations]
#[Group('legacy')]
public function testFailedProcess(): void
{
$key = (string) time();
Expand Down Expand Up @@ -137,13 +138,14 @@ public function testProcessWorks(): void
->method('connect')
;

$capturedDispatches = [];
$this->eventDispatcher->expects($this->exactly(3))
->method('dispatch')
->withConsecutive(
[$this->isInstanceOf(FormEvent::class), HWIOAuthEvents::REGISTRATION_SUCCESS],
[$this->isInstanceOf(InteractiveLoginEvent::class), SecurityEvents::INTERACTIVE_LOGIN],
[$this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::REGISTRATION_COMPLETED]
);
->willReturnCallback(function ($event, $eventName) use (&$capturedDispatches) {
$capturedDispatches[] = [$event, $eventName];

return $event;
});

$this->twig->expects($this->once())
->method('render')
Expand All @@ -152,6 +154,14 @@ public function testProcessWorks(): void

$controller = $this->createConnectController();
$controller->registrationAction($this->request, $key);

$this->assertCount(3, $capturedDispatches);
$this->assertInstanceOf(FormEvent::class, $capturedDispatches[0][0]);
$this->assertSame(HWIOAuthEvents::REGISTRATION_SUCCESS, $capturedDispatches[0][1]);
$this->assertInstanceOf(InteractiveLoginEvent::class, $capturedDispatches[1][0]);
$this->assertSame(SecurityEvents::INTERACTIVE_LOGIN, $capturedDispatches[1][1]);
$this->assertInstanceOf(FilterUserResponseEvent::class, $capturedDispatches[2][0]);
$this->assertSame(HWIOAuthEvents::REGISTRATION_COMPLETED, $capturedDispatches[2][1]);
}

private function makeRegistrationForm(): void
Expand Down
Loading
Loading