-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserLoginCommandTest.php
More file actions
91 lines (75 loc) · 3.29 KB
/
Copy pathUserLoginCommandTest.php
File metadata and controls
91 lines (75 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
namespace ItkDev\OpenIdConnectBundle\Tests\Command;
use ItkDev\OpenIdConnectBundle\Command\UserLoginCommand;
use ItkDev\OpenIdConnectBundle\Util\CliLoginHelper;
use PHPUnit\Framework\MockObject\Stub;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
class UserLoginCommandTest extends TestCase
{
/** @var CliLoginHelper&Stub */
private CliLoginHelper $stubCliLoginHelper;
/** @var UrlGeneratorInterface&Stub */
private UrlGeneratorInterface $stubUrlGenerator;
/** @var UserProviderInterface&Stub */
private UserProviderInterface $stubUserProvider;
private UserLoginCommand $command;
protected function setUp(): void
{
$this->stubCliLoginHelper = $this->createStub(CliLoginHelper::class);
$this->stubUrlGenerator = $this->createStub(UrlGeneratorInterface::class);
$this->stubUserProvider = $this->createStub(UserProviderInterface::class);
$this->command = new UserLoginCommand(
$this->stubCliLoginHelper,
'cli_login_route',
$this->stubUrlGenerator,
$this->stubUserProvider
);
}
public function testExecuteSuccess(): void
{
$this->stubCliLoginHelper
->method('createToken')
->willReturn('generated-token');
$this->stubUrlGenerator
->method('generate')
->willReturn('https://app.example.org/login?loginToken=generated-token');
$tester = new CommandTester($this->command);
$result = $tester->execute(['username' => 'testuser']);
$this->assertSame(Command::SUCCESS, $result);
$this->assertStringContainsString('https://app.example.org/login?loginToken=generated-token', $tester->getDisplay());
}
public function testExecutePassesTokenAndRouteToUrlGenerator(): void
{
$this->stubCliLoginHelper
->method('createToken')
->willReturn('generated-token');
$urlGenerator = $this->createMock(UrlGeneratorInterface::class);
$urlGenerator->expects($this->once())
->method('generate')
->with('cli_login_route', ['loginToken' => 'generated-token'], UrlGeneratorInterface::ABSOLUTE_URL)
->willReturn('https://app.example.org/login?loginToken=generated-token');
$command = new UserLoginCommand(
$this->stubCliLoginHelper,
'cli_login_route',
$urlGenerator,
$this->stubUserProvider
);
$tester = new CommandTester($command);
$this->assertSame(Command::SUCCESS, $tester->execute(['username' => 'testuser']));
}
public function testExecuteUserNotFound(): void
{
$this->stubUserProvider
->method('loadUserByIdentifier')
->willThrowException(new UserNotFoundException());
$tester = new CommandTester($this->command);
$result = $tester->execute(['username' => 'nonexistent']);
$this->assertSame(Command::FAILURE, $result);
$this->assertStringContainsString('User does not exist', $tester->getDisplay());
}
}