Completed
Push — master ( 8affb2...358096 )
by Mikhail
06:38
created

PinbaLogger::getLogStack()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * @author Dolgov_M <[email protected]>
4
 * @date   15.11.2017 at 16:41
5
 */
6
7
namespace SilexPinbaProvider\Test;
8
9
use Intaro\PinbaBundle\Stopwatch\Stopwatch;
10
use PHPUnit\Framework\TestCase;
11
use Psr\Log\AbstractLogger;
12
use Silex\Provider\TwigServiceProvider;
13
use SilexPinbaProvider\SilexPinbaProvider;
14
use Silex\Application;
15
16
class PinbaTest extends TestCase
17
{
18
19
    public function testTwigExtension()
20
    {
21
22
        global $app;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
23
        $storage = new \ArrayObject();
24
        $app     = new ApplicationEmulator();
25
        $emulate = false;
26
        if(!function_exists('pinba_script_name_set')) {
27
            $emulate = true;
28
            $app['pinba_logger'] = function () {
29
                return new PinbaLogger();
30
            };
31
            require __DIR__.'/../pinba_emulator.php';
32
        }
33
        $app
34
            ->register(new TwigServiceProvider(),array(
35
                'twig.templates' => array('hello' => 'Hello {{ name }}!'),
36
            ))
37
            ->register(new SilexPinbaProvider());
38
39
        $app['intaro_pinba.stopwatch.class'] = 'SilexPinbaProvider\Test\StopwatchEmulate';
40
        $app->boot();
41
        /**
42
         * @var $stopwatch StopwatchEmulate
43
         */
44
        $stopwatch = $app['intaro_pinba.stopwatch'];
45
        $this->assertTrue($stopwatch instanceof Stopwatch);
46
        $stopwatch-> setStorage($storage);
47
        $app->renderView('hello');
48
        $this->assertTrue(is_array($storage['tags']), var_export($storage, true));
49
        if($emulate) {
50
            /**
51
             * @var $logger PinbaLogger
52
             */
53
            $logger = $app['pinba_logger'];
54
            $this->assertTrue($logger instanceof PinbaLogger);
55
            $stack = $logger->getLogStack();
56
            $this->assertTrue(is_array($stack));
57
            $this->assertNotEmpty($stack);
58
            $expected = [
59
                ['debug', 'pinba_get_info', []],
60
            ];
61
            $this->assertEquals($expected, $stack);
62
        }
63
    }
64
}
65
66
67
class StopwatchEmulate extends Stopwatch
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
68
{
69
70
    /**
71
     * @var \ArrayObject
72
     */
73
    private $storage;
74
75
    /**
76
     * @param \ArrayObject $storage
77
     * @return $this
78
     */
79
    public function setStorage($storage)
80
    {
81
        $this->storage = $storage;
82
        return $this;
83
    }
84
85
86
87
    public function start(array $tags)
88
    {
89
        return new StopwatchEventEmulate($tags, $this->storage);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return new \SilexPinbaPr...$tags, $this->storage); (SilexPinbaProvider\Test\StopwatchEventEmulate) is incompatible with the return type of the parent method Intaro\PinbaBundle\Stopwatch\Stopwatch::start of type Intaro\PinbaBundle\Stopwatch\StopwatchEvent.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
90
    }
91
92
}
93
94
class StopwatchEventEmulate
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
95
{
96
    /**
97
     * @var array
98
     */
99
    private $tags;
100
    /**
101
     * @var \ArrayObject
102
     */
103
    private $storage;
104
105
    /**
106
     * StopwatchEventEmulate constructor.
107
     * @param array $tags
108
     * @param \ArrayObject $storage
109
     */
110
    public function __construct(array $tags, \ArrayObject $storage)
111
    {
112
        $this->tags    = $tags;
113
        $this->storage = $storage;
114
    }
115
116
117
    public function stop()
118
    {
119
       $this->storage['tags'] = $this->tags;
120
    }
121
122
}
123
124
125
class ApplicationEmulator extends Application
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
126
{
127
    use Application\TwigTrait;
128
}
129
130
class PinbaLogger extends AbstractLogger {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
131
132
    private $logStack = [];
133
134
    /**
135
     * Logs with an arbitrary level.
136
     *
137
     * @param mixed $level
138
     * @param string $message
139
     * @param array $context
140
     *
141
     * @return void
142
     */
143
    public function log($level, $message, array $context = array())
144
    {
145
        $this->logStack[] = [$level, $message, $context];
146
    }
147
148
    /**
149
     * @return array
150
     */
151
    public function getLogStack()
152
    {
153
        return $this->logStack;
154
    }
155
156
}