Completed
Push — develop ( 521632...eb9c4a )
by Mike
06:46
created

ErrorHandlingMiddlewareTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 2
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testThatParsingStartIsLogged() 0 20 1
A testThatAnErrorIsLogged() 0 19 1
1
<?php declare(strict_types=1);
2
3
namespace Parser\Middleware;
4
5
use phpDocumentor\Parser\Middleware\ErrorHandlingMiddleware;
6
use phpDocumentor\Reflection\File\LocalFile;
7
use phpDocumentor\Reflection\Php\Factory\File\CreateCommand;
8
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Log\LoggerInterface;
11
use Psr\Log\LogLevel;
12
13
/**
14
 * @coversDefaultClass \phpDocumentor\Parser\Middleware\ErrorHandlingMiddleware
15
 * @covers ::<private>
16
 * @covers ::__construct
17
 */
18
final class ErrorHandlingMiddlewareTest extends TestCase
19
{
20
    /**
21
     * @covers ::execute
22
     */
23
    public function testThatParsingStartIsLogged()
24
    {
25
        $filename = __FILE__;
26
        $command = new CreateCommand(new LocalFile($filename), new ProjectFactoryStrategies([]));
27
28
        $logger = $this->prophesize(LoggerInterface::class);
29
        $logger->log(LogLevel::INFO, 'Starting to parse file: ' . __FILE__, [])->shouldBeCalled();
30
31
        $middleware = new ErrorHandlingMiddleware($logger->reveal());
32
        $result = $middleware->execute(
33
            $command,
34
            function(CreateCommand $receivedCommand) use ($command) {
35
                $this->assertSame($command, $receivedCommand);
36
37
                return 'result';
38
            }
39
        );
40
41
        $this->assertSame('result', $result);
42
    }
43
44
    /**
45
     * @covers ::execute
46
     */
47
    public function testThatAnErrorIsLogged()
48
    {
49
        $filename = __FILE__;
50
        $command = new CreateCommand(new LocalFile($filename), new ProjectFactoryStrategies([]));
51
52
        $logger = $this->prophesize(LoggerInterface::class);
53
        $logger->log(LogLevel::INFO, 'Starting to parse file: ' . __FILE__, [])->shouldBeCalled();
54
        $logger->log(LogLevel::ALERT, '  Unable to parse file "' . __FILE__ . '", an error was detected: this is a test', [])->shouldBeCalled();
55
56
        $middleware = new ErrorHandlingMiddleware($logger->reveal());
57
        $result = $middleware->execute(
58
            $command,
59
            function(CreateCommand $receivedCommand) use ($command) {
0 ignored issues
show
Unused Code introduced by Mike van Riel
The parameter $receivedCommand is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
                throw new \Exception('this is a test');
61
            }
62
        );
63
64
        $this->assertNull($result);
65
    }
66
}
67