3 changed files with 146 additions and 59 deletions
-
60Zend/tests/list_keyed_evaluation_order.inc
-
68Zend/tests/list_keyed_evaluation_order.phpt
-
77Zend/tests/list_keyed_evaluation_order_nested.phpt
@ -0,0 +1,60 @@ |
|||
<?php declare(strict_types=1); |
|||
|
|||
// Observer objects for the Zend/tests/list_keyed_evaluation_order.* tests |
|||
|
|||
class Stringable |
|||
{ |
|||
private $name; |
|||
public function __construct(string $name) { |
|||
$this->name = $name; |
|||
} |
|||
|
|||
public function __toString(): string { |
|||
echo "$this->name evaluated.", PHP_EOL; |
|||
return $this->name; |
|||
} |
|||
} |
|||
|
|||
class Indexable implements ArrayAccess |
|||
{ |
|||
private $array; |
|||
public function __construct(array $array) { |
|||
$this->array = $array; |
|||
} |
|||
|
|||
public function offsetExists($offset): bool { |
|||
echo "Existence of offset $offset checked for.", PHP_EOL; |
|||
return isset($this->array[$offset]); |
|||
} |
|||
|
|||
public function offsetUnset($offset): void { |
|||
unset($this->array[$offset]); |
|||
echo "Offset $offset removed.", PHP_EOL; |
|||
} |
|||
|
|||
public function offsetGet($offset) { |
|||
echo "Offset $offset retrieved.", PHP_EOL; |
|||
return $this->array[$offset]; |
|||
} |
|||
|
|||
public function offsetSet($offset, $value): void { |
|||
$this->array[$offset] = $value; |
|||
echo "Offset $offset set to $value.", PHP_EOL; |
|||
} |
|||
} |
|||
|
|||
class IndexableRetrievable |
|||
{ |
|||
private $label; |
|||
private $indexable; |
|||
|
|||
public function __construct(string $label, Indexable $indexable) { |
|||
$this->label = $label; |
|||
$this->indexable = $indexable; |
|||
} |
|||
|
|||
public function getIndexable(): Indexable { |
|||
echo "Indexable $this->label retrieved.", PHP_EOL; |
|||
return $this->indexable; |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
--TEST-- |
|||
list() with keys, evaluation order: nested |
|||
--FILE-- |
|||
<?php |
|||
|
|||
require_once "list_keyed_evaluation_order.inc"; |
|||
|
|||
$a = new Stringable("A"); |
|||
$c = new Stringable("C"); |
|||
$f = new Stringable("F"); |
|||
$g = new Stringable("G"); |
|||
$i = new Stringable("I"); |
|||
|
|||
$k = new IndexableRetrievable("K", new Indexable([ |
|||
"A" => "offset value for A", |
|||
"C" => new Indexable([ |
|||
0 => "offset value for 0", |
|||
1 => "offset value for 1" |
|||
]), |
|||
"F" => new Indexable([ |
|||
"G" => "offset value for G", |
|||
"I" => "offset value for I" |
|||
]) |
|||
])); |
|||
|
|||
$store = new Indexable([]); |
|||
|
|||
// list($a => $b, $c => list($d, $e), $f => list($g => $h, $i => $j)) = $k; |
|||
// Should be evaluated in the order: |
|||
// 1. Evaluate $k |
|||
// 2. Evaluate $a |
|||
// 3. Evaluate $k[$a] |
|||
// 4. Assign $b from $k[$a] |
|||
// 5. Evaluate $c |
|||
// 6. Evaluate $k[$c] |
|||
// 7. Evaluate $k[$c][0] |
|||
// 8. Assign $d from $k[$c][0] |
|||
// 9. Evaluate $k[$c][1] |
|||
// 10. Assign $e from $k[$c][1] |
|||
// 11. Evaluate $f |
|||
// 12. Evaluate $k[$f] |
|||
// 13. Evaluate $g |
|||
// 14. Evaluate $k[$f][$g] |
|||
// 15. Assign $h from $k[$f][$g] |
|||
// 16. Evaluate $i |
|||
// 17. Evaluate $k[$f][$i] |
|||
// 18. Assign $j from $k[$f][$i] |
|||
|
|||
list( |
|||
(string)$a => $store["B"], |
|||
(string)$c => list($store["D"], $store["E"]), |
|||
(string)$f => list( |
|||
(string)$g => $store["H"], |
|||
(string)$i => $store["J"] |
|||
) |
|||
) = $k->getIndexable(); |
|||
|
|||
?> |
|||
--EXPECT-- |
|||
Indexable K retrieved. |
|||
A evaluated. |
|||
Offset A retrieved. |
|||
Offset B set to offset value for A. |
|||
C evaluated. |
|||
Offset C retrieved. |
|||
Offset 0 retrieved. |
|||
Offset D set to offset value for 0. |
|||
Offset 1 retrieved. |
|||
Offset E set to offset value for 1. |
|||
F evaluated. |
|||
Offset F retrieved. |
|||
G evaluated. |
|||
Offset G retrieved. |
|||
Offset H set to offset value for G. |
|||
I evaluated. |
|||
Offset I retrieved. |
|||
Offset J set to offset value for I. |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue