-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from Allypost/bugfix/101-fix-integrity-violation
Fix Integrity Constraint Violation exception on delete command
- Loading branch information
Showing
5 changed files
with
134 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters