Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Provide negative unit-test for Issue #81 #102

Merged
merged 2 commits into from
Jan 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/AbstractContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ abstract class AbstractContainer extends ArrayObject
*/
protected static $defaultManager;

/**
* Default value to return by reference from offsetGet
*/
private $defaultValue = null;

/**
* Constructor
*
Expand Down Expand Up @@ -425,7 +430,7 @@ public function offsetExists($key)
public function &offsetGet($key)
{
if (! $this->offsetExists($key)) {
return;
return $this->defaultValue;
}
$storage = $this->getStorage();
$name = $this->getName();
Expand Down
68 changes: 68 additions & 0 deletions test/AbstractContainerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Session;

use PHPUnit\Framework\TestCase;
use Zend\Session\Container;
use Zend\Session\Config\StandardConfig;
use Zend\Session\ManagerInterface as Manager;
use ZendTest\Session\TestAsset\TestContainer;

/**
* @group Zend_Session
* @covers Zend\Session\AbstractContainer
*/
class AbstractContainerTest extends TestCase
{
/**
* Hack to allow running tests in separate processes
*
* @see http://matthewturland.com/2010/08/19/process-isolation-in-phpunit/
*/
protected $preserveGlobalState = false;

/**
* @var Manager
*/
protected $manager;

/**
* @var Container
*/
protected $container;

public function setUp()
{
$_SESSION = [];
Container::setDefaultManager(null);

$config = new StandardConfig([
'storage' => 'Zend\\Session\\Storage\\ArrayStorage',
]);

$this->manager = $manager = new TestAsset\TestManager($config);
$this->container = new TestContainer('Default', $manager);
}

public function tearDown()
{
$_SESSION = [];
Container::setDefaultManager(null);
}

/**
* This test case fails on zend-session 2.8.0 with the php error below and works fine on 2.7.*.
* "Only variable references should be returned by reference"
*/
public function testOffsetGetMissingKey()
{
self::assertNull($this->container->offsetGet('this key does not exist in the container'));
}
}
17 changes: 17 additions & 0 deletions test/TestAsset/TestContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace ZendTest\Session\TestAsset;

use Zend\Session\AbstractContainer;

class TestContainer extends AbstractContainer
{
// do nothing
}