Completed
Push — master ( 77fd29...f7cf81 )
by Jacob
02:48
created

testGetExplainQueryUnsupportedDriver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 12
loc 12
rs 9.4286
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Particletree\Pqp;
4
5
use PDO;
6
use PHPUnit_Framework_Testcase;
7
use ReflectionClass;
8
use ReflectionMethod;
9
10
class PhpQuickProfilerTest extends PHPUnit_Framework_TestCase
11
{
12
13
    protected static $dbConnection;
14
15
    public static function setUpBeforeClass()
16
    {
17
        self::$dbConnection = new PDO('sqlite::memory:');
18
        self::$dbConnection->exec("
19
            CREATE TABLE IF NOT EXISTS `testing` (
20
                `id` integer PRIMARY KEY AUTOINCREMENT,
21
                `title` varchar(60) NOT NULL
22
            );"
23
        );
24
        self::$dbConnection->exec("
25
            INSERT INTO `testing`
26
                (`title`)
27
            VALUES
28
                ('alpha'),
29
                ('beta'),
30
                ('charlie'),
31
                ('delta');"
32
        );
33
    }
34
35
    public function testConstruct()
36
    {
37
        $startTime = microtime(true);
38
39
        $profiler = new PhpQuickProfiler();
40
        $this->assertAttributeEquals($startTime, 'startTime', $profiler);
41
42
        $profiler = new PhpQuickProfiler($startTime);
43
        $this->assertAttributeEquals($startTime, 'startTime', $profiler);
44
    }
45
46
    public function testSetConsole()
47
    {
48
        $console = new Console();
49
        $profiler = new PhpQuickProfiler();
50
        $profiler->setConsole($console);
51
52
        $this->assertAttributeSame($console, 'console', $profiler);
53
    }
54
55
    public function testSetDisplay()
56
    {
57
        $display = new Display();
58
        $profiler = new PhpQuickProfiler();
59
        $profiler->setDisplay($display);
60
61
        $this->assertAttributeSame($display, 'display', $profiler);
62
    }
63
64
    public function testGatherFileData()
65
    {
66
        $files = get_included_files();
67
        $profiler = new PhpQuickProfiler();
68
        $gatheredFileData = $profiler->gatherFileData();
69
70
        $this->assertInternalType('array', $gatheredFileData);
71
        $this->assertEquals(count($files), count($gatheredFileData));
72
        foreach ($gatheredFileData as $fileData) {
73
            $this->assertInternalType('array', $fileData);
74
            $this->assertArrayHasKey('name', $fileData);
75
            $this->assertContains($fileData['name'], $files);
76
            $this->assertArrayHasKey('size', $fileData);
77
            $this->assertEquals($fileData['size'], filesize($fileData['name']));
78
        }
79
    }
80
81
    public function testGatherMemoryData()
82
    {
83
        $memoryUsage = memory_get_peak_usage();
84
        $allowedLimit = ini_get('memory_limit');
85
        $profiler = new PhpQuickProfiler();
86
        $gatheredMemoryData = $profiler->gatherMemoryData();
87
88
        $this->assertInternalType('array', $gatheredMemoryData);
89
        $this->assertEquals(2, count($gatheredMemoryData));
90
        $this->assertArrayHasKey('used', $gatheredMemoryData);
91
        $this->assertEquals($memoryUsage, $gatheredMemoryData['used']);
92
        $this->assertArrayHasKey('allowed', $gatheredMemoryData);
93
        $this->assertEquals($allowedLimit, $gatheredMemoryData['allowed']);
94
    }
95
96
    public function testSetProfiledQueries()
97
    {
98
        $profiledQueries = $this->dataProfiledQueries();
99
        $profiler = new PhpQuickProfiler();
100
        $profiler->setProfiledQueries($profiledQueries);
101
102
        $this->assertAttributeEquals($profiledQueries, 'profiledQueries', $profiler);
103
    }
104
105
    /**
106
     * @dataProvider dataProfiledQueries
107
     */
108
    public function testExplainQuery($sql, $parameters)
109
    {
110
        $profiler = new PhpQuickProfiler();
111
        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
112
113
        $explainedQuery = $reflectedMethod->invokeArgs(
114
            $profiler,
115
            array(self::$dbConnection, $sql, $parameters)
116
        );
117
        $this->assertInternalType('array', $explainedQuery);
118
        $this->assertGreaterThan(0, count($explainedQuery));
119
    }
120
121
    /**
122
     * @expectedException Exception
123
     */
124
    public function testExplainQueryBadQueryException()
125
    {
126
        $invalidQuery = 'SELECT * FROM `fake_table`';
127
        $profiler = new PhpQuickProfiler();
128
        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
129
130
        $explainedQuery = $reflectedMethod->invokeArgs(
0 ignored issues
show
Unused Code introduced by
$explainedQuery is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
131
            $profiler,
132
            array(self::$dbConnection, $invalidQuery)
133
        );
134
    }
135
136
    /**
137
     * @expectedException Exception
138
     */
139 View Code Duplication
    public function testExplainQueryBadParametersException()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
140
    {
141
        $query = 'SELECT * FROM `testing` WHERE `title` = :title';
142
        $invalidParams = array('id' => 1);
143
        $profiler = new PhpQuickProfiler();
144
        $reflectedMethod = $this->getAccessibleMethod($profiler, 'explainQuery');
145
146
        $explainedQuery = $reflectedMethod->invokeArgs(
0 ignored issues
show
Unused Code introduced by
$explainedQuery is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
147
            $profiler,
148
            array(self::$dbConnection, $query, $invalidParams)
149
        );
150
    }
151
152
    /**
153
     * @dataProvider dataConnectionDrivers
154
     */
155
    public function testGetExplainQuery($driver, $prefix)
156
    {
157
        $query = 'SELECT * FROM `testing`';
158
        $profiler = new PhpQuickProfiler();
159
        $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
160
161
        $explainQuery = $reflectedMethod->invokeArgs(
162
            $profiler,
163
            array($query, $driver)
164
        );
165
166
        $explainPrefix = str_replace($query, '', $explainQuery);
167
        $explainPrefix = trim($explainPrefix);
168
        $this->assertEquals($prefix, $explainPrefix);
169
    }
170
171
    /**
172
     * @expectedException Exception
173
     */
174 View Code Duplication
    public function testGetExplainQueryUnsupportedDriver()
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
    {
176
        $query = 'SELECT * FROM `testing`';
177
        $unsupportedDriver = 'zz';
178
        $profiler = new PhpQuickProfiler();
179
        $reflectedMethod = $this->getAccessibleMethod($profiler, 'getExplainQuery');
180
181
        $explainQuery = $reflectedMethod->invokeArgs(
0 ignored issues
show
Unused Code introduced by
$explainQuery is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
182
            $profiler,
183
            array($query, $unsupportedDriver)
184
        );
185
    }
186
187
    public function testGatherSpeedData()
188
    {
189
        $elapsedTime = 1.234;
190
        $startTime = microtime(true) - $elapsedTime;
191
        $allowedTime = ini_get('max_execution_time');
192
        $profiler = new PhpQuickProfiler($startTime);
193
        $gatheredSpeedData = $profiler->gatherSpeedData();
194
195
        $this->assertInternalType('array', $gatheredSpeedData);
196
        $this->assertEquals(2, count($gatheredSpeedData));
197
        $this->assertArrayHasKey('elapsed', $gatheredSpeedData);
198
        $this->assertEquals($elapsedTime, $gatheredSpeedData['elapsed']);
199
        $this->assertArrayHasKey('allowed', $gatheredSpeedData);
200
        $this->assertEquals($allowedTime, $gatheredSpeedData['allowed']);
201
    }
202
203
    public function dataProfiledQueries()
204
    {
205
        return array(
206
            array(
207
              'sql' => "SELECT * FROM testing",
208
              'parameters' => array(),
209
              'time' => 25
210
            ),
211
            array(
212
              'sql' => "SELECT id FROM testing WHERE title = :title",
213
              'parameters' => array('title' => 'beta'),
214
              'time' => 5
215
            )
216
        );
217
    }
218
219
    public function dataConnectionDrivers()
220
    {
221
        return array(
222
            array(
223
                'driver' => 'mysql',
224
                'prefix' => 'EXPLAIN'
225
            ),
226
            array(
227
                'driver' => 'sqlite',
228
                'prefix' => 'EXPLAIN QUERY PLAN'
229
            )
230
        );
231
    }
232
233
    protected function getAccessibleMethod(PhpQuickProfiler $profiler, $methodName)
234
    {
235
        $reflectedConsole = new ReflectionClass(get_class($profiler));
236
        $reflectedMethod = $reflectedConsole->getMethod($methodName);
237
        $reflectedMethod->setAccessible(true);
238
        return $reflectedMethod;
239
    }
240
241
    public static function tearDownAfterClass()
242
    {
243
        self::$dbConnection = null;
244
    }
245
}
246