Completed
Push — master ( 882cda...9e63b7 )
by Thomas
02:18 queued 47s
created

testMessageCollectorIsLoadedAfterDatabaseCollector()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 9
nc 5
nop 0
1
<?php
2
3
use DebugBar\DataCollector\MessagesCollector;
4
5
/**
6
 * Tests for DebugBar
7
 */
8
class DebugBarTest extends SapphireTest
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
{
10
    public function setUp()
11
    {
12
        parent::setUp();
13
14
        // Init manually because we are running tests
15
        DebugBar::initDebugBar();
16
    }
17
18
    public function tearDown()
19
    {
20
        DebugBar::clearDebugBar();
21
22
        parent::tearDown();
23
    }
24
25
    public function testInitIsWorking()
26
    {
27
        // De we have a debugbar instance
28
        $this->assertNotEmpty(DebugBar::getDebugBar());
0 ignored issues
show
Bug introduced by
The method assertNotEmpty() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
30
        // Do we have a db proxy
31
        if (method_exists('DB', 'get_conn')) {
32
            $conn = DB::get_conn();
33
        } else {
34
            $conn = DB::getConn();
0 ignored issues
show
Deprecated Code introduced by
The method DB::getConn() has been deprecated with message: since version 4.0 Use DB::get_conn instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
35
        }
36
37
        $class = get_class($conn);
38
        $this->assertContains($class, array('DebugBarDatabaseNewProxy', 'DebugBarDatabaseProxy'));
39
    }
40
41
    public function testLHelper()
42
    {
43
        $msg = 'Test me';
44
        l($msg);
45
46
        $debugbar = DebugBar::getDebugBar();
47
48
        /* @var $messagesCollector  DebugBar\DataCollector\MessagesCollector  */
49
        $messagesCollector = $debugbar->getCollector('messages');
50
        $messages = $messagesCollector->getMessages();
51
        $found = false;
52
        foreach ($messages as $message) {
53
            $txt = $message['message'];
54
            if (strpos($txt, $msg) !== false) {
55
                $found = true;
56
            }
57
        }
58
        $this->assertTrue($found);
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59
    }
60
61
    public function testDHelper()
62
    {
63
        $this->markTestSkipped(
0 ignored issues
show
Bug introduced by
The method markTestSkipped() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
64
            'This test needs to be looked at again, the output buffering is not capturing the result'
65
        );
66
        $sql = 'SELECT * FROM Member';
67
        ob_start();
68
        // Passing a SapphireTest as first arg prevent exit
69
        d($this, 'test', $sql);
70
        $content = ob_get_clean();
71
        $this->assertTrue((bool) strpos($content, "Value for: 'test'"), "Value for test not found");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
72
        $this->assertTrue((bool) strpos($content, 'sf-dump'), "Symfony dumper not found");
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
        $this->assertTrue(
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            (bool)strpos($content, '<span style="font-weight:bold;">SELECT</span>'),
75
            "Sql formatted query not found"
76
        );
77
    }
78
79
    /**
80
     * @param callable $context
81
     * @param string   $expected
82
     * @dataProvider whyDisabledProvider
83
     */
84
    public function testWhyDisabled($context, $expected)
85
    {
86
        $context();
87
        $this->assertSame($expected, DebugBar::whyDisabled());
0 ignored issues
show
Bug introduced by
The method assertSame() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
88
    }
89
90
    /**
91
     * @return array[]
92
     */
93
    public function whyDisabledProvider()
94
    {
95
        return array(
96
            array(
97
                function () {
98
                    Director::set_environment_type('live');
0 ignored issues
show
Deprecated Code introduced by
The method Director::set_environment_type() has been deprecated with message: 4.0 Use the "Director.environment_type" config setting instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
                },
100
                'Not in dev mode'
101
            ),
102
            array(
103
                function () {
104
                    Config::inst()->update('DebugBar', 'disabled', true);
105
                },
106
                'Disabled by a constant or configuration'
107
            ),
108
            array(
109
                function () {
110
                    // no-op
111
                },
112
                'In CLI mode'
113
            )
114
        );
115
    }
116
117
    public function testNotLocalIp()
0 ignored issues
show
Coding Style introduced by
testNotLocalIp uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

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

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
118
    {
119
        Config::inst()->update('DebugBar', 'check_local_ip', false);
120
        $this->assertFalse(DebugBar::notLocalIp());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
121
122
        Config::inst()->update('DebugBar', 'check_local_ip', true);
123
        $original = $_SERVER['REMOTE_ADDR'];
124
        $_SERVER['REMOTE_ADDR'] = '123.456.789.012';
125
        $this->assertTrue(DebugBar::notLocalIp());
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
126
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
127
        $this->assertFalse(DebugBar::notLocalIp());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
129
        unset($_SERVER['REMOTE_ADDR']);
130
        $this->assertFalse(DebugBar::notLocalIp());
0 ignored issues
show
Bug introduced by
The method assertFalse() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
131
132
        $_SERVER['REMOTE_ADDR'] = $original;
133
    }
134
135
    /**
136
     * For the database collector to be able to push messages to the message collector, it must be loaded
137
     * before the message collector. This test ensures that won't accidentally change in future.
138
     */
139
    public function testMessageCollectorIsLoadedAfterDatabaseCollector()
140
    {
141
        $bar = DebugBar::getDebugBar();
142
143
        $passedDatabaseCollector = false;
144
        foreach ($bar->getCollectors() as $collector) {
145
            if ($collector instanceof DebugBarDatabaseCollector) {
146
                $passedDatabaseCollector = true;
147
            }
148
            if ($collector instanceof MessagesCollector) {
149
                $this->assertTrue($passedDatabaseCollector, 'Message collector must be after database collector');
0 ignored issues
show
Bug introduced by
The method assertTrue() does not seem to exist on object<DebugBarTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
                break;
151
            }
152
        }
153
    }
154
}
155