Completed
Push — develop ( 4b49c4...89d32a )
by Jaap
09:06 queued 05:30
created

phpDocumentor/Command/Helper/LoggerHelperTest.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5.3
6
 *
7
 * @copyright 2010-2014 Mike van Riel / Naenius (http://www.naenius.com)
8
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
9
 * @link      http://phpdoc.org
10
 */
11
12
namespace phpDocumentor\Command\Helper;
13
14
use Closure;
15
use Mockery as m;
16
use Monolog\Logger;
17
use phpDocumentor\Configuration;
18
use phpDocumentor\Event\LogEvent;
19
use PHPUnit\Framework\TestCase;
20
use Psr\Log\LogLevel;
21
use stdClass;
22
use Symfony\Component\Console\Output\OutputInterface;
23
24
/**
25
 * Testcase for LoggerHelper
26
 */
27
class LoggerHelperTest extends \Mockery\Adapter\Phpunit\MockeryTestCase
28
{
29
    /**
30
     *
31
     * @var LoggerHelper
32
     */
33
    protected $fixture;
34
35
    /**
36
     * Fake config class.
37
     *
38
     * @var stdClass
39
     */
40
    protected $config;
41
42
    const MY_LOGLEVEL = 'loglevel';
43
44
    const MY_DEFAULT_LOG_PATH = 'defaultPath';
45
46
    protected function setUp()
47
    {
48
        $this->fixture = new LoggerHelper();
49
        $this->config = new Configuration();
50
        $this->config->getLogging()->setLevel(static::MY_LOGLEVEL);
51
        $this->config->getLogging()->setPaths(array('default' => static::MY_DEFAULT_LOG_PATH));
0 ignored issues
show
array('default' => static::MY_DEFAULT_LOG_PATH) is of type array<string,string,{"default":"string"}>, but the function expects a array<integer,object<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...
52
    }
53
54
    /**
55
     * Assure that addOption is called once
56
     *
57
     * @covers phpDocumentor\Command\Helper\LoggerHelper::addOptions
58
     */
59
    public function testAddOptions()
60
    {
61
        $commandMock = m::mock('phpDocumentor\Command\Command')
62
            ->shouldReceive('addOption')
63
            ->once()
64
            ->withAnyArgs()
65
            ->getMock();
66
67
        $this->fixture->addOptions($commandMock);
68
69
        //test passes by mockery assertion
70
        $this->assertTrue(true);
71
    }
72
73
    /**
74
     * Assure that name of the helper isn't changed
75
     *
76
     * @covers phpDocumentor\Command\Helper\LoggerHelper::getName
77
     */
78
    public function testGetName()
79
    {
80
        $this->assertSame('phpdocumentor_logger', $this->fixture->getName());
81
    }
82
83
    /**
84
     * Assure that name of the helper isn't changed
85
     *
86
     * @covers phpDocumentor\Command\Helper\LoggerHelper::connectOutputToLogging
87
     */
88
    public function testConnectOutputToLoggingExecutedOnce()
89
    {
90
        $assertClosure = function ($closure) {
91
            return $closure instanceof Closure;
92
        };
93
94
        $commandMock = m::mock('phpDocumentor\Command\Command')
95
            ->shouldReceive('getService')
96
            ->with('event_dispatcher')
97
            ->andReturnSelf()
98
            ->getMock();
99
100
        $commandMock->shouldReceive('addListener')
101
            ->once()
102
            ->withArgs(
103
                array(
104
                    'parser.file.pre',
105
                    m::on($assertClosure)
106
                )
107
            );
108
109
        $commandMock->shouldReceive('addListener')
110
            ->once()
111
            ->withArgs(
112
                array(
113
                    'system.log',
114
                    m::on($assertClosure)
115
                )
116
            );
117
118
        $this->fixture->connectOutputToLogging(
119
            m::mock('Symfony\Component\Console\Output\OutputInterface'),
120
            $commandMock
121
        );
122
123
        // call for a second time.
124
        $this->fixture->connectOutputToLogging(
125
            m::mock('Symfony\Component\Console\Output\OutputInterface'),
126
            $commandMock
127
        );
128
129
        //test passes by mockery assertion
130
        $this->assertTrue(true);
131
    }
132
133
    /**
134
     * test replacement of placeholders in log message
135
     *
136
     * @covers phpDocumentor\Command\Helper\LoggerHelper::logEvent
137
     */
138
    public function testLogEventPlaceHoldersAreReplaced()
139
    {
140
        $output = m::mock('Symfony\Component\Console\Output\OutputInterface')
141
            ->shouldReceive('writeln')
142
            ->with('  <error>my first message with 2 replacements</error>')
143
            ->shouldReceive('getVerbosity')
144
            ->andReturn(LogLevel::ERROR)
145
            ->getMock();
146
147
        $command = m::mock('phpDocumentor\Command\Command')
148
            ->shouldReceive('getContainer')->andReturnSelf()
149
            ->shouldReceive('offsetGet')->andReturnSelf()
150
            ->shouldReceive('translate')
151
            ->andReturnUsing(
152
                function ($message) {
153
                    return $message;
154
                }
155
            )
156
            ->getMock();
157
158
        $event = new LogEvent($this);
159
        $event->setPriority(LogLevel::ERROR);
160
        $event->setMessage('my %s message with %d replacements');
161
        $event->setContext(array(
162
            'first',
163
            2,
164
        ));
165
166
        $this->fixture->logEvent($output, $event, $command);
167
    }
168
169
    /**
170
     * Assure nothing is logged when priority is not matching
171
     *
172
     * @covers phpDocumentor\Command\Helper\LoggerHelper::logEvent
173
     */
174
    public function testLogPriorityIsChecked()
175
    {
176
        $output = m::mock('Symfony\Component\Console\Output\OutputInterface')
177
            ->shouldReceive('writeln')
178
            ->never()
179
            ->shouldReceive('getVerbosity')
180
            ->andReturn(LogLevel::ERROR)
181
            ->getMock();
182
183
        $command = m::mock('phpDocumentor\Command\Command');
184
185
        $event = new LogEvent($this);
186
        $event->setPriority(LogLevel::DEBUG);
187
188
        $this->fixture->logEvent($output, $event, $command);
189
    }
190
191
    /**
192
     * Check Loglevel per verbosity
193
     *
194
     * @dataProvider verbosityDataProvider
195
     * @covers phpDocumentor\Command\Helper\LoggerHelper::reconfigureLogger
196
     */
197
    public function testReconfigureLoggerVerbosity($verbosity, $expectedLogLevel)
198
    {
199
        $input = m::mock('Symfony\Component\Console\Input\InputInterface')
200
            ->shouldReceive('getOption')
201
            ->andReturnNull()
202
            ->getMock();
203
204
        $output = m::mock('Symfony\Component\Console\Output\OutputInterface')
205
            ->shouldReceive('getVerbosity')
206
            ->andReturn($verbosity)
207
            ->getMock();
208
209
        $application = m::mock('phpDocumentor\Application')
210
            ->shouldReceive('configureLogger')->withArgs(array("", $expectedLogLevel, "defaultPath",))
211
            ->shouldReceive('offsetGet')->with('config')->andReturn($this->config)
212
            ->shouldReceive('offsetGet')->andReturnNull()
213
            ->getMock();
214
215
        $command = m::mock('phpDocumentor\Command\Command')
216
            ->shouldReceive('getContainer')
217
            ->andReturn($application)
218
            ->getMock();
219
220
        $this->fixture->reconfigureLogger($input, $output, $command);
221
    }
222
223
    /**
224
     * Dataprovider for verbosity tests
225
     *
226
     * @return array
227
     */
228
    public function verbosityDataProvider()
229
    {
230
        return array(
231
            array(
232
                OutputInterface::VERBOSITY_QUIET,
233
                Logger::ERROR
234
            ),
235
            array(
236
                OutputInterface::VERBOSITY_NORMAL,
237
                self::MY_LOGLEVEL
238
            ),
239
            array(
240
                OutputInterface::VERBOSITY_VERBOSE,
241
                Logger::WARNING
242
            ),
243
            array(
244
                OutputInterface::VERBOSITY_VERY_VERBOSE,
245
                Logger::INFO
246
            ),
247
            array(
248
                OutputInterface::VERBOSITY_DEBUG,
249
                Logger::DEBUG
250
            ),
251
        );
252
    }
253
254
    /**
255
     *
256
     * @covers phpDocumentor\Command\Helper\LoggerHelper::reconfigureLogger
257
     */
258
    public function testLogPathDefaultIsUsed()
259
    {
260
        $input = m::mock('Symfony\Component\Console\Input\InputInterface')
261
            ->shouldReceive('getOption')
262
            ->andReturnNull()
263
            ->getMock();
264
265
        $output = m::mock('Symfony\Component\Console\Output\OutputInterface')
266
            ->shouldReceive('getVerbosity')
267
            ->andReturn(OutputInterface::VERBOSITY_QUIET)
268
            ->getMock();
269
270
        $application = m::mock('phpDocumentor\Application')
271
            ->shouldReceive('configureLogger')->withArgs(array(null, Logger::ERROR, static::MY_DEFAULT_LOG_PATH))
272
            ->shouldReceive('offsetGet')->with('config')->andReturn($this->config)
273
            ->shouldReceive('offsetGet')->with(m::any())->andReturnNull()
274
            ->getMock();
275
276
        $command = m::mock('phpDocumentor\Command\Command')
277
            ->shouldReceive('getContainer')
278
            ->andReturn($application)
279
            ->getMock();
280
281
        $this->fixture->reconfigureLogger($input, $output, $command);
282
    }
283
}
284