ImportCommandTest::testRunDryrun()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 24
rs 9.536
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Mathielen\ImportEngineBundle\Tests\Command;
4
5
use Mathielen\ImportEngine\Event\ImportConfigureEvent;
6
use Mathielen\ImportEngineBundle\Utils;
7
use Mathielen\ImportEngine\Import\Import;
8
use Mathielen\ImportEngine\Importer\Importer;
9
use Mathielen\ImportEngine\ValueObject\ImportConfiguration;
10
use Mathielen\ImportEngine\ValueObject\ImportRequest;
11
use Mathielen\ImportEngine\ValueObject\ImportRun;
12
use Mathielen\ImportEngineBundle\Command\ImportCommand;
13
use Symfony\Component\Console\Input\ArrayInput;
14
use Symfony\Component\Console\Output\Output;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
class ImportCommandTest extends \PHPUnit_Framework_TestCase
18
{
19
    /**
20
     * @var ImportCommand
21
     */
22
    private $command;
23
24
    /**
25
     * @var ContainerBuilder
26
     */
27
    private $container;
28
29
    protected function setUp()
30
    {
31
        $ib = $this->getMockBuilder('Mathielen\ImportEngine\Import\ImportBuilder')->disableOriginalConstructor()->getMock();
32
33
        $this->container = new ContainerBuilder();
34
        $this->container->set('event_dispatcher', $this->createMock('Symfony\Component\EventDispatcher\EventDispatcherInterface'));
35
        $this->container->set('mathielen_importengine.import.builder', $ib);
36
        $this->container->set('mathielen_importengine.import.runner', $this->getMockBuilder('Mathielen\ImportEngine\Import\Run\ImportRunner')->disableOriginalConstructor()->getMock());
37
38
        $this->command = new ImportCommand();
0 ignored issues
show
Bug introduced by
The call to ImportCommand::__construct() misses some required arguments starting with $importBuilder.
Loading history...
39
        $this->command->setContainer($this->container);
0 ignored issues
show
Bug introduced by
The method setContainer() does not seem to exist on object<Mathielen\ImportE...\Command\ImportCommand>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
40
    }
41
42
    public function testRunLimit()
43
    {
44
        $this->container->get('mathielen_importengine.import.builder')
45
            ->expects($this->once())
46
            ->method('buildFromRequest')
47
            ->will($this->returnValue(
48
                new Import(
49
                    new Importer(
50
                        $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface')
51
                    ),
52
                    $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface'),
53
                    new ImportRun(new ImportConfiguration())
54
                )
55
            ));
56
57
        $this->container->get('event_dispatcher')
58
            ->expects($this->exactly(2))
59
            ->method('addListener')
60
            ->withConsecutive(
61
                array(
62
                    ImportConfigureEvent::AFTER_BUILD,
63
                    $this->anything(),
64
                )
65
            );
66
67
        $this->container->get('mathielen_importengine.import.runner')
68
            ->expects($this->once())
69
            ->method('run');
70
71
        $input = new ArrayInput(array('--limit' => 1, '--importer' => 'abc', 'source_id' => 'source_id'), $this->command->getDefinition());
72
        $output = new TestOutput();
73
74
        $this->command->run($input, $output);
75
    }
76
77
    public function testRunDryrun()
78
    {
79
        $this->container->get('mathielen_importengine.import.builder')
80
            ->expects($this->once())
81
            ->method('buildFromRequest')
82
            ->will($this->returnValue(
83
                new Import(
84
                    new Importer(
85
                        $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface')
86
                    ),
87
                    $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface'),
88
                    new ImportRun(new ImportConfiguration())
89
                )
90
            ));
91
92
        $this->container->get('mathielen_importengine.import.runner')
93
            ->expects($this->once())
94
            ->method('dryrun');
95
96
        $input = new ArrayInput(array('--dryrun' => true, '--importer' => 'abc', 'source_id' => 'source_id'), $this->command->getDefinition());
97
        $output = new TestOutput();
98
99
        $this->command->run($input, $output);
100
    }
101
102
    /**
103
     * @dataProvider getRunData
104
     */
105
    public function testRun(array $input, $parsedSourceId)
106
    {
107
        $this->container->get('mathielen_importengine.import.builder')
108
            ->expects($this->once())
109
            ->method('buildFromRequest')
110
            ->with(new ImportRequest($parsedSourceId, 'default', null, Utils::whoAmI().'@CLI'))
111
            ->will($this->returnValue(
112
                new Import(
113
                    new Importer(
114
                        $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface')
115
                    ),
116
                    $this->createMock('Mathielen\ImportEngine\Storage\StorageInterface'),
117
                    new ImportRun(new ImportConfiguration())
118
                )
119
            ));
120
121
        $this->container->get('mathielen_importengine.import.runner')
122
            ->expects($this->once())
123
            ->method('run');
124
125
        $input = new ArrayInput($input, $this->command->getDefinition());
126
        $output = new TestOutput();
127
128
        $this->command->run($input, $output);
129
    }
130
131
    public function getRunData()
132
    {
133
        return array(
134
            array(array('source_id' => 'source_id'), 'source_id'),
135
            array(array('source_id' => 'service.method?arg1=abc'), array('service' => 'service', 'method' => 'method', 'arguments' => array('arg1' => 'abc'))),
136
        );
137
    }
138
}
139
140
class TestOutput extends Output
141
{
142
    public $output = '';
143
144
    public function clear()
145
    {
146
        $this->output = '';
147
    }
148
149
    protected function doWrite($message, $newline)
150
    {
151
        $this->output .= $message.($newline ? "\n" : '');
152
    }
153
}
154