Skip to content

Commit

Permalink
Phalcon\Session\Bag: add of initialization for remove()
Browse files Browse the repository at this point in the history
  • Loading branch information
morafabio committed Feb 22, 2017
1 parent 2622296 commit 83d6aa6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- Fixed `Phalcon\Session\Bag::remove` to initialize the bag before removing a value

# [3.1.0](https://github.com/phalcon/cphalcon/releases/tag/v3.1.0) (2016-XX-XX)
- Added `Phalcon\Validation\Validator\Callback`, `Phalcon\Validation::getData`
- Added the ability to truncate database tables
Expand Down
4 changes: 4 additions & 0 deletions phalcon/session/bag.zep
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ class Bag implements InjectionAwareInterface, BagInterface, \IteratorAggregate,
*/
public function remove(string! property) -> boolean
{
if this->_initialized === false {
this->initialize();
}

var data;

let data = this->_data;
Expand Down
41 changes: 41 additions & 0 deletions tests/unit/Session/BagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,45 @@ function () {
}
);
}

/**
* Delete a value in a bag (not initialized internally)
*
* @author Fabio Mora <[email protected]>
* @since 2017-02-21
*/
public function testDeleteInitializeInternalData()
{
$this->specify(
"Delete a value in a non initialized bag has failed",
function () {
$reflectionClass = new \ReflectionClass(\Phalcon\Session\Bag::class);
$_data = $reflectionClass->getProperty('_data');
$_data->setAccessible(true);
$_initialized = $reflectionClass->getProperty('_initialized');
$_initialized->setAccessible(true);

// Setup a bag with a value
$bag = new \Phalcon\Session\Bag('fruit');
$bag->set('apples', 10);
expect($bag->get('apples'))->same(10);
expect($_data->getValue($bag))->same(['apples' => 10]);
expect($_initialized->getValue($bag))->true();

// Emulate a reset of the internal status (e.g. as would be done by a sleep/wakeup handler)
$serializedBag = serialize($bag);
unset($bag);

$bag = unserialize($serializedBag);
$_data->setValue($bag, NULL);
$_initialized->setValue($bag, false);

// Delete
expect($_initialized->getValue($bag))->false();
expect($bag->remove('apples'))->true();
expect($bag->get('apples'))->null();
expect($_initialized->getValue($bag))->true();
}
);
}
}

0 comments on commit 83d6aa6

Please sign in to comment.