Completed
Push — master ( d30f73...69c390 )
by Loïc
03:06
created

InstallerContext::iRunInstallDataCommandLine()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of UnivNantes.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AppBundle\Behat\Command;
13
14
use AppBundle\Command\Installer\InstallDatabaseCommand;
15
use AppBundle\Command\Installer\SetupCommand;
16
use Symfony\Bundle\FrameworkBundle\Console\Application;
17
use Symfony\Component\Console\Tester\CommandTester;
18
19
/**
20
 * @author Loïc Frémont <[email protected]>
21
 */
22
class InstallerContext extends DefaultContext
23
{
24
    /**
25
     * @var array
26
     */
27
    private $inputChoices = [
28
        'e-mail' => '[email protected]',
29
        'password' => 'pswd',
30
        'confirmation' => 'pswd',
31
    ];
32
33
    /**
34
     * @Given I do not provide an email
35
     */
36
    public function iDoNotProvideEmail()
37
    {
38
        $this->inputChoices['e-mail'] = '';
39
    }
40
41
    /**
42
     * @Given I do not provide a correct email
43
     */
44
    public function iDoNotProvideCorrectEmail()
45
    {
46
        $this->inputChoices['e-mail'] = 'janusz';
47
    }
48
49
    /**
50
     * @Given I provide full administrator data
51
     */
52
    public function iProvideFullAdministratorData()
53
    {
54
        $this->inputChoices['e-mail'] = '[email protected]';
55
        $this->inputChoices['password'] = 'pswd1$';
56
        $this->inputChoices['confirmation'] = $this->inputChoices['password'];
57
    }
58
59
    /**
60
     * @param string $name
61
     */
62
    private function iExecuteCommandWithInputChoices($name)
63
    {
64
        $this->questionHelper = $this->command->getHelper('question');
65
        $inputString = implode(PHP_EOL, $this->inputChoices);
66
        $this->questionHelper->setInputStream($this->getInputStream($inputString.PHP_EOL));
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Symfony\Component\Console\Helper\HelperInterface as the method setInputStream() does only exist in the following implementations of said interface: Sensio\Bundle\GeneratorB...d\Helper\QuestionHelper, Symfony\Component\Console\Helper\QuestionHelper, Symfony\Component\Consol...r\SymfonyQuestionHelper.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
67
68
        try {
69
            $this->getTester()->execute(['command' => $name]);
70
        } catch (\Exception $e) {
71
        }
72
    }
73
74
    /**
75
     * @When /^I run Install setup command$/
76
     */
77
    public function iRunInstallSetupCommmandLine()
78
    {
79
        $this->application = new Application($this->kernel);
80
        $this->application->add(new SetupCommand());
81
82
        $this->command = $this->application->find('app:install:setup');
83
        $this->setTester(new CommandTester($this->command));
84
85
        $this->iExecuteCommandWithInputChoices('app:install:setup');
86
    }
87
88
    /**
89
     * @When /^I run Install database command$/
90
     */
91
    public function iRunInstallDatabaseCommmandLine()
92
    {
93
        $this->application = new Application($this->kernel);
94
        $this->application->add(new InstallDatabaseCommand());
95
96
        $this->command = $this->application->find('app:install:database');
97
        $this->setTester(new CommandTester($this->command));
98
99
        $this->iExecuteCommandAndConfirm('app:install:database');
100
    }
101
}
102