Enalean /
tuleap
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Copyright (c) Enalean, 2012. All Rights Reserved. |
||
| 4 | * |
||
| 5 | * This file is a part of Tuleap. |
||
| 6 | * |
||
| 7 | * Tuleap is free software; you can redistribute it and/or modify |
||
| 8 | * it under the terms of the GNU General Public License as published by |
||
| 9 | * the Free Software Foundation; either version 2 of the License, or |
||
| 10 | * (at your option) any later version. |
||
| 11 | * |
||
| 12 | * Tuleap is distributed in the hope that it will be useful, |
||
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 15 | * GNU General Public License for more details. |
||
| 16 | * |
||
| 17 | * You should have received a copy of the GNU General Public License |
||
| 18 | * along with Tuleap. If not, see <http://www.gnu.org/licenses/>. |
||
| 19 | */ |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Returns a DSL like mockgenerator |
||
| 23 | * |
||
| 24 | * <code> |
||
| 25 | * stub('someclass')->someMethod($arg1, $arg2, ...)->returns($someResult); |
||
| 26 | * </code> |
||
| 27 | * |
||
| 28 | * that is an alternative to : |
||
| 29 | * |
||
| 30 | * <code> |
||
| 31 | * Mock::generate('SomeClass'); |
||
| 32 | * $mock = new MockSomeClass(); |
||
| 33 | * $mock->setReturnValue('someMethod', $someResult, array($arg1, $arg2, ...); |
||
| 34 | * </code> |
||
| 35 | * |
||
| 36 | * @param a class name or a simpletest mock |
||
| 37 | * |
||
| 38 | * @return \OngoingIntelligentStub |
||
| 39 | */ |
||
| 40 | function stub($classname_or_simpletest_mock) { |
||
| 41 | if (is_object($classname_or_simpletest_mock)) { |
||
| 42 | $mock = $classname_or_simpletest_mock; |
||
| 43 | } else { |
||
| 44 | $mock = mock($classname_or_simpletest_mock); |
||
| 45 | } |
||
| 46 | return new OngoingIntelligentStub($mock); |
||
| 47 | } |
||
| 48 | |||
| 49 | function expect($classname_or_simpletest_mock) { |
||
| 50 | return stub($classname_or_simpletest_mock); |
||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * mock('SomeClass'); |
||
| 55 | * |
||
| 56 | * is exactly the same as |
||
| 57 | * |
||
| 58 | * <code> |
||
| 59 | * Mock::generate('SomeClass'); |
||
| 60 | * $mock = new MockSomeClass(); |
||
| 61 | * </code> |
||
| 62 | * |
||
| 63 | * @param type $classname |
||
| 64 | * |
||
| 65 | * @return a simpletest mock |
||
| 66 | */ |
||
| 67 | function mock($classname) { |
||
| 68 | $mockclassname = "Mock$classname"; |
||
| 69 | if (strpos($classname, '\\') !== false) { |
||
| 70 | $mockclassname = "Mock". str_replace('\\', '_', $classname); |
||
| 71 | } |
||
| 72 | Mock::generate($classname, $mockclassname); |
||
| 73 | return new $mockclassname(); |
||
| 74 | } |
||
| 75 | |||
| 76 | function partial_stub($classname_or_simpletest_mock, array $mocked_methods) { |
||
| 77 | if (is_object($classname_or_simpletest_mock)) { |
||
| 78 | $mock = $classname_or_simpletest_mock; |
||
| 79 | } else { |
||
| 80 | $mock = partial_mock($classname_or_simpletest_mock); |
||
|
0 ignored issues
–
show
|
|||
| 81 | } |
||
| 82 | return new OngoingIntelligentStub($mock); |
||
| 83 | } |
||
| 84 | |||
| 85 | function partial_mock($classname, array $mocked_methods, array $construct_params = null) { |
||
| 86 | $object = TestHelper::getPartialMock($classname, $mocked_methods); |
||
| 87 | if ($construct_params) { |
||
| 88 | call_user_func_array(array($object, '__construct'), $construct_params); |
||
| 89 | } |
||
| 90 | return $object; |
||
| 91 | } |
||
| 92 | |||
| 93 | class OngoingIntelligentStub { |
||
| 94 | public $mock; |
||
| 95 | private $method; |
||
| 96 | private $arguments; |
||
| 97 | |||
| 98 | function __construct($mock) { |
||
| 99 | $this->mock = $mock; |
||
| 100 | } |
||
| 101 | |||
| 102 | public function __call($name, $arguments) { |
||
| 103 | if ($this->method) { |
||
| 104 | throw new Exception("Cannot stub '{$name}()', method '{$this->method}()' already stubbed. Wrong usage of stub()"); |
||
| 105 | } |
||
| 106 | |||
| 107 | $this->method = $name; |
||
| 108 | $this->arguments = $arguments; |
||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function once($message = '%s') { |
||
| 113 | if (empty($this->arguments)) { |
||
| 114 | $this->mock->expectOnce($this->method, false, $message); |
||
| 115 | } else { |
||
| 116 | $this->mock->expectOnce($this->method, $this->arguments, $message); |
||
| 117 | } |
||
| 118 | return $this; |
||
| 119 | } |
||
| 120 | |||
| 121 | public function atLeastOnce($message = '%s') { |
||
| 122 | if (empty($this->arguments)) { |
||
| 123 | $this->mock->expectAtLeastOnce($this->method, false, $message); |
||
| 124 | } else { |
||
| 125 | $this->mock->expectAtLeastOnce($this->method, $this->arguments, $message); |
||
| 126 | } |
||
| 127 | return $this; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function never() { |
||
| 131 | $this->mock->expectNever($this->method); |
||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function at($timing) { |
||
| 136 | $this->mock->expectAt($timing, $this->method, $this->arguments); |
||
| 137 | return $this; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function count($count) { |
||
| 141 | $this->mock->expectCallCount($this->method, $count); |
||
| 142 | return $this; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @return the configured mock |
||
| 147 | */ |
||
| 148 | public function returns($value) { |
||
| 149 | if (empty($this->arguments)) { |
||
| 150 | $this->mock->setReturnValue($this->method, $value); |
||
| 151 | } else { |
||
| 152 | $this->mock->setReturnValue($this->method, $value, $this->arguments); |
||
| 153 | } |
||
| 154 | return $this->mock; |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * @return the configured mock |
||
| 159 | */ |
||
| 160 | public function returnsAt($timing, $value) { |
||
| 161 | $this->mock->setReturnValueAt($timing, $this->method, $value); |
||
| 162 | return $this->mock; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Ease return of DatabaseAccessResult objects: |
||
| 167 | * |
||
| 168 | * Example: |
||
| 169 | * stub('Dao')->getStuff()->returnsDar(array('id' => '1'), array('id' => '2')); |
||
| 170 | * |
||
| 171 | * Returns 2 rows out of the database: |
||
| 172 | * |Id| |
||
| 173 | * |1 | |
||
| 174 | * |2 | |
||
| 175 | */ |
||
| 176 | public function returnsDar() { |
||
| 177 | return $this->returnsDarFromArray(func_get_args()); |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Ease return of DatabaseAccessResult objects: |
||
| 182 | * |
||
| 183 | * Example: |
||
| 184 | * stub('Dao')->getStuff()->returnsDarFromArray( |
||
| 185 | * array( |
||
| 186 | * array('id' => '1'), |
||
| 187 | * array('id' => '2') |
||
| 188 | * ) |
||
| 189 | * ); |
||
| 190 | * |
||
| 191 | * Returns 2 rows out of the database: |
||
| 192 | * |Id| |
||
| 193 | * |1 | |
||
| 194 | * |2 | |
||
| 195 | */ |
||
| 196 | public function returnsDarFromArray($array) { |
||
| 197 | return $this->returns(TestHelper::argListToDar($array)); |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Ease returns of empty DatabaseAccessResult |
||
| 202 | * |
||
| 203 | * Example: |
||
| 204 | * stub('Dao')->getStuff()->returnsEmptyDar() |
||
| 205 | */ |
||
| 206 | public function returnsEmptyDar() { |
||
| 207 | return $this->returns(TestHelper::emptyDar()); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Ease returns of DatabaseAccessResult with errors |
||
| 212 | * |
||
| 213 | * Example: |
||
| 214 | * stub('Dao')->getStuff()->returnsDarWithErrors() |
||
| 215 | */ |
||
| 216 | public function returnsDarWithErrors() { |
||
| 217 | return $this->returns(TestHelper::errorDar()); |
||
| 218 | } |
||
| 219 | |||
| 220 | public function throws(Exception $e) { |
||
| 221 | if (empty($this->arguments)) { |
||
| 222 | $this->mock->throwOn($this->method, $e); |
||
| 223 | } else { |
||
| 224 | $this->mock->throwOn($this->method, $e, $this->arguments); |
||
| 225 | } |
||
| 226 | return $this->mock; |
||
| 227 | } |
||
| 228 | |||
| 229 | public function throwsAt($timing, Exception $e) { |
||
| 230 | if (empty($this->arguments)) { |
||
| 231 | $this->mock->throwAt($timing, $this->method, $e); |
||
| 232 | } else { |
||
| 233 | $this->mock->throwAt($timing, $this->method, $e, $this->arguments); |
||
| 234 | } |
||
| 235 | return $this->mock; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | ?> |
||
| 239 |
This check looks for function calls that miss required arguments.