Skip to content

Commit

Permalink
Merge pull request #127 from Allypost/bugfix/101-fix-integrity-violation
Browse files Browse the repository at this point in the history
Fix Integrity Constraint Violation exception on delete command
  • Loading branch information
HypeMC authored Dec 5, 2019
2 parents b2ee617 + 2b71617 commit 424b770
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Resources/config/doctrine/model/AccessToken.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<field name="scopes" type="oauth2_scope" nullable="true" />
<field name="revoked" type="boolean" />
<many-to-one field="client" target-entity="Trikoder\Bundle\OAuth2Bundle\Model\Client">
<join-column name="client" referenced-column-name="identifier" nullable="false" />
<join-column name="client" referenced-column-name="identifier" nullable="false" on-delete="CASCADE" />
</many-to-one>
</entity>
</doctrine-mapping>
19 changes: 11 additions & 8 deletions Tests/Acceptance/DeleteClientCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Trikoder\Bundle\OAuth2Bundle\Manager\ClientManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;

/**
* @covers \Trikoder\Bundle\OAuth2Bundle\Command\DeleteClientCommand
*/
final class DeleteClientCommandTest extends AbstractAcceptanceTest
{
public function testDeleteClient(): void
Expand Down Expand Up @@ -42,27 +45,27 @@ public function testDeleteNonExistentClient(): void
$this->assertStringContainsString(sprintf('oAuth2 client identified as "%s" does not exist', $identifierName), $output);
}

private function findClient($identifier): ?Client
private function findClient(string $identifier): ?Client
{
return
$this
->client
->getContainer()
->get(ClientManagerInterface::class)
->getClientManager()
->find($identifier)
;
}

private function fakeAClient($identifier): Client
private function fakeAClient(string $identifier): Client
{
return new Client($identifier, 'quzbaz');
}

private function getClientManager(): ClientManagerInterface
{
return $this->client
->getContainer()
->get(ClientManagerInterface::class)
return
$this
->client
->getContainer()
->get(ClientManagerInterface::class)
;
}

Expand Down
6 changes: 3 additions & 3 deletions Tests/Acceptance/DoctrineAccessTokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Tests\Unit;
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance;

use DateTime;
use Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\AccessTokenManager as DoctrineAccessTokenManager;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;
use Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance\AbstractAcceptanceTest;

/**
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first.
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first.
* @covers \Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\AccessTokenManager
*/
final class DoctrineAccessTokenManagerTest extends AbstractAcceptanceTest
{
Expand Down
116 changes: 116 additions & 0 deletions Tests/Acceptance/DoctrineClientManagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance;

use DateTime;
use Doctrine\ORM\EntityManagerInterface;
use Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\ClientManager as DoctrineClientManager;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;

/**
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first.
* @covers \Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\ClientManager
*/
final class DoctrineClientManagerTest extends AbstractAcceptanceTest
{
public function testSimpleDelete(): void
{
/** @var $em EntityManagerInterface */
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$doctrineClientManager = new DoctrineClientManager($em);

$client = new Client('client', 'secret');
$em->persist($client);
$em->flush();

$doctrineClientManager->remove($client);

$this->assertNull(
$em
->getRepository(Client::class)
->find($client->getIdentifier())
);
}

public function testClientDeleteCascadesToAccessTokens(): void
{
/** @var $em EntityManagerInterface */
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$doctrineClientManager = new DoctrineClientManager($em);

$client = new Client('client', 'secret');
$em->persist($client);
$em->flush();

$accessToken = new AccessToken('access token', (new DateTime())->modify('+1 day'), $client, $client->getIdentifier(), []);
$em->persist($accessToken);
$em->flush();

$doctrineClientManager->remove($client);

$this->assertNull(
$em
->getRepository(Client::class)
->find($client->getIdentifier())
);

// The entity manager has to be cleared manually
// because it doesn't process deep integrity constraints
$em->clear();

$this->assertNull(
$em
->getRepository(AccessToken::class)
->find($accessToken->getIdentifier())
);
}

public function testClientDeleteCascadesToAccessTokensAndRefreshTokens(): void
{
/** @var $em EntityManagerInterface */
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$doctrineClientManager = new DoctrineClientManager($em);

$client = new Client('client', 'secret');
$em->persist($client);
$em->flush();

$accessToken = new AccessToken('access token', (new DateTime())->modify('+1 day'), $client, $client->getIdentifier(), []);
$em->persist($accessToken);
$em->flush();

$refreshToken = new RefreshToken('refresh token', (new DateTime())->modify('+1 day'), $accessToken);
$em->persist($refreshToken);
$em->flush();

$doctrineClientManager->remove($client);

$this->assertNull(
$em
->getRepository(Client::class)
->find($client->getIdentifier())
);

// The entity manager has to be cleared manually
// because it doesn't process deep integrity constraints
$em->clear();

$this->assertNull(
$em
->getRepository(AccessToken::class)
->find($accessToken->getIdentifier())
);

/** @var $refreshToken RefreshToken */
$refreshToken = $em
->getRepository(RefreshToken::class)
->find($refreshToken->getIdentifier())
;
$this->assertNotNull($refreshToken);
$this->assertNull($refreshToken->getAccessToken());
}
}
6 changes: 3 additions & 3 deletions Tests/Acceptance/DoctrineRefreshTokenManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

declare(strict_types=1);

namespace Trikoder\Bundle\OAuth2Bundle\Tests\Unit;
namespace Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance;

use DateTime;
use Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\RefreshTokenManager as DoctrineRefreshTokenManager;
use Trikoder\Bundle\OAuth2Bundle\Model\AccessToken;
use Trikoder\Bundle\OAuth2Bundle\Model\Client;
use Trikoder\Bundle\OAuth2Bundle\Model\RefreshToken;
use Trikoder\Bundle\OAuth2Bundle\Tests\Acceptance\AbstractAcceptanceTest;

/**
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first.
* @TODO This should be in the Integration tests folder but the current tests infrastructure would need improvements first.
* @covers \Trikoder\Bundle\OAuth2Bundle\Manager\Doctrine\RefreshTokenManager
*/
final class DoctrineRefreshTokenManagerTest extends AbstractAcceptanceTest
{
Expand Down

0 comments on commit 424b770

Please sign in to comment.