CommandTest::testOutput()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 22
rs 9.2
cc 1
eloc 16
nc 1
nop 0
1
<?php namespace Tests;
2
3
use Exception;
4
use InvalidArgumentException;
5
use org\bovigo\vfs\vfsStream;
6
use PHPUnit_Framework_TestCase;
7
use Simondubois\UnsplashDownloader\Command;
8
use Simondubois\UnsplashDownloader\Task;
9
use Simondubois\UnsplashDownloader\Validate;
10
use Symfony\Component\Console\Input\ArrayInput;
11
use Symfony\Component\Console\Output\BufferedOutput;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class CommandTest extends PHPUnit_Framework_TestCase
15
{
16
17
    /**
18
     * Mock Validate class
19
     * @param  array $options Options to be validated
20
     * @return object Mocked validate
21
     */
22
    private function mockValidate($options) {
23
        $validate = $this->getMock('Simondubois\UnsplashDownloader\Validate', array_keys($options));
24
25
        unset($options['featured']);
26
27
        foreach ($options as $key => $value) {
28
            $validate->expects($this->once())
29
                ->method($key)
30
                ->with($this->identicalTo($value))
31
                ->willReturn(is_numeric($value) ? intval($value) : $value);
32
        }
33
34
        return $validate;
35
    }
36
37
    /**
38
     * Test Simondubois\UnsplashDownloader\Command::verboseOutput()
39
     */
40
    public function testVerboseOutput() {
41
        // Instantiate command
42
        $command = new Command();
43
        $command->output = new BufferedOutput();
44
        $message = 'This is a message'.PHP_EOL.'split on many'.PHP_EOL.'lines';
45
46
        // Output for verbosity : normal
47
        $command->output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
48
        $command->verboseOutput($message);
49
        $this->assertEmpty($command->output->fetch());
50
        $command->verboseOutput($message, 'info');
51
        $this->assertEmpty($command->output->fetch());
52
53
        // Output for verbosity : verbose
54
        $command->output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
55
        $command->verboseOutput($message);
56
        $this->assertEquals($message, $command->output->fetch());
57
        $command->verboseOutput($message, 'info', OutputInterface::OUTPUT_RAW);
58
        $this->assertEquals('<info>'.$message.'</info>', $command->output->fetch());
59
        $command->verboseOutput($message.PHP_EOL, 'info', OutputInterface::OUTPUT_RAW);
60
        $this->assertEquals('<info>'.$message.'</info>'.PHP_EOL, $command->output->fetch());
61
    }
62
63
    /**
64
     * Test Simondubois\UnsplashDownloader\Command::output()
65
     */
66
    public function testOutput() {
67
        // Instantiate command
68
        $command = new Command();
69
        $command->output = new BufferedOutput();
70
        $message = 'This is a message'.PHP_EOL.'split on many'.PHP_EOL.'lines';
71
72
        // Output for verbosity : quiet
73
        $command->output->setVerbosity(OutputInterface::VERBOSITY_QUIET);
74
        $command->output($message);
75
        $this->assertEmpty($command->output->fetch());
76
        $command->output($message, 'info');
77
        $this->assertEmpty($command->output->fetch());
78
79
        // Output for verbosity : normal
80
        $command->output->setVerbosity(OutputInterface::VERBOSITY_NORMAL);
81
        $command->output($message);
82
        $this->assertEquals($message, $command->output->fetch());
83
        $command->output($message, 'info', OutputInterface::OUTPUT_RAW);
84
        $this->assertEquals('<info>'.$message.'</info>', $command->output->fetch());
85
        $command->output($message.PHP_EOL, 'info', OutputInterface::OUTPUT_RAW);
86
        $this->assertEquals('<info>'.$message.'</info>'.PHP_EOL, $command->output->fetch());
87
    }
88
89
    /**
90
     * Test Simondubois\UnsplashDownloader\Command::execute()
91
     */
92
    public function testCategoriesExecute() {
93
        // Mock task
94
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['categories', 'download']);
95
        $task->expects($this->once())->method('categories');
96
        $task->expects($this->never())->method('download');
97
98
        // Mock command
99
        $command = $this->getMock(
100
            'Simondubois\UnsplashDownloader\Command',
101
            ['task', 'parameters']
102
        );
103
        $command->expects($this->once())->method('task')->willReturn($task);
104
        $command->expects($this->never())->method('parameters');
105
106
        // Execute command
107
        $input = new ArrayInput(['--categories' => true], $command->getDefinition());
108
        $output = new BufferedOutput();
109
        $command->execute($input, $output);
110
    }
