InitHandlerTest   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 10.96 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 8 8 1
A testExecute() 0 21 2
A executeProvider() 0 7 1
A testExecuteExitsEarlyIfFileExists() 0 14 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
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace BaleenTest\Cli\CommandBus\Config;
21
22
use Baleen\Cli\CommandBus\Config\InitMessage;
23
use Baleen\Cli\CommandBus\Config\InitHandler;
24
use Baleen\Cli\Config\Config;
25
use Baleen\Cli\Config\ConfigStorage;
26
use BaleenTest\Cli\CommandBus\HandlerTestCase;
27
use Mockery as m;
28
29
/**
30
 * Class InitHandlerTest
31
 * @author Gabriel Somoza <[email protected]>
32
 */
33
class InitHandlerTest extends HandlerTestCase
34
{
35
36
    /** @var m\Mock|ConfigStorage */
37
    protected $configStorage;
38
39
    /**
40
     * setUp
41
     */
42 View Code Duplication
    public function setUp()
43
    {
44
        $this->instance = m::mock(InitHandler::class)->makePartial();
45
        $this->command = m::mock(InitMessage::class)->makePartial();
46
        $this->configStorage = m::mock(ConfigStorage::class);
47
        $this->command->shouldReceive('getConfigStorage')->zeroOrMoreTimes()->andReturn($this->configStorage);
48
        parent::setUp();
49
    }
50
51
    /**
52
     * @param $writeResult
53
     * @dataProvider executeProvider
54
     */
55
    public function testExecute($writeResult)
56
    {
57
        $configFileName = '.baleen.yml';
58
59
        /** @var m\Mock|Config $config */
60
        $config = m::mock(Config::class);
61
62
        $this->command->setConfig($config);
63
64
        $resultMessage = $writeResult ? 'created at' : 'Could not create';
65
        $this->output->shouldReceive('writeln')->with(m::on(function($message) use ($resultMessage) {
66
            return strpos($message, $resultMessage) !== false;
67
        }))->once();
68
69
70
        $config->shouldReceive('getFileName')->once()->andReturn($configFileName);
71
        $this->configStorage->shouldReceive('isInitialized')->once()->andReturn(false);
72
        $this->configStorage->shouldReceive('write')->once()->andReturn($writeResult);
73
74
        $this->handle();
75
    }
76
77
    /**
78
     * @return array
79
     */
80
    public function executeProvider()
81
    {
82
        return [
83
            [ true ],
84
            [ false ],
85
        ];
86
    }
87
88
    /**
89
     * testExecuteExitsEarlyIfFileExists
90
     */
91
    public function testExecuteExitsEarlyIfFileExists()
92
    {
93
        /** @var m\Mock|Config $config */
94
        $config = m::mock(Config::class);
95
        $this->command->setConfig($config);
96
        $this->command->shouldReceive('getCliCommand->getApplication->getName')->andReturn('Baleen');
97
98
        $this->output->shouldReceive('writeln')->with('/already initiali[zs]ed/')->once();
99
        $config->shouldNotReceive('write');
100
101
        $this->configStorage->shouldReceive('isInitialized')->once()->andReturn(true);
102
103
        $this->handle();
104
    }
105
}
106