DebugCommandTest::testCanOutputAsTable()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\Console\Tests\Command\Object;
4
5
use SilverLeague\Console\Tests\Command\AbstractCommandTest;
6
use SilverStripe\Security\Member;
7
use Symfony\Component\Console\Tester\CommandTester;
8
9
/**
10
 * @coversDefaultClass \SilverLeague\Console\Command\Object\DebugCommand
11
 * @package silverstripe-console
12
 * @author  Robbie Averill <[email protected]>
13
 */
14
class DebugCommandTest extends AbstractCommandTest
15
{
16
    /**
17
     * @var Member
18
     */
19
    protected $member;
20
21
    /**
22
     * Create a Member to play with
23
     */
24 View Code Duplication
    protected function setUp()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
25
    {
26
        parent::setUp();
27
28
        $member = Member::create();
29
        $member->Email = '[email protected]';
30
        $member->Password = 'NotRelevant1';
31
        $member->write();
32
33
        $this->member = $member;
34
    }
35
36
    /**
37
     * Delete fixtured members after tests have run
38
     */
39
    protected function tearDown()
40
    {
41
        parent::tearDown();
42
43
        $this->member->delete();
44
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49
    protected function getTestCommand()
50
    {
51
        return 'object:debug';
52
    }
53
54
    /**
55
     * Test that when an invalid class name (non existent or not a DataObject) is given that an error is returned
56
     */
57
    public function testErrorWhenInvalidClassNameProvided()
58
    {
59
        $tester = $this->executeTest(['object' => "A\B\C\D\E\F\G"]);
60
        $this->assertContains('does not exist', $tester->getDisplay());
61
62
        $tester = $this->executeTest(['object' => "SilverStripe\Core\Object"]);
63
        $this->assertContains('is not a DataObject', $tester->getDisplay());
64
    }
65
66
    /**
67
     * Test that when all required arguments are provided and valid the data is retrieved and output directly
68
     */
69
    public function testGetDataImmediatelyWhenAllArgumentsProvidedAndSortingIsApplied()
70
    {
71
        $tester = $this->executeTest(['object' => Member::class, 'id' => $this->member->ID]);
72
        $display = $tester->getDisplay();
73
        $this->assertContains($this->member->Email, $display);
74
75
        // Should be identical
76
        $tester2 = $this->executeTest(['object' => Member::class, 'id' => $this->member->ID]);
77
        $this->assertSame($display, $tester2->getDisplay());
78
79
        // Should be different
80
        $tester3 = $this->executeTest(['object' => Member::class, 'id' => $this->member->ID, '--no-sort' => true]);
81
        $this->assertNotSame($display, $tester3->getDisplay());
82
    }
83
84
    /**
85
     * Simuluating the interactive input, test that we can lookup a model by a column and a value. Also test
86
     * that password columns are not returned
87
     */
88
    public function testSearchMemberByColumn()
89
    {
90
        $tester = new CommandTester($this->command);
91
92
        // Simulate interactive inputs
93
        $tester->setInputs(['Email', $this->member->Email]);
94
95
        $tester->execute(['object' => Member::class]);
96
        $display = $tester->getDisplay();
97
        $this->assertContains($this->member->Email, $display);
98
99
        // Check there's no passwords in the output
100
        $this->assertNotContains('Password', $display);
101
    }
102
103
    /**
104
     * Ensure that table output can be returned if the option is provided
105
     */
106
    public function testCanOutputAsTable()
107
    {
108
        $tester = $this->executeTest(['object' => Member::class, 'id' => $this->member->ID, '--output-table' => true]);
109
110
        $this->assertContains('+--------', $tester->getDisplay());
111
    }
112
113
    /**
114
     * Test that generic info output is provided about the class and ID
115
     */
116
    public function testGenericOutputIsGivenAboutContext()
117
    {
118
        $tester = $this->executeTest(['object' => Member::class, 'id' => $this->member->ID]);
119
120
        $display = $tester->getDisplay();
121
        $this->assertContains('Object: ' . Member::class, $display);
122
        $this->assertContains('ID: ' . $this->member->ID, $display);
123
    }
124
}
125