Skip to content

[Config][Lock][SecurityBundle]  do not use uniqid() in tests #57665

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 6, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function callInRequestContext(KernelBrowser $client, callable $callable)

$eventDispatcher->addListener(KernelEvents::REQUEST, $wrappedCallable);
try {
$client->request('GET', '/'.uniqid('', true));
$client->request('GET', '/not-existent');
} finally {
$eventDispatcher->removeListener(KernelEvents::REQUEST, $wrappedCallable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private function callInRequestContext(KernelBrowser $client, callable $callable)

$eventDispatcher->addListener(KernelEvents::REQUEST, $wrappedCallable);
try {
$client->request('GET', '/'.uniqid('', true));
$client->request('GET', '/not-existent');
} finally {
$eventDispatcher->removeListener(KernelEvents::REQUEST, $wrappedCallable);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function testLogoutWithCsrf()
};
$eventDispatcher->addListener(KernelEvents::REQUEST, $setCsrfToken);
try {
$client->request('GET', '/'.uniqid('', true));
$client->request('GET', '/not-existent');
} finally {
$eventDispatcher->removeListener(KernelEvents::REQUEST, $setCsrfToken);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testIsFreshForDeletedResources()
/**
* @dataProvider provideHashedSignature
*/
public function testHashedSignature(bool $changeExpected, int $changedLine, ?string $changedCode, ?\Closure $setContext = null)
public function testHashedSignature(bool $changeExpected, int $changedLine, ?string $changedCode, int $resourceClassNameSuffix, ?\Closure $setContext = null)
{
if ($setContext) {
$setContext();
Expand Down Expand Up @@ -94,7 +94,7 @@ public function testHashedSignature(bool $changeExpected, int $changedLine, ?str
static $expectedSignature, $generateSignature;

if (null === $expectedSignature) {
eval(\sprintf($code, $class = 'Foo'.str_replace('.', '_', uniqid('', true))));
eval(\sprintf($code, $class = 'Foo'.(string) $resourceClassNameSuffix));
$r = new \ReflectionClass(ReflectionClassResource::class);
$generateSignature = $r->getMethod('generateSignature');
$generateSignature = $generateSignature->getClosure($r->newInstanceWithoutConstructor());
Expand All @@ -105,7 +105,7 @@ public function testHashedSignature(bool $changeExpected, int $changedLine, ?str
if (null !== $changedCode) {
$code[$changedLine] = $changedCode;
}
eval(\sprintf(implode("\n", $code), $class = 'Foo'.str_replace('.', '_', uniqid('', true))));
eval(\sprintf(implode("\n", $code), $class = 'Bar'.(string) $resourceClassNameSuffix));
$signature = implode("\n", iterator_to_array($generateSignature(new \ReflectionClass($class))));

if ($changeExpected) {
Expand All @@ -117,49 +117,50 @@ public function testHashedSignature(bool $changeExpected, int $changedLine, ?str

public static function provideHashedSignature(): iterable
{
yield [false, 0, "// line change\n\n"];
yield [true, 0, '/** class docblock */'];
yield [true, 0, '#[Foo]'];
yield [true, 0, '#[Foo(new MissingClass)]'];
yield [true, 1, 'abstract class %s'];
yield [true, 1, 'final class %s'];
yield [true, 1, 'class %s extends Exception'];
yield [true, 1, 'class %s implements '.DummyInterface::class];
yield [true, 3, 'const FOO = 456;'];
yield [true, 3, 'const BAR = 123;'];
yield [true, 4, '/** pub docblock */'];
yield [true, 5, 'protected $pub = [];'];
yield [true, 5, 'public $pub = [123];'];
yield [true, 5, '#[Foo(new MissingClass)] public $pub = [];'];
yield [true, 6, '/** prot docblock */'];
yield [true, 7, 'private $prot;'];
yield [false, 8, '/** priv docblock */'];
yield [false, 9, 'private $priv = 123;'];
yield [true, 10, '/** pub docblock */'];
yield [true, 11, 'public function pub(...$arg) {}'];
yield [true, 11, 'public function pub($arg = null): Foo {}'];
yield [false, 11, "public function pub(\$arg = null) {\nreturn 123;\n}"];
yield [true, 12, '/** prot docblock */'];
yield [true, 13, 'protected function prot($a = [123]) {}'];
yield [true, 13, '#[Foo] protected function prot($a = []) {}'];
yield [true, 13, 'protected function prot(#[Foo] $a = []) {}'];
yield [true, 13, '#[Foo(new MissingClass)] protected function prot($a = []) {}'];
yield [true, 13, 'protected function prot(#[Foo(new MissingClass)] $a = []) {}'];
yield [false, 14, '/** priv docblock */'];
yield [false, 15, ''];
$i = 0;
yield [false, 0, "// line change\n\n", ++$i];
yield [true, 0, '/** class docblock */', ++$i];
yield [true, 0, '#[Foo]', ++$i];
yield [true, 0, '#[Foo(new MissingClass)]', ++$i];
yield [true, 1, 'abstract class %s', ++$i];
yield [true, 1, 'final class %s', ++$i];
yield [true, 1, 'class %s extends Exception', ++$i];
yield [true, 1, 'class %s implements '.DummyInterface::class, ++$i];
yield [true, 3, 'const FOO = 456;', ++$i];
yield [true, 3, 'const BAR = 123;', ++$i];
yield [true, 4, '/** pub docblock */', ++$i];
yield [true, 5, 'protected $pub = [];', ++$i];
yield [true, 5, 'public $pub = [123];', ++$i];
yield [true, 5, '#[Foo(new MissingClass)] public $pub = [];', ++$i];
yield [true, 6, '/** prot docblock */', ++$i];
yield [true, 7, 'private $prot;', ++$i];
yield [false, 8, '/** priv docblock */', ++$i];
yield [false, 9, 'private $priv = 123;', ++$i];
yield [true, 10, '/** pub docblock */', ++$i];
yield [true, 11, 'public function pub(...$arg) {}', ++$i];
yield [true, 11, 'public function pub($arg = null): Foo {}', ++$i];
yield [false, 11, "public function pub(\$arg = null) {\nreturn 123;\n}", ++$i];
yield [true, 12, '/** prot docblock */', ++$i];
yield [true, 13, 'protected function prot($a = [123]) {}', ++$i];
yield [true, 13, '#[Foo] protected function prot($a = []) {}', ++$i];
yield [true, 13, 'protected function prot(#[Foo] $a = []) {}', ++$i];
yield [true, 13, '#[Foo(new MissingClass)] protected function prot($a = []) {}', ++$i];
yield [true, 13, 'protected function prot(#[Foo(new MissingClass)] $a = []) {}', ++$i];
yield [false, 14, '/** priv docblock */', ++$i];
yield [false, 15, null, ++$i];

// PHP7.4 typed properties without default value are
// undefined, make sure this doesn't throw an error
yield [true, 5, 'public array $pub;'];
yield [false, 7, 'protected int $prot;'];
yield [false, 9, 'private string $priv;'];
yield [true, 17, 'public function __construct(private $bar = new \stdClass()) {}'];
yield [true, 17, 'public function ccc($bar = new \stdClass()) {}'];
yield [true, 17, 'public function ccc($bar = new MissingClass()) {}'];
yield [true, 17, 'public function ccc($bar = 187) {}'];
yield [true, 17, 'public function ccc($bar = ANOTHER_ONE_THAT_WILL_NEVER_BE_DEFINED_CCCCCCCCC) {}'];
yield [true, 17, 'public function ccc($bar = parent::BOOM) {}'];
yield [false, 17, null, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
yield [true, 5, 'public array $pub;', ++$i];
yield [false, 7, 'protected int $prot;', ++$i];
yield [false, 9, 'private string $priv;', ++$i];
yield [true, 17, 'public function __construct(private $bar = new \stdClass()) {}', ++$i];
yield [true, 17, 'public function ccc($bar = new \stdClass()) {}', ++$i];
yield [true, 17, 'public function ccc($bar = new MissingClass()) {}', ++$i];
yield [true, 17, 'public function ccc($bar = 187) {}', ++$i];
yield [true, 17, 'public function ccc($bar = ANOTHER_ONE_THAT_WILL_NEVER_BE_DEFINED_CCCCCCCCC) {}', ++$i];
yield [true, 17, 'public function ccc($bar = parent::BOOM) {}', ++$i];
yield [false, 17, null, ++$i, static function () { \define('A_CONSTANT_THAT_FOR_SURE_WILL_NEVER_BE_DEFINED_CCCCCC', 'foo'); }];
}

public function testEventSubscriber()
Expand Down
54 changes: 27 additions & 27 deletions src/Symfony/Component/Lock/Tests/LockTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class LockTest extends TestCase
{
public function testAcquireNoBlocking()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -48,7 +48,7 @@ public function testAcquireNoBlocking()

public function testAcquireNoBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -64,7 +64,7 @@ public function testAcquireNoBlockingWithPersistingStoreInterface()

public function testAcquireBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -80,7 +80,7 @@ public function testAcquireBlockingWithPersistingStoreInterface()

public function testAcquireBlockingRetryWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -102,7 +102,7 @@ public function testAcquireBlockingRetryWithPersistingStoreInterface()

public function testAcquireReturnsFalse()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -119,7 +119,7 @@ public function testAcquireReturnsFalse()

public function testAcquireReturnsFalseStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -136,7 +136,7 @@ public function testAcquireReturnsFalseStoreInterface()

public function testAcquireBlockingWithBlockingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(BlockingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -155,7 +155,7 @@ public function testAcquireBlockingWithBlockingStoreInterface()

public function testAcquireSetsTtl()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -175,7 +175,7 @@ public function testAcquireSetsTtl()

public function testRefresh()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -192,7 +192,7 @@ public function testRefresh()

public function testRefreshCustom()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -209,7 +209,7 @@ public function testRefreshCustom()

public function testIsAquired()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -223,7 +223,7 @@ public function testIsAquired()

public function testRelease()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -243,7 +243,7 @@ public function testRelease()

public function testReleaseStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -263,7 +263,7 @@ public function testReleaseStoreInterface()

public function testReleaseOnDestruction()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(BlockingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -282,7 +282,7 @@ public function testReleaseOnDestruction()

public function testNoAutoReleaseWhenNotConfigured()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(BlockingStoreInterface::class);
$lock = new Lock($key, $store, 10, false);

Expand All @@ -302,7 +302,7 @@ public function testNoAutoReleaseWhenNotConfigured()
public function testReleaseThrowsExceptionWhenDeletionFail()
{
$this->expectException(LockReleasingException::class);
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -324,7 +324,7 @@ public function testReleaseThrowsExceptionWhenDeletionFail()
public function testReleaseThrowsExceptionIfNotWellDeleted()
{
$this->expectException(LockReleasingException::class);
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -345,7 +345,7 @@ public function testReleaseThrowsExceptionIfNotWellDeleted()
public function testReleaseThrowsAndLog()
{
$this->expectException(LockReleasingException::class);
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$logger = $this->createMock(LoggerInterface::class);
$lock = new Lock($key, $store, 10, true);
Expand Down Expand Up @@ -402,7 +402,7 @@ public function logs(): array
*/
public function testExpiration($ttls, $expected)
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -421,7 +421,7 @@ public function testExpiration($ttls, $expected)
*/
public function testExpirationStoreInterface($ttls, $expected)
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store, 10);

Expand All @@ -448,7 +448,7 @@ public static function provideExpiredDates()

public function testAcquireReadNoBlockingWithSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(SharedLockStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -467,7 +467,7 @@ public function testAcquireReadNoBlockingWithSharedLockStoreInterface()
*/
public function testAcquireReadTwiceWithExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = new class() implements PersistingStoreInterface {
use ExpiringStoreTrait;
private array $keys = [];
Expand Down Expand Up @@ -511,7 +511,7 @@ public function putOffExpiration(Key $key, $ttl): void
*/
public function testAcquireTwiceWithExpiration()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = new class() implements PersistingStoreInterface {
use ExpiringStoreTrait;
private array $keys = [];
Expand Down Expand Up @@ -552,7 +552,7 @@ public function putOffExpiration(Key $key, $ttl): void

public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(BlockingSharedLockStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -568,7 +568,7 @@ public function testAcquireReadBlockingWithBlockingSharedLockStoreInterface()

public function testAcquireReadBlockingWithSharedLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(SharedLockStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -590,7 +590,7 @@ public function testAcquireReadBlockingWithSharedLockStoreInterface()

public function testAcquireReadBlockingWithBlockingLockStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(BlockingStoreInterface::class);
$lock = new Lock($key, $store);

Expand All @@ -606,7 +606,7 @@ public function testAcquireReadBlockingWithBlockingLockStoreInterface()

public function testAcquireReadBlockingWithPersistingStoreInterface()
{
$key = new Key(uniqid(__METHOD__, true));
$key = new Key(__METHOD__);
$store = $this->createMock(PersistingStoreInterface::class);
$lock = new Lock($key, $store);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,8 @@ public function getStore(): PersistingStoreInterface

public function testBackwardCompatibility()
{
$resource = uniqid(__METHOD__, true);
$key1 = new Key($resource);
$key2 = new Key($resource);
$key1 = new Key(__METHOD__);
$key2 = new Key(__METHOD__);

$oldStore = new Symfony51Store($this->getRedisConnection());
$newStore = $this->getStore();
Expand Down
Loading
Loading