Completed
Push — master ( 0ab6a2...d8ddb3 )
by Robbie
01:17
created

ChangeGroupsCommandTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 99
rs 10
c 1
b 0
f 0

6 Methods

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