1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Particletree\Pqp; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use ReflectionClass; |
7
|
|
|
|
8
|
|
|
class DisplayTest extends PHPUnit_Framework_TestCase |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
public function testConstruct() |
12
|
|
|
{ |
13
|
|
|
$display = new Display(); |
14
|
|
|
$reflectedDisplay = new ReflectionClass(get_class($display)); |
15
|
|
|
$reflectedProperty = $reflectedDisplay->getProperty('defaults'); |
16
|
|
|
$reflectedProperty->setAccessible(true); |
17
|
|
|
$defaults = $reflectedProperty->getValue($display); |
18
|
|
|
|
19
|
|
|
$display = new Display(); |
20
|
|
|
$this->assertAttributeEquals($defaults, 'options', $display); |
21
|
|
|
|
22
|
|
|
$options = array( |
23
|
|
|
'script_path' => 'testing/testing.js', |
24
|
|
|
'fake_key' => 'foo bar' |
25
|
|
|
); |
26
|
|
|
$expectedOptions = array_intersect_key($options, $defaults); |
27
|
|
|
$expectedOptions = array_replace($defaults, $expectedOptions); |
28
|
|
|
$display = new Display($options); |
29
|
|
|
$this->assertAttributeEquals($expectedOptions, 'options', $display); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
View Code Duplication |
public function testGetReadableTime() |
|
|
|
|
33
|
|
|
{ |
34
|
|
|
$timeTest = array( |
35
|
|
|
'.032432' => '32.432 ms', |
36
|
|
|
'24.3781' => '24.378 s', |
37
|
|
|
'145.123' => '2.419 m' |
38
|
|
|
); |
39
|
|
|
$display = new Display(); |
40
|
|
|
$reflectedMethod = $this->getAccessibleMethod($display, 'getReadableTime'); |
41
|
|
|
|
42
|
|
|
foreach ($timeTest as $rawTime => $expectedTime) { |
43
|
|
|
$readableTime = $reflectedMethod->invokeArgs($display, array($rawTime)); |
44
|
|
|
$this->assertEquals($expectedTime, $readableTime); |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
View Code Duplication |
public function testGetReadableMemory() |
|
|
|
|
49
|
|
|
{ |
50
|
|
|
$memoryTest = array( |
51
|
|
|
'314' => '314 b', |
52
|
|
|
'7403' => '7.23 k', |
53
|
|
|
'2589983' => '2.47 M' |
54
|
|
|
); |
55
|
|
|
$display = new Display(); |
56
|
|
|
$reflectedMethod = $this->getAccessibleMethod($display, 'getReadableMemory'); |
57
|
|
|
|
58
|
|
|
foreach ($memoryTest as $rawMemory => $expectedMemory) { |
59
|
|
|
$readableMemory = $reflectedMethod->invokeArgs($display, array($rawMemory)); |
60
|
|
|
$this->assertEquals($expectedMemory, $readableMemory); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
protected function getAccessibleMethod(Display $display, $methodName) |
65
|
|
|
{ |
66
|
|
|
$reflectedConsole = new ReflectionClass(get_class($display)); |
67
|
|
|
$reflectedMethod = $reflectedConsole->getMethod($methodName); |
68
|
|
|
$reflectedMethod->setAccessible(true); |
69
|
|
|
return $reflectedMethod; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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.