Completed
Push — master ( 3f58ef...2b3071 )
by Yann
02:01
created

ArchiveTokenCommandTest::runCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
namespace Yokai\SecurityTokenBundle\Tests\Command;
4
5
use Prophecy\Argument;
6
use Prophecy\Prophecy\ObjectProphecy;
7
use Symfony\Bundle\FrameworkBundle\Console\Application;
8
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Helper\QuestionHelper;
11
use Symfony\Component\Console\Tester\CommandTester;
12
use Yokai\SecurityTokenBundle\Archive\ArchivistInterface;
13
14
/**
15
 * @author Yann Eugoné <[email protected]>
16
 */
17
class ArchiveTokenCommandTest extends KernelTestCase
18
{
19
    /**
20
     * @var ArchivistInterface|ObjectProphecy
21
     */
22
    private $archivist;
23
24
    /**
25
     * @var Application
26
     */
27
    private $application;
28
29
    protected function setUp()
30
    {
31
        $this->archivist = $this->prophesize(ArchivistInterface::class);
32
33
        self::bootKernel();
34
        self::$kernel->getContainer()->set('yokai_security_token.archivist', $this->archivist->reveal());
35
36
        $this->application = new Application(self::$kernel);
37
    }
38
39
    protected function tearDown()
40
    {
41
        parent::tearDown();
42
43
        unset(
44
            $this->archivist,
45
            $this->application
46
        );
47
    }
48
49
    protected function command()
50
    {
51
        return $this->application->get('yokai:security-token:archive');
52
    }
53
54
    protected function runCommand(Command $command, array $options = [])
55
    {
56
        $input = ['command' => $command->getName()];
57
        foreach ($options as $name => $value) {
58
            $input['--'.$name] = $value;
59
        }
60
        $tester = new CommandTester($command);
61
        $tester->execute($input);
62
63
        return $tester->getDisplay();
64
    }
65
66
    /**
67
     * @test
68
     */
69
    public function it_archive_no_token_when_run_without_options_without_confirmation()
70
    {
71
        $command = $this->command();
72
73
        $question = $this->createMock(QuestionHelper::class);
74
        $question->expects($this->once())
75
            ->method('ask')
76
            ->willReturn(false);
77
78
        $command->getHelperSet()->set($question, 'question');
79
80
        $this->archivist->archive(Argument::any(), Argument::any())
81
            ->shouldNotBeCalled();
82
83
        $this->runCommand($command);
84
    }
85
86
    /**
87
     * @test
88
     */
89
    public function it_archive_every_token_when_run_without_options_with_confirmation()
90
    {
91
        $command = $this->command();
92
93
        $question = $this->createMock(QuestionHelper::class);
94
        $question->expects($this->once())
95
            ->method('ask')
96
            ->willReturn(true);
97
98
        $command->getHelperSet()->set($question, 'question');
99
100
        $this->archivist->archive(null, null)
101
            ->shouldBeCalledTimes(1)
102
            ->willReturn(10);
103
104
        $output = $this->runCommand($command);
105
106
        self::assertContains('Successfully archived 10 security token(s).', $output);
107
    }
108
109
    /**
110
     * @test
111
     */
112
    public function it_archive_partial_tokens_when_run_with_options()
113
    {
114
        $command = $this->command();
115
116
        $question = $this->createMock(QuestionHelper::class);
117
        $question->expects($this->never())
118
            ->method('ask');
119
120
        $command->getHelperSet()->set($question, 'question');
121
122
        $dateAssertions = Argument::allOf(
123
            Argument::type(\DateTime::class),
124
            Argument::that(function (\DateTime $date) {
125
                return $date->format('Y') === (string) (date('Y') - 1);
126
            })
127
        );
128
129
        $this->archivist->archive('init_password', $dateAssertions)
130
            ->shouldBeCalledTimes(1)
131
            ->willReturn(10);
132
133
        $output = $this->runCommand($command, ['purpose' => 'init_password', 'before' => '1 year']);
134
135
        self::assertContains('Successfully archived 10 security token(s).', $output);
136
    }
137
}
138