111
112
    /**
113
     * Test Simondubois\UnsplashDownloader\Command::execute()
114
     */
115
    public function testDownloadExecute() {
116
        // Mock task
117
        $task = $this->getMock('Simondubois\UnsplashDownloader\Task', ['categories', 'download']);
118
        $task->expects($this->never())->method('categories');
119
        $task->expects($this->once())->method('download');
120
121
        // Mock command
122
        $command = $this->getMock(
123
            'Simondubois\UnsplashDownloader\Command',
124
            ['task', 'parameters']
125
        );
126
        $command->expects($this->once())->method('task')->willReturn($task);
127
        $command->expects($this->once())->method('parameters')
128
            ->with($this->equalTo(new Validate()), $this->identicalTo($task), $this->anything());
129
130
        // Execute command
131
        $input = new ArrayInput([], $command->getDefinition());
132
        $output = new BufferedOutput();
133
        $command->execute($input, $output);
134
    }
135
136
    /**
137
     * Test Simondubois\UnsplashDownloader\Command::execute()
138
     */
139
    public function testTask() {
140
        // Instantiate command
141
        $command = new Command();
142
143
        // Test method
144
        $this->assertInstanceOf('Simondubois\UnsplashDownloader\Task', $command->task());
145
    }
146
147
    /**
148
     * Test Simondubois\UnsplashDownloader\Command::parameters()
149
     */
150
    public function testParameters() {
151
        // Instantiate command
152
        $command = new Command();
153
        $command->output = new BufferedOutput();
154
        $command->output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
155
156
        // Instiantiate file system
157
        $root = vfsStream::setup('test')->url();
158
        $destination = $root.'/destination';
159
        mkdir($destination);
160
        $history = $root.'/history';
161
162
        // Assert attribute assignation (default values)
163
        $options = [
164
            'destination' => getcwd(),
165
            'quantity' => '10',
166
            'history' => null,
167
            'featured' => false,
168
            'category' => '123',
169
        ];
170
171
        // Instantiate task (default values)
172
        $task = $this->getMock(
173
            'Simondubois\UnsplashDownloader\Task',
174
            ['setDestination', 'setQuantity', 'setHistory', 'setFeatured', 'setCategory']
175
        );
176
        $task->expects($this->once())->method('setDestination')->with($this->identicalTo($options['destination']));
177
        $task->expects($this->once())->method('setQuantity')->with($this->identicalTo(intval($options['quantity'])));
178
        $task->expects($this->once())->method('setHistory')->with($this->identicalTo($options['history']));
179
        $task->expects($this->once())->method('setFeatured')->with($this->identicalTo($options['featured']));
180
        $task->expects($this->once())->method('setCategory')->with($this->identicalTo(intval($options['category'])));
181
        $command->parameters($this->mockValidate($options), $task, $options);
182
183
        // Assert output content (default values)
184
        $output = $command->output->fetch();
185
        $this->assertContains(getcwd(), $output);
186
        $this->assertContains($options['quantity'], $output);
187
        $this->assertContains('Do not use history.', $output);
188
        $this->assertContains('featured and not featured', $output);
189
190
        // Assert attribute assignation (custom values)
191
        $options = [
192
            'destination' => $destination,
193
            'quantity' => '100',
194
            'history' => $history,
195
            'featured' => true,
196
            'category' => null,
197
        ];
198
199
        // Instantiate task (custom values)
200
        $task = $this->getMock(
201
            'Simondubois\UnsplashDownloader\Task',
202
            ['setDestination', 'setQuantity', 'setHistory', 'setFeatured', 'setCategory']
203
        );
204
        $task->expects($this->once())->method('setDestination')->with($this->identicalTo($options['destination']));
205
        $task->expects($this->once())->method('setQuantity')->with($this->identicalTo(intval($options['quantity'])));
206
        $task->expects($this->once())->method('setHistory')->with($this->identicalTo($options['history']));
207
        $task->expects($this->once())->method('setFeatured')->with($this->identicalTo($options['featured']));
208
        $task->expects($this->once())->method('setCategory')->with($this->identicalTo($options['category']));
209
        $command->parameters($this->mockValidate($options), $task, $options);
210
211
        // Assert output content (custom values)
212
        $output = $command->output->fetch();
213
        $this->assertContains($options['destination'], $output);
214
        $this->assertContains($options['quantity'], $output);
215
        $this->assertContains($options['history'], $output);
216
        $this->assertContains('only featured', $output);
217
    }
218
219
}
220