AbstractCommonLogTest::readLog()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Yoghi\Bundle\MaddaBundleTest\Utils;
4
5
use Monolog\Formatter\LineFormatter;
6
use Monolog\Handler\StreamHandler;
7
use Monolog\Logger;
8
use org\bovigo\vfs\vfsStream;
9
10
trait AbstractCommonLogTest
11
{
12
    private static $directoryV;
13
14
    /**
15
     * [$logger description].
16
     *
17
     * @var Psr\Log\LoggerInterface
18
     */
19
    private $logger;
20
21
    public static function setUpBeforeClass()
22
    {
23
        self::$directoryV = vfsStream::setup();
24
    }
25
26
    public function setUp()
27
    {
28
        $this->logger = new Logger('phpunit-logger');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Monolog\Logger('phpunit-logger') of type object<Monolog\Logger> is incompatible with the declared type object<Yoghi\Bundle\Madd...sr\Log\LoggerInterface> of property $logger.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29
        $directoryLogOutput = self::$directoryV->url().'/log';
30
        if (!file_exists($directoryLogOutput)) {
31
            mkdir($directoryLogOutput, 0700, true);
32
        }
33
        $output = "%level_name% > %message% %context% %extra%\n";
34
        $formatter = new LineFormatter($output);
35
        $handler = new StreamHandler($directoryLogOutput.'/phpunit.log', Logger::DEBUG, true, null, false);
36
        touch($directoryLogOutput.'/phpunit.log');
37
        $handler->setFormatter($formatter);
38
        $this->logger->pushHandler($handler);
39
        $this->logger->info('Avviato test -> '.$this->getName());
0 ignored issues
show
Bug introduced by
It seems like getName() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
40
    }
41
42
    protected function readLog()
43
    {
44
        $fileLog = self::$directoryV->url().'/log/phpunit.log';
45
46
        return file_get_contents($fileLog);
47
    }
48
49
    public function tearDown()
50
    {
51
        $fileLog = self::$directoryV->url().'/log/phpunit.log';
52
        if ($this->hasFailed()) {
0 ignored issues
show
Bug introduced by
It seems like hasFailed() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
53
            echo "\n---- LOG ----\n";
54
            if (is_readable($fileLog)) {
55
                echo file_get_contents($fileLog);
56
            }
57
            echo "------------\n";
58
        }
59
        if (file_exists($fileLog)) {
60
            unlink($fileLog);
61
        }
62
        $this->logger = null;
63
    }
64
}
65