PHP ArrayObject:在对象外部修改了数组,内部位置不再有效

时间:2016-01-17 13:14:58

标签: php arrayobject

我试图创建一种闪存消息类,它将" flush"第一次获取它们时的消息。因此,如果例如从一个控制器操作转发到另一个(具有相同的HTTP请求),则不在下一个HTTP请求上

这是我的班级设计:

<?php
namespace MartynBiz;

/**
 * Flash messages. Slight variation in that this will store a message until it
 * is accessed - whether that is a next http request, or same request. Simply
 * when get method is called the message is wiped from session.
 * TODO move this to martynbiz\php-flash
 */
class Flash
{
    /**
     * Message storage
     *
     * @var ArrayObject
     */
    protected $storage;

    /**
     * @param string $storage Name to store messages in session
     */
    public function __construct($storage=null)
    {
        // if storage is not defined, create ArrayObject (not persistent)
        if (is_null($storage)) {
            $storage = new \ArrayObject();
        }

        $this->storage = $storage;
    }

    /**
     * Add flash message
     *
     * @param string $key The key to store the message under
     * @param string $message Message to show on next request
     */
    public function addMessage($key, $message)
    {
        // create entry in the session
        $this->storage[$key] = $message;
    }

    /**
     * Get flash messages, and reset storage
     * @return array Messages to show for current request
     */
    public function flushMessages()
    {
        $messages = $this->storage->getArrayCopy();

        // clear storage items
        foreach ($this->storage as $key => $value) {
            unset($this->storage[$key]);
        }

        return $messages;
    }
}

我已经编写了一些PHPUnit测试,这些测试也演示了如何使用Flash类:

<?php

// TODO test with an ArrayAccess/Object $storage passed in

use MartynBiz\Flash;
use Zend\Session\Container;

class FlashTest extends PHPUnit_Framework_TestCase
{
    protected $flash;

    public function testInstantiation()
    {
        $flash = new Flash();

        $this->assertTrue($flash instanceof Flash);
    }

    public function testGettingSetting()
    {
        $flash = new Flash();

        $flash->addMessage('key1', 'value1');
        $flash->addMessage('key2', 'value2');
        $flash->addMessage('key2', 'value3');

        $expected = array(
            'key1' => 'value1',
            'key2' => 'value3',
        );

        // assert first time to access messages
        $messages = $flash->flushMessages();
        $this->assertEquals($expected, $messages);

        // assert messages have been cleared
        $messages = $flash->flushMessages();
        $this->assertEquals(array(), $messages);
    }

    public function testCustomStorage()
    {
        $container = new Container('mycontainer');
        $flash = new Flash($container);

        $flash->addMessage('key1', 'value1');

        $expected = array(
            'key1' => 'value1',
        );

        // assert first time to access messages
        $messages = $flash->flushMessages();
        $this->assertEquals($expected, $messages);

        // assert messages have been cleared
        $messages = $flash->flushMessages();
        $this->assertEquals(array(), $messages);
    }
}

你可以看到我也传递了一个自定义的$ storage(Zend会话容器实例),这是我喜欢的一个选项。这些测试似乎已经通过,但是我在其他测试中遇到以下错误,我不太明白这个问题是什么:

$ ./vendor/bin/phpunit tests/library/FlashTest.php 1
PHPUnit 4.8.21-4-g7f07877 by Sebastian Bergmann and contributors.

.E.

Time: 68 ms, Memory: 4.50Mb

There was 1 error:

1) FlashTest::testGettingSetting
MartynBiz\Flash::flushMessages(): ArrayIterator::next(): Array was modified outside object and internal position is no longer valid

/var/www/crsrc-slimmvc/app/library/MartynBiz/Flash/Flash.php:53
/var/www/crsrc-slimmvc/tests/library/FlashTest.php:33

FAILURES!
Tests: 3, Assertions: 3, Errors: 1.

我已经四处寻找此错误并尝试了一些替代方法(例如$ this-&gt; storage-&gt; getIterator()),但我仍然遇到同样的错误。我出错的任何想法?我想我对ArrayObject来说有点新鲜。

0 个答案:

没有答案