|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Security\RememberMe; |
| 4 | + |
| 5 | +use Doctrine\DBAL\DriverManager; |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider; |
| 8 | +use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken; |
| 9 | +use Symfony\Component\Security\Core\Exception\TokenNotFoundException; |
| 10 | + |
| 11 | +class DoctrineTokenProviderTest extends TestCase |
| 12 | +{ |
| 13 | + public static function setUpBeforeClass() |
| 14 | + { |
| 15 | + if (\PHP_VERSION_ID >= 80000) { |
| 16 | + self::markTestSkipped('Doctrine DBAL 2.x is incompatible with PHP 8.'); |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public function testCreateNewToken() |
| 21 | + { |
| 22 | + $provider = $this->bootstrapProvider(); |
| 23 | + |
| 24 | + $token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51')); |
| 25 | + $provider->createNewToken($token); |
| 26 | + |
| 27 | + $this->assertEquals($provider->loadTokenBySeries('someSeries'), $token); |
| 28 | + } |
| 29 | + |
| 30 | + public function testLoadTokenBySeriesThrowsNotFoundException() |
| 31 | + { |
| 32 | + $provider = $this->bootstrapProvider(); |
| 33 | + |
| 34 | + $this->expectException(TokenNotFoundException::class); |
| 35 | + $provider->loadTokenBySeries('someSeries'); |
| 36 | + } |
| 37 | + |
| 38 | + public function testUpdateToken() |
| 39 | + { |
| 40 | + $provider = $this->bootstrapProvider(); |
| 41 | + |
| 42 | + $token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51')); |
| 43 | + $provider->createNewToken($token); |
| 44 | + $provider->updateToken('someSeries', 'newValue', $lastUsed = new \DateTime('2014-06-26T22:03:46')); |
| 45 | + $token = $provider->loadTokenBySeries('someSeries'); |
| 46 | + |
| 47 | + $this->assertEquals('newValue', $token->getTokenValue()); |
| 48 | + $this->assertEquals($token->getLastUsed(), $lastUsed); |
| 49 | + } |
| 50 | + |
| 51 | + public function testDeleteToken() |
| 52 | + { |
| 53 | + $provider = $this->bootstrapProvider(); |
| 54 | + $token = new PersistentToken('someClass', 'someUser', 'someSeries', 'tokenValue', new \DateTime('2013-01-26T18:23:51')); |
| 55 | + $provider->createNewToken($token); |
| 56 | + $provider->deleteTokenBySeries('someSeries'); |
| 57 | + |
| 58 | + $this->expectException(TokenNotFoundException::class); |
| 59 | + |
| 60 | + $provider->loadTokenBySeries('someSeries'); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return DoctrineTokenProvider |
| 65 | + */ |
| 66 | + private function bootstrapProvider() |
| 67 | + { |
| 68 | + $connection = DriverManager::getConnection([ |
| 69 | + 'driver' => 'pdo_sqlite', |
| 70 | + 'url' => 'sqlite:///:memory:', |
| 71 | + ]); |
| 72 | + $connection->executeUpdate(<<< 'SQL' |
| 73 | + CREATE TABLE rememberme_token ( |
| 74 | + series char(88) UNIQUE PRIMARY KEY NOT NULL, |
| 75 | + value char(88) NOT NULL, |
| 76 | + lastUsed datetime NOT NULL, |
| 77 | + class varchar(100) NOT NULL, |
| 78 | + username varchar(200) NOT NULL |
| 79 | + ); |
| 80 | +SQL |
| 81 | + ); |
| 82 | + |
| 83 | + return new DoctrineTokenProvider($connection); |
| 84 | + } |
| 85 | +} |
0 commit comments