Completed
Push — develop ( 2a440e...d14017 )
by Christian
02:31
created

CreateCommandTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 99
Duplicated Lines 7.07 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 5
dl 7
loc 99
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testExecute() 0 22 1
A testExecuteAdditionalFields() 0 23 1
A getWebsiteCode() 7 7 1
A testWithWrongPassword() 0 38 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace N98\Magento\Command\Customer;
4
5
use Magento\Store\Model\StoreManagerInterface;
6
use N98\Magento\Command\TestCase;
7
use Symfony\Component\Console\Tester\CommandTester;
8
9
/**
10
 * Class CreateCommandTest
11
 * @package N98\Magento\Command\Customer
12
 */
13
class CreateCommandTest extends TestCase
14
{
15
    public function testExecute()
16
    {
17
        $generatedEmail = uniqid('', true) . '@example.com';
18
19
        $input = [
20
            'command'   => 'customer:create',
21
            'email'     => $generatedEmail,
22
            'password'  => 'Password123',
23
            'firstname' => 'John',
24
            'lastname'  => 'Doe',
25
            'website'   => $this->getWebsiteCode(),
26
        ];
27
        $this->assertDisplayContains($input, 'successfully created');
28
29
        // Format option
30
        $generatedEmail = uniqid('', true) . '@example.com';
31
        $input['email'] = $generatedEmail;
32
        $input['--format'] = 'csv';
33
34
        $this->assertDisplayContains($input, 'email,password,firstname,lastname');
35
        $this->assertdisplayContains($input, $generatedEmail . ',Password123,John,Doe');
36
    }
37
38
    public function testExecuteAdditionalFields()
39
    {
40
        $generatedEmail = uniqid('', true) . '@example.com';
41
42
        $input = [
43
            'command'   => 'customer:create',
44
            'email'     => $generatedEmail,
45
            'password'  => 'Password123',
46
            'firstname' => 'John',
47
            'lastname'  => 'Doe',
48
            'website'   => $this->getWebsiteCode(),
49
            'additionalFields' => ['prefix', 'Mr']
50
        ];
51
        $this->assertDisplayContains($input, 'successfully created');
52
53
        // Format option
54
        $generatedEmail = uniqid('', true) . '@example.com';
55
        $input['email'] = $generatedEmail;
56
        $input['--format'] = 'csv';
57
58
        $this->assertDisplayContains($input, 'email,password,firstname,lastname');
59
        $this->assertdisplayContains($input, $generatedEmail . ',Password123,John,Doe');
60
    }
61
62
    /**
63
     * @return string
64
     */
65 View Code Duplication
    private function getWebsiteCode()
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...
66
    {
67
        $storeManager = $this->getApplication()->getObjectManager()->get(StoreManagerInterface::class);
68
        $website = $storeManager->getWebsite('base');
69
70
        return $website->getCode();
71
    }
72
73
    public function testWithWrongPassword()
74
    {
75
        $this->markTestIncomplete('We currently cannot deal with interactive commands');
76
77
        $application = $this->getApplication();
78
        $application->add(new CreateCommand());
79
80
        // try to create a customer with a password < 6 chars
81
        $command = $this->getApplication()->find('customer:create');
82
83
        $generatedEmail = uniqid('', true) . '@example.com';
84
85
        // mock dialog
86
        // We mock the DialogHelper
87
        $dialog = $this->createMock('N98\Util\Console\Helper\ParameterHelper', ['askPassword']);
0 ignored issues
show
Unused Code introduced by
The call to CreateCommandTest::createMock() has too many arguments starting with array('askPassword').

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
88
        $dialog->expects($this->at(0))
89
               ->method('askPassword')
90
               ->willReturn(true); // The user confirms
91
92
        // We override the standard helper with our mock
93
        $command->getHelperSet()->set($dialog, 'parameter');
94
95
        $options = [
96
            'command'   => $command->getName(),
97
            'email'     => $generatedEmail,
98
            'password'  => 'pass',
99
            'firstname' => 'John',
100
            'lastname'  => 'Doe',
101
            'website'   => 1
102
        ];
103
104
        $commandTester = new CommandTester($command);
105
        $commandTester->execute($options);
106
        $this->assertRegExp(
107
            '/The password must have at least 6 characters. Leading or trailing spaces will be ignored./',
108
            $commandTester->getDisplay()
109
        );
110
    }
111
}
112