ChangeGroupsCommandTest::mockQuestionHelper()   A
last analyzed

Complexity

Conditions 3
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 3
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Tests\Command\Member;
4
5
use SilverLeague\Console\Tests\Command\AbstractCommandTest;
6
use SilverStripe\Security\Member;
7
use Symfony\Component\Console\Helper\QuestionHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
use Symfony\Component\Console\Question\ChoiceQuestion;
11
12
/**
13
 * Change a member's assigned groups
14
 *
15
 * @coversDefaultClass \SilverLeague\Console\Command\Member\ChangeGroupsCommand
16
 * @package silverstripe-console
17
 * @author  Robbie Averill <[email protected]>
18
 */
19
class ChangeGroupsCommandTest extends AbstractCommandTest
20
{
21
    /**
22
     * Delete fixtured members after tests have run
23
     */
24
    protected function tearDown()
25
    {
26
        parent::tearDown();
27
28
        $testMember = Member::get()->filter(['Email' => '[email protected]'])->first();
29
        if ($testMember && $testMember->exists()) {
30
            $testMember->delete();
31
        }
32
    }
33
34
    /**
35
     * {@inheritDoc}
36
     */
37
    protected function getTestCommand()
38
    {
39
        return 'member:change-groups';
40
    }
41
42
    /**
43
     * Test that an error message is returned if the Member does not exist
44
     *
45
     * @covers ::execute
46
     */
47
    public function testReportMemberNotFound()
48
    {
49
        $tester = $this->executeTest(['email' => '[email protected]']);
50
        $this->assertContains('Member with email "[email protected]" was not found', $tester->getDisplay());
51
    }
52
53
    /**
54
     * Test that when a Group is chosen from the multiselect list, the user is assigned to that Group or Groups
55
     *
56
     * @covers ::execute
57
     */
58
    public function testAddToGroups()
59
    {
60
        $member = $this->createMember();
0 ignored issues
show
Unused Code introduced by
$member is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
61
62
        $this->mockQuestionHelper();
63
        $tester = $this->executeTest(['email' => '[email protected]']);
64
        $this->assertContains('Groups updated.', $tester->getDisplay());
65
66
        $memberCodes = Member::get()
67
            ->filter('Email', '[email protected]')
68
            ->first()
69
            ->Groups()
70
            ->column('Code');
71
72
        $this->assertSame(['content-authors'], $memberCodes);
73
    }
74
75
    /**
76
     * Creates a dummy user for testing with
77
     *
78
     * @return Member
79
     */
80
    protected function createMember()
81
    {
82
        $member = Member::create();
83
        $member->Email = '[email protected]';
84
        $member->Password = 'Opensesame1';
85
        $member->write();
86
        return $member;
87
    }
88
89
    /**
90
     * Mock a QuestionHelper and tell it to return a predefined choice for which Group to assign
91
     *
92
     * @return QuestionHelper
93
     */
94
    protected function mockQuestionHelper()
95
    {
96
        $mock = $this
97
            ->getMockBuilder(QuestionHelper::class)
98
            ->setMethods(['ask'])
99
            ->getMock();
100
101
        $mock
102
            ->expects($this->once())
103
            ->method('ask')
104
            ->with(
105
                $this->isInstanceOf(InputInterface::class),
106
                $this->isInstanceOf(OutputInterface::class),
107
                $this->callback(
108
                    function ($argument) {
109
                        return $argument instanceof ChoiceQuestion
110
                            && $argument->isMultiselect()
111
                            && !empty($argument->getChoices());
112
                    }
113
                )
114
            )
115
            ->willReturn(['content-authors']);
116
117
        $this->command->getApplication()->getHelperSet()->set($mock, 'question');
118
    }
119
}
120