Completed
Push — master ( 3e963f...fbd0cf )
by Yann
02:04
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())
0 ignored issues
show
Documentation introduced by
\Prophecy\Argument::any() is of type object<Prophecy\Argument\Token\AnyValueToken>, but the function expects a null|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method archive does only exist in Yokai\SecurityTokenBundl...hive\ArchivistInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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)
0 ignored issues
show
Bug introduced by
The method archive does only exist in Yokai\SecurityTokenBundl...hive\ArchivistInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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)
0 ignored issues
show
Documentation introduced by
$dateAssertions is of type object<Prophecy\Argument\Token\LogicalAndToken>, but the function expects a null|object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Bug introduced by
The method archive does only exist in Yokai\SecurityTokenBundl...hive\ArchivistInterface, but not in Prophecy\Prophecy\ObjectProphecy.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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