|
1 | 1 | <?php |
2 | 2 | /** |
3 | | - * Zend Framework (http://framework.zend.com/) |
4 | | - * |
5 | | - * @link http://github.com/zendframework/zf2 for the canonical source repository |
6 | | - * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
7 | | - * @license http://framework.zend.com/license/new-bsd New BSD License |
| 3 | + * @see https://github.com/zendframework/zend-serializer for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com) |
| 5 | + * @license https://github.com/zendframework/zend-serializer/blob/master/LICENSE.md New BSD License |
8 | 6 | */ |
9 | 7 |
|
10 | 8 | namespace ZendTest\Serializer\Adapter; |
11 | 9 |
|
12 | 10 | use PHPUnit\Framework\TestCase; |
| 11 | +use stdClass; |
13 | 12 | use Zend\Serializer; |
14 | 13 | use Zend\Serializer\Exception\InvalidArgumentException; |
15 | 14 |
|
@@ -167,65 +166,59 @@ public function testUnserializingInvalidStringRaisesException($string, $expected |
167 | 166 | $this->adapter->unserialize($string); |
168 | 167 | } |
169 | 168 |
|
170 | | - public function testUnserializeNoWhitelistedClasses() |
| 169 | + /** |
| 170 | + * @requires PHP 7.0 |
| 171 | + */ |
| 172 | + public function testPhp7WillNotUnserializeObjectsWhenUnserializeWhitelistedClassesIsFalse() |
171 | 173 | { |
172 | 174 | $value = 'O:8:"stdClass":0:{}'; |
| 175 | + $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
173 | 176 |
|
174 | | - if (PHP_MAJOR_VERSION >= 7) { |
175 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
176 | | - |
177 | | - $data = $this->adapter->unserialize($value); |
178 | | - |
179 | | - $this->assertNotInstanceOf(\stdClass::class, $data); |
180 | | - $this->assertInstanceOf('__PHP_Incomplete_Class', $data); |
181 | | - } else { |
182 | | - // In PHP < 7.0 the options-class will throw an exception |
183 | | - |
184 | | - self::expectException(InvalidArgumentException::class); |
185 | | - self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.'); |
| 177 | + $data = $this->adapter->unserialize($value); |
186 | 178 |
|
187 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
188 | | - } |
| 179 | + $this->assertNotInstanceOf(stdClass::class, $data); |
| 180 | + $this->assertInstanceOf('__PHP_Incomplete_Class', $data); |
189 | 181 | } |
190 | 182 |
|
191 | | - public function testUnserializeClassNotAllowed() |
| 183 | + public function testWhenUnserializeClassWhiteListIsFalseButPHPIsPriorTo7AnExceptionIsRaised() |
192 | 184 | { |
193 | 185 | $value = 'O:8:"stdClass":0:{}'; |
194 | 186 |
|
195 | 187 | if (PHP_MAJOR_VERSION >= 7) { |
196 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist([\My\Dummy::class]); |
| 188 | + $this->markTestSkipped(sprintf('Test %s is only needed in PHP versions prior to 7.0', __FUNCTION__)); |
| 189 | + } |
197 | 190 |
|
198 | | - $data = $this->adapter->unserialize($value); |
| 191 | + self::expectException(InvalidArgumentException::class); |
| 192 | + self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.'); |
| 193 | + $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * @requires PHP 7.0 |
| 198 | + */ |
| 199 | + public function testUnserializeWillNotUnserializeClassesThatAreNotInTheWhitelist() |
| 200 | + { |
| 201 | + $value = 'O:8:"stdClass":0:{}'; |
199 | 202 |
|
200 | | - $this->assertNotInstanceOf(\stdClass::class, $data); |
201 | | - $this->assertInstanceOf('__PHP_Incomplete_Class', $data); |
202 | | - } else { |
203 | | - // In PHP < 7.0 the options-class will throw an exception |
| 203 | + $this->adapter->getOptions()->setUnserializeClassWhitelist([\My\Dummy::class]); |
204 | 204 |
|
205 | | - self::expectException(InvalidArgumentException::class); |
206 | | - self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.'); |
| 205 | + $data = $this->adapter->unserialize($value); |
207 | 206 |
|
208 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
209 | | - } |
| 207 | + $this->assertNotInstanceOf(stdClass::class, $data); |
| 208 | + $this->assertInstanceOf('__PHP_Incomplete_Class', $data); |
210 | 209 | } |
211 | 210 |
|
212 | | - public function testUnserializeClassAllowed() |
| 211 | + /** |
| 212 | + * @requires PHP 7.0 |
| 213 | + */ |
| 214 | + public function testUnserializeWillUnserializeAnyClassWhenUnserializeWhitelistedClassesIsTrue() |
213 | 215 | { |
214 | 216 | $value = 'O:8:"stdClass":0:{}'; |
215 | 217 |
|
216 | | - if (PHP_MAJOR_VERSION >= 7) { |
217 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist([\stdClass::class]); |
218 | | - |
219 | | - $data = $this->adapter->unserialize($value); |
220 | | - $this->assertInstanceOf(\stdClass::class, $data); |
221 | | - $this->assertNotInstanceOf('__PHP_Incomplete_Class', $data); |
222 | | - } else { |
223 | | - // In PHP < 7.0 the options-class will throw an exception |
| 218 | + $this->adapter->getOptions()->setUnserializeClassWhitelist([stdClass::class]); |
224 | 219 |
|
225 | | - self::expectException(InvalidArgumentException::class); |
226 | | - self::expectExceptionMessage('Class whitelist for unserialize() is only available on PHP 7.0 or higher.'); |
227 | | - |
228 | | - $this->adapter->getOptions()->setUnserializeClassWhitelist(false); |
229 | | - } |
| 220 | + $data = $this->adapter->unserialize($value); |
| 221 | + $this->assertInstanceOf(stdClass::class, $data); |
| 222 | + $this->assertNotInstanceOf('__PHP_Incomplete_Class', $data); |
230 | 223 | } |
231 | 224 | } |
0 commit comments