Passed
Push — master ( 972297...322a58 )
by Robbie
02:26
created

ChangeGroupsCommandTest::testAddToGroups()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 16
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 11
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
     * Ensure a clean slate for each test run
23
     *
24
     * {@inheritDoc}
25
     */
26
    public function setUp()
27
    {
28
        parent::setUp();
29
        Member::get()->removeAll();
30
    }
31
32
    /**
33
     * {@inheritDoc}
34
     */
35
    protected function getTestCommand()
36
    {
37
        return 'member:change-groups';
38
    }
39
40
    /**
41
     * Test that an error message is returned if the Member does not exist
42
     *
43
     * @covers ::execute
44
     */
45
    public function testReportMemberNotFound()
46
    {
47
        $tester = $this->executeTest(['email' => '[email protected]']);
48
        $this->assertContains('Member with email "[email protected]" was not found', $tester->getDisplay());
49
    }
50
51
    /**
52
     * Test that when a Group is chosen from the multiselect list, the user is assigned to that Group or Groups
53
     *
54
     * @covers ::execute
55
     */
56
    public function testAddToGroups()
57
    {
58
        $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...
59
60
        $this->mockQuestionHelper();
61
        $tester = $this->executeTest(['email' => '[email protected]']);
62
        $this->assertContains('Groups updated.', $tester->getDisplay());
63
64
        $memberCodes = Member::get()
65
            ->filter('Email', '[email protected]')
66
            ->first()
67
            ->Groups()
68
            ->column('Code');
69
70
        $this->assertSame(['content-authors'], $memberCodes);
71
    }
72
73
    /**
74
     * Creates a dummy user for testing with
75
     *
76
     * @return Member
77
     */
78
    protected function createMember()
79
    {
80
        $member = Member::create();
81
        $member->Email = '[email protected]';
82
        $member->Password = 'opensesame';
83
        $member->write();
84
        return $member;
85
    }
86
87
    /**
88
     * Mock a QuestionHelper and tell it to return a predefined choice for which Group to assign
89
     *
90
     * @return QuestionHelper
91
     */
92
    protected function mockQuestionHelper()
93
    {
94
        $mock = $this
95
            ->getMockBuilder(QuestionHelper::class)
96
            ->setMethods(['ask'])
97
            ->getMock();
98
99
        $mock
100
            ->expects($this->once())
101
            ->method('ask')
102
            ->with(
103
                $this->isInstanceOf(InputInterface::class),
104
                $this->isInstanceOf(OutputInterface::class),
105
                $this->callback(
106
                    function ($argument) {
107
                        return $argument instanceof ChoiceQuestion
108
                            && $argument->isMultiselect()
109
                            && !empty($argument->getChoices());
110
                    }
111
                )
112
            )
113
            ->willReturn(['content-authors']);
114
115
        $this->command->getApplication()->getHelperSet()->set($mock, 'question');
116
    }
117
}
118