1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
|
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Polyfill\Tests\Php80;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
/**
* @author Ion Bazan <ion.bazan@gmail.com>
* @author Nico Oelgart <nicoswd@gmail.com>
*
* @group legacy
*/
class Php80Test extends TestCase
{
/**
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
*
* @dataProvider fdivProvider
*/
#[DataProvider('fdivProvider')]
public function testFdiv($expected, $divident, $divisor)
{
try {
$result = fdiv($divident, $divisor);
} catch (\DivisionByZeroError $e) {
$result = $expected;
}
$this->assertSame($expected, $result);
// Cast to string to detect negative zero "-0"
$this->assertSame((string) $expected, (string) $result);
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
*
* @dataProvider nanFdivProvider
*/
#[DataProvider('nanFdivProvider')]
public function testFdivNan($divident, $divisor)
{
try {
$this->assertNan(fdiv($divident, $divisor));
} catch (\DivisionByZeroError $e) {
$this->assertNan(\NAN);
}
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::fdiv
*
* @dataProvider invalidFloatProvider
*/
#[DataProvider('invalidFloatProvider')]
public function testFdivTypeError($divident, $divisor)
{
$this->expectException('TypeError');
fdiv($divident, $divisor);
}
public function testFilterValidateBool()
{
$this->assertTrue(\defined('FILTER_VALIDATE_BOOL'));
$this->assertSame(\FILTER_VALIDATE_BOOLEAN, \FILTER_VALIDATE_BOOL);
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
*/
public function testPregNoError()
{
$this->assertSame('No error', preg_last_error_msg());
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
*/
public function testPregMalformedUtfError()
{
@preg_split('/a/u', "a\xff");
$this->assertSame('Malformed UTF-8 characters, possibly incorrectly encoded', preg_last_error_msg());
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::preg_last_error_msg
*/
public function testPregMalformedUtf8Offset()
{
@preg_match('/a/u', "\xE3\x82\xA2", $m, 0, 1);
$this->assertSame(
'The offset did not correspond to the beginning of a valid UTF-8 code point',
preg_last_error_msg()
);
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::str_contains
*/
public function testStrContains()
{
$this->assertTrue(str_contains('abc', ''));
$this->assertTrue(str_contains('abc', null));
$this->assertTrue(str_contains('abc', 'a'));
$this->assertTrue(str_contains('abc', 'bc'));
$this->assertTrue(str_contains('abc', 'abc'));
$this->assertTrue(str_contains('한국어', '국'));
$this->assertTrue(str_contains('한국어', ''));
$this->assertTrue(str_contains('', ''));
$this->assertFalse(str_contains('abc', 'd'));
$this->assertFalse(str_contains('', 'd'));
$this->assertFalse(str_contains(null, 'd'));
$this->assertFalse(str_contains('abc', 'abcd'));
$this->assertFalse(str_contains('DÉJÀ', 'à'));
$this->assertFalse(str_contains('a', 'à'));
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::str_starts_with
*/
public function testStrStartsWith()
{
$testStr = 'beginningMiddleEnd';
$this->assertTrue(str_starts_with($testStr, 'beginning'));
$this->assertTrue(str_starts_with($testStr, $testStr));
$this->assertTrue(str_starts_with($testStr, ''));
$this->assertTrue(str_starts_with($testStr, null));
$this->assertTrue(str_starts_with('', ''));
$this->assertTrue(str_starts_with(null, ''));
$this->assertTrue(str_starts_with("\x00", ''));
$this->assertTrue(str_starts_with("\x00", "\x00"));
$this->assertTrue(str_starts_with("\x00a", "\x00"));
$this->assertTrue(str_starts_with("a\x00bc", "a\x00b"));
$this->assertFalse(str_starts_with($testStr, 'Beginning'));
$this->assertFalse(str_starts_with($testStr, 'eginning'));
$this->assertFalse(str_starts_with($testStr, $testStr.$testStr));
$this->assertFalse(str_starts_with('', ' '));
$this->assertFalse(str_starts_with($testStr, "\x00"));
$this->assertFalse(str_starts_with("a\x00b", "a\x00d"));
$this->assertFalse(str_starts_with("a\x00b", "z\x00b"));
$this->assertFalse(str_starts_with('a', "a\x00"));
$this->assertFalse(str_starts_with('a', "\x00a"));
// අයේෂ් = අ + ය + "ේ" + ෂ + ්
// අයේෂ් = (0xe0 0xb6 0x85) + (0xe0 0xb6 0xba) + (0xe0 0xb7 0x9a) + (0xe0 0xb7 0x82) + (0xe0 0xb7 0x8a)
$testMultiByte = 'අයේෂ්'; // 0xe0 0xb6 0x85 0xe0 0xb6 0xba 0xe0 0xb7 0x9a 0xe0 0xb7 0x82 0xe0 0xb7 0x8a
$this->assertTrue(str_starts_with($testMultiByte, 'අයේ')); // 0xe0 0xb6 0x85 0xe0 0xb6 0xba 0xe0 0xb7 0x9a
$this->assertTrue(str_starts_with($testMultiByte, 'අය')); // 0xe0 0xb6 0x85 0xe0 0xb6 0xba
$this->assertFalse(str_starts_with($testMultiByte, 'ය')); // 0xe0 0xb6 0xba
$this->assertFalse(str_starts_with($testMultiByte, 'අේ')); // 0xe0 0xb6 0x85 0xe0 0xb7 0x9a
$testEmoji = '🙌🎉✨🚀'; // 0xf0 0x9f 0x99 0x8c 0xf0 0x9f 0x8e 0x89 0xe2 0x9c 0xa8 0xf0 0x9f 0x9a 0x80
$this->assertTrue(str_starts_with($testEmoji, '🙌')); // 0xf0 0x9f 0x99 0x8c
$this->assertFalse(str_starts_with($testEmoji, '✨')); // 0xe2 0x9c 0xa8
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::str_ends_with
*/
public function testStrEndsWith()
{
$testStr = 'beginningMiddleEnd';
$this->assertTrue(str_ends_with($testStr, 'End'));
$this->assertFalse(str_ends_with($testStr, 'end'));
$this->assertFalse(str_ends_with($testStr, 'en'));
$this->assertTrue(str_ends_with($testStr, $testStr));
$this->assertFalse(str_ends_with($testStr, $testStr.$testStr));
$this->assertTrue(str_ends_with($testStr, ''));
$this->assertTrue(str_ends_with($testStr, null));
$this->assertTrue(str_ends_with('', ''));
$this->assertTrue(str_ends_with(null, ''));
$this->assertFalse(str_ends_with('', ' '));
$this->assertFalse(str_ends_with($testStr, "\x00"));
$this->assertTrue(str_ends_with("\x00", ''));
$this->assertTrue(str_ends_with("\x00", "\x00"));
$this->assertTrue(str_ends_with("a\x00", "\x00"));
$this->assertTrue(str_ends_with("ab\x00c", "b\x00c"));
$this->assertFalse(str_ends_with("a\x00b", "d\x00b"));
$this->assertFalse(str_ends_with("a\x00b", "a\x00z"));
$this->assertFalse(str_ends_with('a', "\x00a"));
$this->assertFalse(str_ends_with('a', "a\x00"));
$testMultiByte = 'අයේෂ්'; // 0xe0 0xb6 0x85 0xe0 0xb6 0xba 0xe0 0xb7 0x9a 0xe0 0xb7 0x82 0xe0 0xb7 0x8a
$this->assertTrue(str_ends_with($testMultiByte, 'ෂ්')); // 0xe0 0xb7 0x82 0xe0 0xb7 0x8a
$this->assertTrue(str_ends_with($testMultiByte, '්')); // 0xe0 0xb7 0x8a
$this->assertFalse(str_ends_with($testMultiByte, 'ෂ')); // 0xe0 0xb7 0x82
$testEmoji = '🙌🎉✨🚀'; // 0xf0 0x9f 0x99 0x8c 0xf0 0x9f 0x8e 0x89 0xe2 0x9c 0xa8 0xf0 0x9f 0x9a 0x80
$this->assertTrue(str_ends_with($testEmoji, '🚀')); // 0xf0 0x9f 0x9a 0x80
$this->assertFalse(str_ends_with($testEmoji, '✨')); // 0xe2 0x9c 0xa8
$this->assertFalse(str_ends_with('', '[]'));
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::get_resource_id
*/
public function testGetResourceIdWithValidResource()
{
$resource = fopen(__FILE__, 'r');
$resourceId = (int) $resource;
$this->assertSame($resourceId, get_resource_id($resource));
fclose($resource);
$this->assertSame($resourceId, get_resource_id($resource));
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::get_resource_id
*
* @dataProvider invalidResourceProvider
*/
#[DataProvider('invalidResourceProvider')]
public function testGetResourceWithInvalidValue($value)
{
$this->expectException('TypeError');
get_resource_id($value);
}
public static function fdivProvider()
{
return [
[10 / 3, '10', '3'],
[10 / 3, 10.0, 3.0],
[-4.0, -10.0, 2.5],
[-4.0, 10.0, -2.5],
[\INF, 10.0, 0.0],
[-\INF, 10.0, -0.0],
[-\INF, -10.0, 0.0],
[\INF, -10.0, -0.0],
[\INF, \INF, 0.0],
[-\INF, \INF, -0.0],
[-\INF, -\INF, 0.0],
[\INF, -\INF, -0.0],
[0.0, 0.0, \INF],
[-0.0, 0.0, -\INF],
[-0.0, -0.0, \INF],
[0.0, -0.0, -\INF],
];
}
public static function nanFdivProvider()
{
return [
[0.0, 0.0],
[0.0, -0.0],
[-0.0, 0.0],
[-0.0, -0.0],
[\INF, \INF],
[\INF, -\INF],
[-\INF, \INF],
[-\INF, -\INF],
[\NAN, \NAN],
[\INF, \NAN],
[-0.0, \NAN],
[\NAN, \INF],
[\NAN, 0.0],
];
}
public static function invalidFloatProvider()
{
return [
['invalid', 1.0],
['invalid', 'invalid'],
[1.0, 'invalid'],
];
}
public static function invalidResourceProvider()
{
return [
[true],
[null],
[new \stdClass()],
['test'],
[10],
[10.0],
];
}
/**
* @covers \Symfony\Polyfill\Php80\Php80::get_debug_type
*/
public function testGetDebugType()
{
$this->assertSame(__CLASS__, get_debug_type($this));
$this->assertSame('stdClass', get_debug_type(new \stdClass()));
$this->assertSame('class@anonymous', get_debug_type(eval('return new class() {};')));
$this->assertSame('stdClass@anonymous', get_debug_type(eval('return new class() extends stdClass {};')));
$this->assertSame('Reflector@anonymous', get_debug_type(eval('return new class() implements Reflector { function __toString() {} public static function export() {} };')));
$this->assertSame('string', get_debug_type('foo'));
$this->assertSame('bool', get_debug_type(false));
$this->assertSame('bool', get_debug_type(true));
$this->assertSame('null', get_debug_type(null));
$this->assertSame('array', get_debug_type([]));
$this->assertSame('int', get_debug_type(1));
$this->assertSame('float', get_debug_type(1.2));
$this->assertSame('resource (stream)', get_debug_type($h = fopen(__FILE__, 'r')));
$this->assertSame('resource (closed)', get_debug_type(fclose($h) ? $h : $h));
$unserializeCallbackHandler = ini_set('unserialize_callback_func', null);
$var = unserialize('O:8:"Foo\Buzz":0:{}');
ini_set('unserialize_callback_func', $unserializeCallbackHandler);
$this->assertSame('__PHP_Incomplete_Class', get_debug_type($var));
}
public function testAttributePolyfill()
{
$attribute = new \Attribute();
$this->assertSame(\Attribute::TARGET_ALL, $attribute->flags);
$attribute = new \Attribute(\Attribute::TARGET_CLASS);
$this->assertSame(\Attribute::TARGET_CLASS, $attribute->flags);
}
}
|