HEX
Server: nginx/1.24.0
System: Linux localhost 5.15.0-46-generic #49-Ubuntu SMP Thu Aug 4 18:03:25 UTC 2022 x86_64
User: www (1000)
PHP: 8.3.27
Disabled: passthru,exec,system,putenv,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv
Upload Files
File: /www/server/php/83/src/ext/spl/tests/iterator_045.phpt
--TEST--
SPL: CachingIterator and offsetSet/Unset, getCache using flag FULL_CACHE
--FILE--
<?php

class MyFoo
{
    function __toString()
    {
        return 'foo';
    }
}

class MyCachingIterator extends CachingIterator
{
    function __construct(Iterator $it, $flags = 0)
    {
        parent::__construct($it, $flags);
    }

    function testSet($ar)
    {
        echo __METHOD__ . "()\n";
        foreach($ar as $k => $v)
        {
            echo "set($k,$v)\n";
            $this->offsetSet($k, $v);
        }
    }

    function testUnset($ar)
    {
        echo __METHOD__ . "()\n";
        foreach($ar as $k => $v)
        {
            echo "unset($v)\n";
            $this->offsetUnset($v);
        }
    }

    function fill()
    {
        echo __METHOD__ . "()\n";
        foreach($this as $v) ;
    }

    function show()
    {
        echo __METHOD__ . "()\n";
        var_dump($this->getCache());
    }
}

$it = new MyCachingIterator(new ArrayIterator(array(0, 'foo'=>1, 2, 'bar'=>3, 4)));

try
{
    var_dump($it->offsetSet(0, 0));
}
catch(Exception $e)
{
    echo "Exception: " . $e->getMessage() . "\n";
}

try
{
    var_dump($it->offsetUnset(0));
}
catch(Exception $e)
{
    echo "Exception: " . $e->getMessage() . "\n";
}

$it = new MyCachingIterator(new ArrayIterator(array(0, 1, 2, 3)), CachingIterator::FULL_CACHE);

$checks = array(0 => 25, 1 => 42, 3 => 'FooBar');
$unsets = array(0, 2);

$it->testSet($checks);
$it->show();
$it->testUnset($unsets);
$it->show();
$it->fill();
$it->show();
$it->testSet($checks);
$it->show();
$it->testUnset($unsets);
$it->show();

?>
--EXPECT--
Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
Exception: MyCachingIterator does not use a full cache (see CachingIterator::__construct)
MyCachingIterator::testSet()
set(0,25)
set(1,42)
set(3,FooBar)
MyCachingIterator::show()
array(3) {
  [0]=>
  int(25)
  [1]=>
  int(42)
  [3]=>
  string(6) "FooBar"
}
MyCachingIterator::testUnset()
unset(0)
unset(2)
MyCachingIterator::show()
array(2) {
  [1]=>
  int(42)
  [3]=>
  string(6) "FooBar"
}
MyCachingIterator::fill()
MyCachingIterator::show()
array(4) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
  [3]=>
  int(3)
}
MyCachingIterator::testSet()
set(0,25)
set(1,42)
set(3,FooBar)
MyCachingIterator::show()
array(4) {
  [0]=>
  int(25)
  [1]=>
  int(42)
  [2]=>
  int(2)
  [3]=>
  string(6) "FooBar"
}
MyCachingIterator::testUnset()
unset(0)
unset(2)
MyCachingIterator::show()
array(2) {
  [1]=>
  int(42)
  [3]=>
  string(6) "FooBar"
}