Completed
Push — develop ( f3c189...e92436 )
by Alejandro
08:55
created

ConfigMigrationControllerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 6
c 2
b 1
f 0
lcom 1
cbo 6
dl 0
loc 86
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A setRequestParams() 0 4 1
A testParseConfigWithInvalidFormatThrowsException() 0 6 1
A testInvalidConfigKeyReturnsError() 0 10 1
A testDumpOutput() 0 13 1
A testOutputFile() 0 16 1
A initController() 0 13 1
1
<?php
2
namespace AcMailerTest\Controller;
3
4
use AcMailer\Controller\ConfigMigrationController;
5
use AcMailer\Service\ConfigMigrationService;
6
use AcMailerTest\Console\AdapterMock;
7
use Zend\Console\Request;
8
use PHPUnit_Framework_TestCase as TestCase;
9
use Zend\Stdlib\Parameters;
10
11
/**
12
 * Class ConfigMigrationControllerTest
13
 * @author Alejandro Celaya Alastrué
14
 * @link http://www.alejandrocelaya.com
15
 */
16
class ConfigMigrationControllerTest extends TestCase
17
{
18
    /**
19
     * @var ConfigMigrationController
20
     */
21
    private $controller;
22
    /**
23
     * @var Request
24
     */
25
    private $request;
26
    /**
27
     * @var AdapterMock
28
     */
29
    private $console;
30
31
    /**
32
     * @expectedException \AcMailer\Exception\InvalidArgumentException
33
     */
34
    public function testParseConfigWithInvalidFormatThrowsException()
35
    {
36
        $this->initController();
37
        $this->setRequestParams(['format' => 'invalid']);
38
        $this->controller->parseConfigAction();
39
    }
40
41
    public function testInvalidConfigKeyReturnsError()
42
    {
43
        $this->initController();
44
        $this->setRequestParams(['configKey' => 'invalid']);
45
        $this->controller->parseConfigAction();
46
        $this->assertEquals(
47
            'It wasn\'t possible to find the configuration key "invalid"',
48
            $this->console->getLines()[0]
49
        );
50
    }
51
52
    public function testDumpOutput()
53
    {
54
        $this->initController([
55
            'mail_options' => []
56
        ]);
57
        $this->setRequestParams(['format' => 'json']);
58
        $this->controller->parseConfigAction();
59
        $this->assertEquals([
60
            0 => 'This is your new configuration for the AcMailer module:',
61
            1 => '{"acmailer_options":{"default":{"message_options":{"body":[]},'
62
                . '"smtp_options":{"connection_config":[]},"file_options":[]}}}'
63
        ], $this->console->getLines());
64
    }
65
66
    public function testOutputFile()
67
    {
68
        $this->initController([
69
            'mail_options' => []
70
        ]);
71
        $expectedFile = __DIR__ . '/../attachments/mail_config.json';
72
        $this->setRequestParams(['format' => 'json', 'outputFile' => $expectedFile]);
73
        $this->controller->parseConfigAction();
74
        $this->assertTrue(file_exists($expectedFile));
75
        $this->assertEquals(
76
            '{"acmailer_options":{"default":{"message_options":{"body":[]},'
77
            . '"smtp_options":{"connection_config":[]},"file_options":[]}}}',
78
            file_get_contents($expectedFile)
79
        );
80
        unlink($expectedFile);
81
    }
82
83
    private function initController(array $globalConfig = [])
84
    {
85
        $this->controller = new ConfigMigrationController(new ConfigMigrationService(), $globalConfig);
86
        $this->console = new AdapterMock();
87
        $this->controller->setConsole($this->console);
88
        $this->request = new Request();
89
90
        // Set the request via reflection
91
        $refClass = new \ReflectionClass($this->controller);
92
        $property = $refClass->getProperty('request');
93
        $property->setAccessible(true);
94
        $property->setValue($this->controller, $this->request);
95
    }
96
97
    private function setRequestParams(array $params = [])
98
    {
99
        $this->request->setParams(new Parameters($params));
100
    }
101
}
102