Completed
Push — master ( 0a78d5...fce8e6 )
by GBProd
02:18
created

setProvressBarExpectsMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 1
1
<?php
2
3
namespace ElasticsearchDataProviderBundle\Command;
4
5
use GBProd\ElasticsearchDataProviderBundle\Command\ProvidingProgressBar;
6
use GBProd\ElasticsearchDataProviderBundle\DataProvider\DataProvider;
7
use GBProd\ElasticsearchDataProviderBundle\DataProvider\RegistryEntry;
8
use GBProd\ElasticsearchDataProviderBundle\Event\HasFinishedProviding;
9
use GBProd\ElasticsearchDataProviderBundle\Event\HasProvidedDocument;
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 $consoleOutput;
26
    private $provider;
27
    private $entry;
28
29
    public function setUp()
30
    {
31
        $this->dispatcher = $this->getMock(EventDispatcherInterface::class);
32
        $this->consoleOutput     = $this->getMock(OutputInterface::class);
33
34
        $this->testedInstance = new ProvidingProgressBar(
35
            $this->dispatcher,
36
            $this->consoleOutput
37
        );
38
39
        $this->provider = $this->getMock(DataProvider::class);
40
        $this->entry = new RegistryEntry($this->provider, 'my_index', 'my_type');
41
    }
42
43
    public function testOnStartedHandlingDisplayNumberOfEntries()
44
    {
45
        $event = new HasStartedHandling([
46
            $this->getMock(DataProvider::class),
47
            $this->getMock(DataProvider::class),
48
            $this->getMock(DataProvider::class),
49
        ]);
50
51
        $this
52
            ->consoleOutput
53
            ->expects($this->once())
54
            ->method('writeln')
55
            ->with(
56
                $this->stringContains('<comment>3</comment>')
57
            )
58
        ;
59
60
        $this->testedInstance->onStartedHandling($event);
61
    }
62
63
    public function testOnStartedProvidingDisplayProviderName()
64
    {
65
        $event = new HasStartedProviding($this->entry);
66
67
        $this
68
            ->consoleOutput
69
            ->expects($this->once())
70
            ->method('writeln')
71
            ->with($this->stringContains(get_class($this->provider)))
72
        ;
73
74
        $this->testedInstance->onStartedProviding($event);
75
76
        $this->assertEmpty($this->testedInstance->progressBar);
77
    }
78
79
    public function testOnStartedProvidingCreateProgressBar()
80
    {
81
        $event = new HasStartedProviding($this->entry);
82
83
        $this->provider
84
            ->expects($this->any())
85
            ->method('count')
86
            ->willReturn(42)
87
        ;
88
89
        $this->testedInstance->onStartedProviding($event);
90
91
        $this->assertNotEmpty($this->testedInstance->progressBar);
92
        $this->assertInstanceOf(
93
            ProgressBar::class,
94
            $this->testedInstance->progressBar
95
        );
96
        $this->assertEquals(42, $this->testedInstance->progressBar->getMaxSteps());
97
    }
98
99
    public function testOnProvidedDocumentAdvanceProgress()
100
    {
101
        $event = new HasProvidedDocument('id');
102
103
        $this->setProvressBarExpectsMethod('advance');
104
105
        $this->testedInstance->onProvidedDocument($event);
106
    }
107
108
    private function setProvressBarExpectsMethod($method)
109
    {
110
        $this->testedInstance->progressBar = $this
111
            ->getMock(ProgressBar::class, [], [$this->consoleOutput])
112
        ;
113
114
        $this->testedInstance
115
            ->progressBar
116
            ->expects($this->once())
117
            ->method($method)
118
        ;
119
    }
120
121
    public function testOnFinishedProvidingFinishProgress()
122
    {
123
        $event = new HasFinishedProviding($this->entry);
124
125
        $this->setProvressBarExpectsMethod('finish');
126
127
        $this->testedInstance->onFinishedProviding($event);
128
129
        $this->assertEmpty($this->testedInstance->progressBar);
130
    }
131
}
132