- Add test case for Symfony Cache style negated static coalesce assignment condition - Add test case for Symfony Serializer style array_map(strval(...)) with Stringable values - Add test case for Symfony ServiceLocator style match count with closure unpack - Add test case for Symfony Workflow style array dim coalesce assignment followed by assign-op - Include comprehensive test scenarios covering edge cases and expected behaviors - Verify proper handling of null coalescing operators and array operations in AOT contextpull/15/head
parent
19d74e459d
commit
7711a91dec
4 changed files with 227 additions and 0 deletions
@ -0,0 +1,37 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony Cache style negated static coalesce assignment condition |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class SymfonyApcuSupportedCase |
||||||
|
{ |
||||||
|
private static ?bool $supported = null; |
||||||
|
public static int $checks = 0; |
||||||
|
|
||||||
|
public static function probe(): bool |
||||||
|
{ |
||||||
|
self::$checks++; |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
|
||||||
|
public static function createSystemCache(): string |
||||||
|
{ |
||||||
|
if (!self::$supported ??= self::probe()) { |
||||||
|
return 'fallback'; |
||||||
|
} |
||||||
|
|
||||||
|
return 'apcu'; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
var_dump(SymfonyApcuSupportedCase::createSystemCache()); |
||||||
|
var_dump(SymfonyApcuSupportedCase::createSystemCache()); |
||||||
|
var_dump(SymfonyApcuSupportedCase::$checks); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(8) "fallback" |
||||||
|
string(8) "fallback" |
||||||
|
int(1) |
||||||
@ -0,0 +1,57 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony Serializer style array_map(strval(...)) with Stringable values |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class SymfonyExpectedTypeName |
||||||
|
{ |
||||||
|
public function __construct(private string $name) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public function __toString(): string |
||||||
|
{ |
||||||
|
return 'type:' . $this->name; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class SymfonyNotNormalizableValueCase extends RuntimeException |
||||||
|
{ |
||||||
|
private ?array $expectedTypes; |
||||||
|
|
||||||
|
public function __construct(?array $expectedTypes = null) |
||||||
|
{ |
||||||
|
parent::__construct('not normalizable'); |
||||||
|
|
||||||
|
$this->expectedTypes = $expectedTypes ? array_map(strval(...), $expectedTypes) : $expectedTypes; |
||||||
|
} |
||||||
|
|
||||||
|
public function getExpectedTypes(): ?array |
||||||
|
{ |
||||||
|
return $this->expectedTypes; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$exception = new SymfonyNotNormalizableValueCase([ |
||||||
|
'int', |
||||||
|
new SymfonyExpectedTypeName('uuid'), |
||||||
|
'array', |
||||||
|
]); |
||||||
|
|
||||||
|
var_dump($exception->getExpectedTypes()); |
||||||
|
|
||||||
|
$empty = new SymfonyNotNormalizableValueCase(); |
||||||
|
var_dump($empty->getExpectedTypes()); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
array(3) { |
||||||
|
[0]=> |
||||||
|
string(3) "int" |
||||||
|
[1]=> |
||||||
|
string(9) "type:uuid" |
||||||
|
[2]=> |
||||||
|
string(5) "array" |
||||||
|
} |
||||||
|
NULL |
||||||
@ -0,0 +1,79 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony ServiceLocator style match count with closure unpack |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class SymfonyServiceLocatorBase |
||||||
|
{ |
||||||
|
public function __construct(array $serviceMap) |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
public function get(string $id): mixed |
||||||
|
{ |
||||||
|
return 'parent:' . $id; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class SymfonyServiceLocatorCase extends SymfonyServiceLocatorBase |
||||||
|
{ |
||||||
|
public function __construct( |
||||||
|
private Closure $factory, |
||||||
|
private array $serviceMap, |
||||||
|
private ?array $serviceTypes = null, |
||||||
|
) { |
||||||
|
parent::__construct($serviceMap); |
||||||
|
} |
||||||
|
|
||||||
|
public function get(string $id): mixed |
||||||
|
{ |
||||||
|
return match (count($this->serviceMap[$id] ?? [])) { |
||||||
|
0 => parent::get($id), |
||||||
|
1 => $this->serviceMap[$id][0], |
||||||
|
default => ($this->factory)(...$this->serviceMap[$id]), |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
public function getProvidedServices(): array |
||||||
|
{ |
||||||
|
return $this->serviceTypes ??= array_map(static fn () => '?', $this->serviceMap); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$locator = new SymfonyServiceLocatorCase( |
||||||
|
static fn (string $class, string $method): string => $class . '::' . $method, |
||||||
|
[ |
||||||
|
'missing' => [], |
||||||
|
'single' => ['ready'], |
||||||
|
'factory' => ['App\Service', 'build'], |
||||||
|
] |
||||||
|
); |
||||||
|
|
||||||
|
var_dump($locator->get('missing')); |
||||||
|
var_dump($locator->get('single')); |
||||||
|
var_dump($locator->get('factory')); |
||||||
|
var_dump($locator->getProvidedServices()); |
||||||
|
var_dump($locator->getProvidedServices()); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
string(14) "parent:missing" |
||||||
|
string(5) "ready" |
||||||
|
string(18) "App\Service::build" |
||||||
|
array(3) { |
||||||
|
["missing"]=> |
||||||
|
string(1) "?" |
||||||
|
["single"]=> |
||||||
|
string(1) "?" |
||||||
|
["factory"]=> |
||||||
|
string(1) "?" |
||||||
|
} |
||||||
|
array(3) { |
||||||
|
["missing"]=> |
||||||
|
string(1) "?" |
||||||
|
["single"]=> |
||||||
|
string(1) "?" |
||||||
|
["factory"]=> |
||||||
|
string(1) "?" |
||||||
|
} |
||||||
@ -0,0 +1,54 @@ |
|||||||
|
--TEST-- |
||||||
|
Symfony Workflow style array dim coalesce assignment followed by assign-op |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class SymfonyWorkflowMarkingCase |
||||||
|
{ |
||||||
|
private array $places = []; |
||||||
|
|
||||||
|
public function __construct(array $representation = []) |
||||||
|
{ |
||||||
|
foreach ($representation as $place => $nbToken) { |
||||||
|
$this->mark($place, $nbToken); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
public function mark(string $place, int $nbToken = 1): void |
||||||
|
{ |
||||||
|
if ($nbToken < 1) { |
||||||
|
throw new InvalidArgumentException(sprintf('The number of tokens must be greater than 0, "%s" given.', $nbToken)); |
||||||
|
} |
||||||
|
|
||||||
|
$this->places[$place] ??= 0; |
||||||
|
$this->places[$place] += $nbToken; |
||||||
|
} |
||||||
|
|
||||||
|
public function getPlaces(): array |
||||||
|
{ |
||||||
|
return $this->places; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main(): void |
||||||
|
{ |
||||||
|
$marking = new SymfonyWorkflowMarkingCase(['draft' => 1]); |
||||||
|
$marking->mark('draft', 2); |
||||||
|
$marking->mark('review', 3); |
||||||
|
|
||||||
|
var_dump($marking->getPlaces()); |
||||||
|
|
||||||
|
try { |
||||||
|
$marking->mark('invalid', 0); |
||||||
|
} catch (InvalidArgumentException $e) { |
||||||
|
echo $e->getMessage(), "\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
array(2) { |
||||||
|
["draft"]=> |
||||||
|
int(3) |
||||||
|
["review"]=> |
||||||
|
int(3) |
||||||
|
} |
||||||
|
The number of tokens must be greater than 0, "0" given. |
||||||
Loading…
Reference in new issue