Skip to content

Commit

Permalink
Merge pull request #154 from kbond/fixes
Browse files Browse the repository at this point in the history
Fixes
  • Loading branch information
kbond authored Oct 9, 2024
2 parents 3dd63db + d3a52e9 commit 49881ed
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 85 deletions.
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,6 @@ $browser
// authenticate a user for subsequent actions
->actingAs($user) // \Symfony\Component\Security\Core\User\UserInterface

// If using zenstruck/foundry, you can pass a factory/proxy
->actingAs(UserFactory::new())

// fail if authenticated
->assertNotAuthenticated()

Expand All @@ -332,8 +329,7 @@ $browser
// fails if NOT authenticated as "kbond"
->assertAuthenticated('kbond')

// \Symfony\Component\Security\Core\User\UserInterface or, if using
// zenstruck/foundry, you can pass a factory/proxy
// \Symfony\Component\Security\Core\User\UserInterface
->assertAuthenticated($user)
;
```
Expand Down
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@
},
"require-dev": {
"dbrekelmans/bdi": "^1.0",
"justinrainbow/json-schema": "^5.2",
"justinrainbow/json-schema": "^5.3",
"mtdowling/jmespath.php": "^2.6",
"phpstan/phpstan": "^1.4",
"phpunit/phpunit": "^9.5|^10.4",
"phpunit/phpunit": "^9.6.21|^10.4",
"symfony/mime": "^5.4|^6.0|^7.0",
"symfony/panther": "^1.1|^2.0.1",
"symfony/phpunit-bridge": "^6.0|^7.0",
"symfony/security-bundle": "^5.4|^6.0|^7.0",
"zenstruck/foundry": "^1.30"
"symfony/security-bundle": "^5.4|^6.0|^7.0"
},
"suggest": {
"justinrainbow/json-schema": "Json schema validator. Needed to use Json::assertMatchesSchema().",
Expand Down
24 changes: 14 additions & 10 deletions src/Browser/KernelBrowser.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,20 @@ final public function withProfiling(): self
}

/**
* @param UserInterface|Proxy<UserInterface>|Factory<UserInterface> $user
* @param UserInterface $user
*
* @return static
*/
public function actingAs(object $user, ?string $firewall = null): self
{
if ($user instanceof Factory) {
$user = $user->create();
if ($user instanceof Factory) { // @phpstan-ignore-line
trigger_deprecation('zenstruck/browser', '1.9', 'Passing a Factory to actingAs() is deprecated, pass the created object instead.');
$user = $user->create(); // @phpstan-ignore-line
}

if ($user instanceof Proxy) {
$user = $user->object();
if ($user instanceof Proxy) { // @phpstan-ignore-line
trigger_deprecation('zenstruck/browser', '1.9', 'Passing a Proxy to actingAs() is deprecated, pass the real object instead.');
$user = $user->object(); // @phpstan-ignore-line
}

if (!$user instanceof UserInterface) {
Expand All @@ -151,7 +153,7 @@ public function actingAs(object $user, ?string $firewall = null): self
}

/**
* @param string|UserInterface|Proxy<UserInterface>|Factory<UserInterface>|null $as
* @param string|UserInterface|null $as
*
* @return static
*/
Expand All @@ -171,12 +173,14 @@ public function assertAuthenticated($as = null): self
return $this;
}

if ($as instanceof Factory) {
$as = $as->create();
if ($as instanceof Factory) { // @phpstan-ignore-line
trigger_deprecation('zenstruck/browser', '1.9', 'Passing a Factory to assertAuthenticated() is deprecated, pass the created object instead.');
$as = $as->create(); // @phpstan-ignore-line
}

if ($as instanceof Proxy) {
$as = $as->object();
if ($as instanceof Proxy) { // @phpstan-ignore-line
trigger_deprecation('zenstruck/browser', '1.9', 'Passing a Proxy to assertAuthenticated() is deprecated, pass the real object instead.');
$as = $as->object(); // @phpstan-ignore-line
}

if ($as instanceof UserInterface) {
Expand Down
2 changes: 1 addition & 1 deletion src/Browser/Test/LegacyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private static function normalizeTestName(string $name): string
\preg_match('#^(?<test>[\w:\\\]+) with data set "(?<dataset>.*)"#', $name, $matches);
}

$normalized = \strtr($matches['test'], '\\:', '-_');
$normalized = \strtr($matches['test'], '\\:', '-_'); // @phpstan-ignore-line

if (isset($matches['dataset'])) {
$normalized .= '__data-set-'.\preg_replace('/\W+/', '-', $matches['dataset']);
Expand Down
5 changes: 0 additions & 5 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Zenstruck\Foundry\ZenstruckFoundryBundle;

/**
* @author Kevin Bond <[email protected]>
Expand Down Expand Up @@ -173,7 +172,6 @@ public function registerBundles(): iterable
{
yield new FrameworkBundle();
yield new SecurityBundle();
yield new ZenstruckFoundryBundle();
}

protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader): void
Expand Down Expand Up @@ -209,9 +207,6 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
}

$c->loadFromExtension('security', $security);
$c->loadFromExtension('zenstruck_foundry', [
'auto_refresh_proxies' => false,
]);
$c->register('logger', NullLogger::class); // disable logging
}

Expand Down
39 changes: 1 addition & 38 deletions tests/KernelBrowserAuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,13 @@
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\InMemoryUser;
use Zenstruck\Browser\Test\HasBrowser;
use Zenstruck\Foundry\Test\Factories;

use function Zenstruck\Foundry\anonymous;

/**
* @author Kevin Bond <[email protected]>
*/
final class KernelBrowserAuthenticationTest extends KernelTestCase
{
use Factories, HasBrowser;
use HasBrowser;

/**
* @test
Expand All @@ -41,54 +38,20 @@ public function can_act_as_user(): void
;
}

/**
* @test
*/
public function can_act_as_user_with_foundry_factory(): void
{
$user = anonymous(InMemoryUser::class, ['username' => 'kevin', 'password' => 'pass']);

$this->browser()
->throwExceptions()
->actingAs($user)
->visit('/user')
->assertSee('user: kevin/pass')
;
}

/**
* @test
*/
public function can_act_as_user_with_foundry_proxy(): void
{
$user = anonymous(InMemoryUser::class)->create(['username' => 'kevin', 'password' => 'pass']);

$this->browser()
->throwExceptions()
->actingAs($user)
->visit('/user')
->assertSee('user: kevin/pass')
;
}

/**
* @test
*/
public function can_make_authentication_assertions(): void
{
$username = 'kevin';
$user = new InMemoryUser('kevin', 'pass');
$factory = anonymous(InMemoryUser::class, ['username' => 'kevin', 'password' => 'pass']);
$proxy = anonymous(InMemoryUser::class)->create(['username' => 'kevin', 'password' => 'pass']);

$this->browser()
->assertNotAuthenticated()
->actingAs($user)
->assertAuthenticated()
->assertAuthenticated($username)
->assertAuthenticated($user)
->assertAuthenticated($factory)
->assertAuthenticated($proxy)
->visit('/user')
->assertAuthenticated()
->assertAuthenticated($username)
Expand Down
44 changes: 22 additions & 22 deletions tests/NormalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public static function namesProvider(): \Generator
$baseTemplate = 'error_'.__METHOD__;

yield 'test name without datasets' => [
'test name' => __METHOD__,
'expected output' => \strtr($baseTemplate, '\\:', '-_').'__0',
'testName' => __METHOD__,
'expectedOutput' => \strtr($baseTemplate, '\\:', '-_').'__0',
];

$datasetTemplate = $baseTemplate.'__data-set-%s__0';
Expand All @@ -55,49 +55,49 @@ public static function namesProvider(): \Generator
$numericOutput = \strtr($numericTemplate, '\\:', '-_');

yield 'phpunit 10 alpha' => [
'test name' => __METHOD__.' with data set "test set"',
'expected output' => $alphaOutput,
'testName' => __METHOD__.' with data set "test set"',
'expectedOutput' => $alphaOutput,
];
yield 'phpunit 10 numeric' => [
'test name' => __METHOD__.' with data set #0',
'expected output' => $numericOutput,
'testName' => __METHOD__.' with data set #0',
'expectedOutput' => $numericOutput,
];
yield 'legacy alpha' => [
'test name' => __METHOD__.' with data set "test set" (test set)',
'expected output' => $alphaOutput,
'testName' => __METHOD__.' with data set "test set" (test set)',
'expectedOutput' => $alphaOutput,
];
yield 'legacy numeric' => [
'test name' => __METHOD__.' with data set #0 (test set)',
'expected output' => $numericOutput,
'testName' => __METHOD__.' with data set #0 (test set)',
'expectedOutput' => $numericOutput,
];
}

public static function edgeCaseTestNames(): \Generator
{
$baseTemplate = \strtr('error_'.__METHOD__.'__data-set-', '\\:', '-_');
yield 'self within moustache' => [
'test name' => __METHOD__.' with data set "te{{self}}st" (test set)',
'expected output' => $baseTemplate.'te-self-st__0',
'testName' => __METHOD__.' with data set "te{{self}}st" (test set)',
'expectedOutput' => $baseTemplate.'te-self-st__0',
];
yield 'double quoted with space' => [
'test name' => __METHOD__.' with data set "_self.env.setCache("uri://host.net:2121") _self.env.loadTemplate("other-host")" (test set)',
'expected output' => $baseTemplate.'_self-env-setCache-uri-host-net-2121-_self-env-loadTemplate-other-host-__0',
'testName' => __METHOD__.' with data set "_self.env.setCache("uri://host.net:2121") _self.env.loadTemplate("other-host")" (test set)',
'expectedOutput' => $baseTemplate.'_self-env-setCache-uri-host-net-2121-_self-env-loadTemplate-other-host-__0',
];
yield 'double quotes in moustache' => [
'test name' => __METHOD__.' with data set "te{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}st"',
'expected output' => $baseTemplate.'te-_self-env-registerUndefinedFilterCallback-exec-_self-env-getFilter-id-st__0',
'testName' => __METHOD__.' with data set "te{{_self.env.registerUndefinedFilterCallback("exec")}}{{_self.env.getFilter("id")}}st"',
'expectedOutput' => $baseTemplate.'te-_self-env-registerUndefinedFilterCallback-exec-_self-env-getFilter-id-st__0',
];
yield 'escaped simple quote' => [
'test name' => __METHOD__.' with data set "te{{\'/etc/passwd\'|file_excerpt(1,30)}}st"',
'expected output' => $baseTemplate.'te-etc-passwd-file_excerpt-1-30-st__0',
'testName' => __METHOD__.' with data set "te{{\'/etc/passwd\'|file_excerpt(1,30)}}st"',
'expectedOutput' => $baseTemplate.'te-etc-passwd-file_excerpt-1-30-st__0',
];
yield 'single quote for array index access' => [
'test name' => __METHOD__.' with data set "te{{[\'id\']|filter(\'system\')}}st"',
'expected output' => $baseTemplate.'te-id-filter-system-st__0',
'testName' => __METHOD__.' with data set "te{{[\'id\']|filter(\'system\')}}st"',
'expectedOutput' => $baseTemplate.'te-id-filter-system-st__0',
];
yield 'numeric array access' => [
'test name' => __METHOD__.' with data set "te{{[0]|reduce(\'system\',\'id\')}}st"',
'expected output' => $baseTemplate.'te-0-reduce-system-id-st__0',
'testName' => __METHOD__.' with data set "te{{[0]|reduce(\'system\',\'id\')}}st"',
'expectedOutput' => $baseTemplate.'te-0-reduce-system-id-st__0',
];
}
}

0 comments on commit 49881ed

Please sign in to comment.