These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | // @codingStandardsIgnoreStart |
||
4 | class TestListener implements \PHPUnit_Framework_TestListener |
||
5 | { |
||
6 | // @codingStandardsIgnoreEnd |
||
7 | private $directory; |
||
8 | private $durationLimit; |
||
9 | |||
10 | /** |
||
11 | * @param string $directory The log directory |
||
12 | * @param float $durationLimit The max execution time in seconds |
||
13 | * after that a test duration time is logged |
||
14 | */ |
||
15 | public function __construct($directory, $durationLimit = 0.1) |
||
16 | { |
||
17 | $this->directory = $directory; |
||
18 | $this->durationLimit = $durationLimit; |
||
19 | } |
||
20 | |||
21 | public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
22 | { |
||
23 | $this->storeAScreenshot($test); |
||
24 | } |
||
25 | |||
26 | public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time) |
||
27 | { |
||
28 | $this->storeAScreenshot($test); |
||
29 | } |
||
30 | |||
31 | public function endTest(\PHPUnit_Framework_Test $test, $time) |
||
32 | { |
||
33 | //$this->storeAScreenshot($test); |
||
34 | if ($time > $this->durationLimit) { |
||
35 | @file_put_contents( |
||
36 | $this->directory . DIRECTORY_SEPARATOR . 'test_duration.log', |
||
37 | sprintf("%.2f sec: %s::%s\n", $time, get_class($test), $test->getName()), |
||
0 ignored issues
–
show
|
|||
38 | FILE_APPEND |
||
39 | ); |
||
40 | } |
||
41 | } |
||
42 | |||
43 | private function storeAScreenshot(\PHPUnit_Framework_Test $test) |
||
44 | { |
||
45 | if ($test instanceof \PHPUnit_Extensions_Selenium2TestCase) { |
||
46 | $className = explode('\\', get_class($test)); |
||
47 | try { |
||
48 | $file = getcwd() . DIRECTORY_SEPARATOR . $this->directory . DIRECTORY_SEPARATOR . end($className); |
||
49 | $file .= '__' |
||
50 | . preg_replace('/[^A-Za-z0-9_\-]/', '_', $test->getName()) |
||
51 | . '__ ' . date('Y-m-d\TH-i-s') . '.png'; |
||
52 | file_put_contents($file, $test->currentScreenshot()); |
||
53 | } catch (\Exception $e) { |
||
54 | $file .= '.txt'; |
||
55 | file_put_contents( |
||
56 | $file, |
||
57 | "Screenshot generation doesn't work." . "\n" . $e->getMessage() . "\n" . $e->getTraceAsString() |
||
58 | ); |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | |||
63 | public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
64 | { |
||
65 | |||
66 | } |
||
67 | |||
68 | public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time) |
||
69 | { |
||
70 | |||
71 | } |
||
72 | |||
73 | public function startTest(\PHPUnit_Framework_Test $test) |
||
74 | { |
||
75 | |||
76 | } |
||
77 | |||
78 | public function startTestSuite(\PHPUnit_Framework_TestSuite $suite) |
||
79 | { |
||
80 | $groups = $suite->getGroups(); |
||
81 | if ($suite instanceof PHPUnit_Extensions_SeleniumTestSuite || |
||
82 | in_array('selenium', $groups) |
||
83 | ) { |
||
84 | $this->runPhantom(); |
||
85 | } |
||
86 | |||
87 | } |
||
88 | |||
89 | public function endTestSuite(\PHPUnit_Framework_TestSuite $suite) |
||
90 | { |
||
91 | |||
92 | } |
||
93 | |||
94 | public function addRiskyTest(\PHPUnit_Framework_Test $test, Exception $e, $time) |
||
95 | { |
||
96 | |||
97 | } |
||
98 | |||
99 | private function setSeleniumCoverageFlag() |
||
100 | { |
||
101 | //create file in tmp folder |
||
102 | $fileName = getcwd() . DIRECTORY_SEPARATOR . |
||
103 | 'app' . DIRECTORY_SEPARATOR . |
||
104 | 'logs' . DIRECTORY_SEPARATOR . |
||
105 | 'selenium.coverage'; |
||
106 | |||
107 | if (file_exists($fileName)) { |
||
108 | unlink($fileName); |
||
109 | } |
||
110 | |||
111 | if (defined('PHPUNIT_SELENIUM_COVERAGE')) { |
||
112 | $file = fopen($fileName, "w"); |
||
113 | fclose($file); |
||
114 | } |
||
115 | } |
||
116 | |||
117 | private function runPhantom() |
||
118 | { |
||
119 | if (strtolower(PHPUNIT_TESTSUITE_EXTENSION_SELENIUM2_BROWSER) == 'phantomjs') { |
||
120 | if (!$this->waitServerRun( |
||
121 | 1, |
||
122 | PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_HOST, |
||
123 | PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_PORT |
||
124 | )) { |
||
125 | if (PHP_OS == 'WINNT') { |
||
126 | pclose( |
||
127 | popen( |
||
128 | "start /b " . PHPUNIT_TESTSUITE_BROWSER_PATH_WINNT . |
||
129 | " --webdriver=" . PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_PORT . |
||
130 | " --disk-cache=true" . |
||
131 | " --ignore-ssl-errors=true", |
||
132 | "r" |
||
133 | ) |
||
134 | ); |
||
135 | } else { |
||
136 | shell_exec( |
||
137 | "nohup " . PHPUNIT_TESTSUITE_BROWSER_PATH_LINUX . |
||
138 | " --webdriver=" . PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_PORT . |
||
139 | " --disk-cache=true" . |
||
140 | " --ignore-ssl-errors=true" . |
||
141 | " > /dev/null 2> /dev/null &" |
||
142 | ); |
||
143 | } |
||
144 | $this->waitServerRun( |
||
145 | 60, |
||
146 | PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_HOST, |
||
147 | PHPUNIT_TESTSUITE_EXTENSION_SELENIUM_PORT |
||
148 | ); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | private function waitServerRun($timeOut = 5, $url = 'localhost', $port = '4444') |
||
154 | { |
||
155 | do { |
||
156 | $fp = @fsockopen($url, (int)$port, $errno, $errstr, 1); |
||
157 | $timeOut--; |
||
158 | if ($timeOut <= 0) { |
||
159 | break; |
||
160 | } |
||
161 | if (!$fp) { |
||
162 | sleep(1); |
||
163 | } |
||
164 | } while (!$fp); |
||
165 | |||
166 | if ($result = is_resource($fp)) { |
||
167 | fclose($fp); |
||
168 | } |
||
169 | return $result; |
||
170 | } |
||
171 | } |
||
172 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: