Completed
Push — master ( b8b393...c56780 )
by GBProd
02:34
created

ProvidingProgressBarTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 114
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 8
dl 38
loc 114
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testOnStartedHandlingDisplayNumberOfEntries() 0 15 1
A testOnStartedProvidingDisplayProviderName() 0 18 1
A testOnStartedProvidingCreateProgressBar() 0 21 1
A testOnIndexedDocumentAdvanceProgress() 18 18 1
A testOnFinishedProvidingFinishProgress() 20 20 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 ElasticsearchDataProviderBundle\Command;
4
5
use GBProd\ElasticsearchDataProviderBundle\Command\ProvidingProgressBar;
6
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProviderInterface;
7
use GBProd\ElasticsearchDataProviderBundle\DataProvider\RegistryEntry;
8
use GBProd\ElasticsearchDataProviderBundle\Event\HasFinishedProviding;
9
use GBProd\ElasticsearchDataProviderBundle\Event\HasIndexedDocument;
10
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedHandling;
11
use GBProd\ElasticsearchDataProviderBundle\Event\HasStartedProviding;
12
use Symfony\Component\Console\Helper\ProgressBar;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
/**
17
 * Tests for ProvidingProgressBar
18
 *
19
 * @author gbprod <[email protected]>
20
 */
21
class ProvidingProgressBarTest extends \PHPUnit_Framework_TestCase
22
{
23
    private $testedInstance;
24
    private $dispatcher;
25
    private $output;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
26
27
    public function setUp()
28
    {
29
        $this->dispatcher = $this->getMock(EventDispatcherInterface::class);
30
        $this->output     = $this->getMock(OutputInterface::class);
31
32
        $this->testedInstance = new ProvidingProgressBar(
33
            $this->dispatcher,
34
            $this->output
35
        );
36
    }
37
38
    public function testOnStartedHandlingDisplayNumberOfEntries()
39
    {
40
        $event = new HasStartedHandling([1, 2, 3]);
0 ignored issues
show
Documentation introduced by
array(1, 2, 3) is of type array<integer,integer,{"...nteger","2":"integer"}>, but the function expects a array<integer,object<GBP...e\Event\RegistryEntry>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
41
42
        $this
43
            ->output
44
            ->expects($this->once())
45
            ->method('writeln')
46
            ->with(
47
                $this->stringContains('<comment>3</comment>')
48
            )
49
        ;
50
51
        $this->testedInstance->onStartedHandling($event);
52
    }
53
54
    public function testOnStartedProvidingDisplayProviderName()
55
    {
56
        $provider = $this->getMock(DataProviderInterface::class);
57
        $entry = new RegistryEntry($provider, 'my_index', 'my_type');
58
59
        $event = new HasStartedProviding($entry);
60
61
        $this
62
            ->output
63
            ->expects($this->once())
64
            ->method('writeln')
65
            ->with($this->stringContains(get_class($provider)))
66
        ;
67
68
        $this->testedInstance->onStartedProviding($event);
69
70
        $this->assertEmpty($this->testedInstance->progressBar);
71
    }
72
73
    public function testOnStartedProvidingCreateProgressBar()
74
    {
75
        $provider = $this->getMock(DataProviderInterface::class);
76
        $provider
77
            ->expects($this->any())
78
            ->method('count')
79
            ->willReturn(42)
80
        ;
81
82
        $entry = new RegistryEntry($provider, 'my_index', 'my_type');
83
        $event = new HasStartedProviding($entry);
84
85
        $this->testedInstance->onStartedProviding($event);
86
87
        $this->assertNotEmpty($this->testedInstance->progressBar);
88
        $this->assertInstanceOf(
89
            ProgressBar::class,
90
            $this->testedInstance->progressBar
91
        );
92
        $this->assertEquals(42, $this->testedInstance->progressBar->getMaxSteps());
93
    }
94
95 View Code Duplication
    public function testOnIndexedDocumentAdvanceProgress()
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...
96
    {
97
        $provider = $this->getMock(DataProviderInterface::class);
98
        $entry = new RegistryEntry($provider, 'my_index', 'my_type');
99
        $event = new HasIndexedDocument($entry);
0 ignored issues
show
Documentation introduced by
$entry is of type object<GBProd\Elasticsea...Provider\RegistryEntry>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
100
101
        $this->testedInstance->progressBar = $this
102
            ->getMock(ProgressBar::class, [], [$this->output])
103
        ;
104
105
        $this->testedInstance
106
            ->progressBar
107
            ->expects($this->once())
108
            ->method('advance')
109
        ;
110
111
        $this->testedInstance->onIndexedDocument($event);
112
    }
113
114 View Code Duplication
    public function testOnFinishedProvidingFinishProgress()
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...
115
    {
116
        $provider = $this->getMock(DataProviderInterface::class);
117
        $entry = new RegistryEntry($provider, 'my_index', 'my_type');
118
        $event = new HasFinishedProviding($entry);
119
120
        $this->testedInstance->progressBar = $this
121
            ->getMock(ProgressBar::class, [], [$this->output])
122
        ;
123
124
        $this->testedInstance
125
            ->progressBar
126
            ->expects($this->once())
127
            ->method('finish')
128
        ;
129
130
        $this->testedInstance->onFinishedProviding($event);
131
132
        $this->assertEmpty($this->testedInstance->progressBar);
133
    }
134
}
135