diff --git a/CHANGELOG-4.0.md b/CHANGELOG-4.0.md index 48825d791fc..43dc16c6f12 100644 --- a/CHANGELOG-4.0.md +++ b/CHANGELOG-4.0.md @@ -27,6 +27,13 @@ The implementation offers PSR-7/PSR-17 compatible components in a different name ## Fixed - Fixed Assets Manager hard reference to \Phalcon\Tag, should use DI [#12261](https://github.com/phalcon/cphalcon/issues/12261) +## Changed +- Refactored `Phalcon\Registry` to use the `Phalcon\Collection` class [#13893](https://github.com/phalcon/cphalcon/issues/13893) +- Refactored `Phalcon\Session\Bag` to use the `Phalcon\Collection` class [#13893](https://github.com/phalcon/cphalcon/issues/13893) + +## Removed +- Removed `Phalcon\Session\BagInterface` [#13893](https://github.com/phalcon/cphalcon/issues/13893) + # [4.0.0-alpha.3](https://github.com/phalcon/cphalcon/releases/tag/v4.0.0-alpha.3) (2019-02-31) ## Added diff --git a/ext/phalcon/session/baginterface.zep.c b/ext/phalcon/session/baginterface.zep.c deleted file mode 100644 index d476a149e78..00000000000 --- a/ext/phalcon/session/baginterface.zep.c +++ /dev/null @@ -1,76 +0,0 @@ - -#ifdef HAVE_CONFIG_H -#include "../../ext_config.h" -#endif - -#include -#include "../../php_ext.h" -#include "../../ext.h" - -#include - -#include "kernel/main.h" - - -/** - * This file is part of the Phalcon. - * - * (c) Phalcon Team - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -/** - * Phalcon\Session\BagInterface - * - * Interface for Phalcon\Session\Bag - */ -ZEPHIR_INIT_CLASS(Phalcon_Session_BagInterface) { - - ZEPHIR_REGISTER_INTERFACE(Phalcon\\Session, BagInterface, phalcon, session_baginterface, phalcon_session_baginterface_method_entry); - - return SUCCESS; - -} - -/** - * Getter of values - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, __get); - -/** - * Isset property - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, __isset); - -/** - * Setter of values - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, __set); - -/** - * Destroys the session bag - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, destroy); - -/** - * Getter of values - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, get); - -/** - * Isset property - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, has); - -/** - * Initializes the session bag. This method must not be called directly, the - * class calls it when its internal data is accessed - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, initialize); - -/** - * Setter of values - */ -ZEPHIR_DOC_METHOD(Phalcon_Session_BagInterface, set); - diff --git a/ext/phalcon/session/baginterface.zep.h b/ext/phalcon/session/baginterface.zep.h deleted file mode 100644 index bef18b3fd70..00000000000 --- a/ext/phalcon/session/baginterface.zep.h +++ /dev/null @@ -1,75 +0,0 @@ - -extern zend_class_entry *phalcon_session_baginterface_ce; - -ZEPHIR_INIT_CLASS(Phalcon_Session_BagInterface); - -ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_session_baginterface___get, 0, 0, 1) -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif -ZEND_END_ARG_INFO() - -#if PHP_VERSION_ID >= 70200 -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phalcon_session_baginterface___isset, 0, 1, _IS_BOOL, 0) -#else -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phalcon_session_baginterface___isset, 0, 1, _IS_BOOL, NULL, 0) -#endif -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_session_baginterface___set, 0, 0, 2) -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif - ZEND_ARG_INFO(0, value) -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_session_baginterface_get, 0, 0, 1) -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif - ZEND_ARG_INFO(0, defaultValue) -ZEND_END_ARG_INFO() - -#if PHP_VERSION_ID >= 70200 -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phalcon_session_baginterface_has, 0, 1, _IS_BOOL, 0) -#else -ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_phalcon_session_baginterface_has, 0, 1, _IS_BOOL, NULL, 0) -#endif -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif -ZEND_END_ARG_INFO() - -ZEND_BEGIN_ARG_INFO_EX(arginfo_phalcon_session_baginterface_set, 0, 0, 2) -#if PHP_VERSION_ID >= 70200 - ZEND_ARG_TYPE_INFO(0, property, IS_STRING, 0) -#else - ZEND_ARG_INFO(0, property) -#endif - ZEND_ARG_INFO(0, value) -ZEND_END_ARG_INFO() - -ZEPHIR_INIT_FUNCS(phalcon_session_baginterface_method_entry) { - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, __get, arginfo_phalcon_session_baginterface___get) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, __isset, arginfo_phalcon_session_baginterface___isset) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, __set, arginfo_phalcon_session_baginterface___set) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, destroy, NULL) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, get, arginfo_phalcon_session_baginterface_get) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, has, arginfo_phalcon_session_baginterface_has) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, initialize, NULL) - PHP_ABSTRACT_ME(Phalcon_Session_BagInterface, set, arginfo_phalcon_session_baginterface_set) - PHP_FE_END -}; diff --git a/phalcon/collection.zep b/phalcon/collection.zep index 8595329910e..d77e466d2f9 100644 --- a/phalcon/collection.zep +++ b/phalcon/collection.zep @@ -24,13 +24,18 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS /** * @var array */ - private data = []; + protected data = []; - public function __construct(array! data = null) -> void + /** + * @var array + */ + protected lowerKeys = []; + + + + public function __construct(array! data = []) -> void { - if typeof data === "array" { - this->init(data); - } + this->init(data); } /** @@ -86,12 +91,16 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS /** * Get the element from the collection */ - public function get(string! element, var defaultValue = null) -> var | bool + public function get(string! element, var defaultValue = null, bool insensitive = true) -> var | bool { var value; - if likely fetch value, this->data[element] { - return value; + if likely insensitive { + let element = strtolower(element); + } + + if likely fetch value, this->lowerKeys[element] { + return this->data[value]; } return defaultValue; @@ -108,44 +117,25 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS /** * Get the element from the collection */ - public function has(string! element) -> bool + public function has(string! element, bool insensitive = true) -> bool { - return isset this->data[element]; - } - - /** - * Delete the element from the collection - */ - public function remove(string! element) -> void - { - array data; - - let data = this->data; - - unset data[element]; - - this->init(data); - } - - /** - * Set an element in the collection - */ - public function set(string! element, var value) -> void - { - array data; - - let data = this->data, - data[element] = value; + if likely insensitive { + let element = strtolower(element); + } - this->init(data); + return isset this->lowerKeys[element]; } /** * Initialize internal array */ - public function init(array! data = []) + public function init(array! data = []) -> void { - let this->data = data; + var key, value; + + for key, value in data { + this->set(key, value); + } } /** @@ -206,6 +196,27 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS this->remove(element); } + /** + * Delete the element from the collection + */ + public function remove(string! element) -> void + { + var data, lowerKeys, key, value; + + let data = this->data, + lowerKeys = this->lowerKeys, + key = strtolower(element); + + if this->has(element) { + let value = lowerKeys[key]; + unset lowerKeys[key]; + unset data[value]; + } + + let this->data = data, + this->lowerKeys = lowerKeys; + } + /** * String representation of object * @@ -213,12 +224,21 @@ class Collection implements \ArrayAccess, \Countable, \IteratorAggregate, \JsonS */ public function serialize() -> string { - array data; + return serialize(this->data); + } - let data = this->data; + /** + * Set an element in the collection + */ + public function set(string! element, var value) -> void + { + var key; - return serialize(data); - } + let key = strtolower(element); + + let this->data[element] = value, + this->lowerKeys[key] = element; + } /** * Returns the object in an array format diff --git a/phalcon/registry.zep b/phalcon/registry.zep index bd04671b8f0..945ce8eacfd 100644 --- a/phalcon/registry.zep +++ b/phalcon/registry.zep @@ -8,7 +8,9 @@ * file that was distributed with this source code. */ - namespace Phalcon; +namespace Phalcon; + +use Phalcon\Collection; /** * Phalcon\Registry @@ -60,121 +62,203 @@ * are implemented using object handlers or similar techniques: this allows * to bypass relatively slow method calls. */ -final class Registry implements \ArrayAccess, \Countable, \Iterator +final class Registry extends Collection { - protected _data; + /** + * Constructor + */ + public final function __construct(array! data = null) -> void + { + parent::__construct(data); + } /** - * Registry constructor + * Magic getter to get an element from the collection */ - public final function __construct() + public final function __get(string! element) -> var { - let this->_data = []; + return parent::get(element); } /** - * Checks if the element is present in the registry + * Magic isset to check whether an element exists or not */ - public final function offsetExists(var offset) -> bool + public final function __isset(string! element) -> bool { - return isset this->_data[offset]; + return parent::has(element); } /** - * Returns an index in the registry + * Magic setter to assign values to an element */ - public final function offsetGet(var offset) -> var + public final function __set(string! element, var value) -> void { - return this->_data[offset]; + parent::set(element, value); } /** - * Sets an element in the registry + * Magic unset to remove an element from the collection */ - public final function offsetSet(var offset, var value) -> void + public final function __unset(string! element) -> void { - let this->_data[offset] = value; + parent::remove(element); } /** - * Unsets an element in the registry + * Clears the internal collection */ - public final function offsetUnset(var offset) -> void + public final function clear() -> void { - unset this->_data[offset]; + parent::clear(); } /** - * Checks how many elements are in the register + * Count elements of an object + * + * @link https://php.net/manual/en/countable.count.php */ public final function count() -> int { - return count(this->_data); + return parent::count(); + } + + /** + * Get the element from the collection + */ + public final function get(string! element, var defaultValue = null, bool insensitive = true) -> var | bool + { + return parent::get(element, defaultValue, insensitive); } /** - * Moves cursor to next row in the registry + * Returns the iterator of the class */ - public final function next() -> void + public final function getIterator() -> { - next(this->_data); + return parent::getIterator(); } /** - * Gets pointer number of active row in the registry + * Get the element from the collection */ - public final function key() -> int + public final function has(string! element, bool insensitive = true) -> bool { - return key(this->_data); + return parent::has(element, insensitive); } /** - * Rewinds the registry cursor to its beginning + * Initialize internal array */ - public final function rewind() -> void + public final function init(array! data = []) -> void { - reset(this->_data); + parent::init(data); } /** - * Checks if the iterator is valid + * Specify data which should be serialized to JSON + * + * @link https://php.net/manual/en/jsonserializable.jsonserialize.php */ - public function valid() -> bool + public final function jsonSerialize () -> array { - return key(this->_data) !== null; + return parent::jsonSerialize(); } /** - * Obtains the current value in the internal iterator + * Whether a offset exists + * + * @link https://php.net/manual/en/arrayaccess.offsetexists.php */ - public function current() + public final function offsetExists(var element) -> bool { - return current(this->_data); + return parent::has(element); } /** - * Sets an element in the registry + * Offset to retrieve + * + * @link https://php.net/manual/en/arrayaccess.offsetget.php */ - public final function __set(string! key, var value) -> void + public final function offsetGet(var element) -> var { - this->offsetSet(key, value); + return parent::get(element); } /** - * Returns an index in the registry + * Offset to set + * + * @link https://php.net/manual/en/arrayaccess.offsetset.php */ - public final function __get(string! key) -> var + public final function offsetSet(var element, var value) -> void { - return this->offsetGet(key); + parent::set(element, value); } - public final function __isset(string! key) -> bool + /** + * Offset to unset + * + * @link https://php.net/manual/en/arrayaccess.offsetunset.php + */ + public final function offsetUnset(var element) -> void { - return this->offsetExists(key); + parent::remove(element); } - public final function __unset(string! key) -> void + /** + * Delete the element from the collection + */ + public final function remove(string! element) -> void { - this->offsetUnset(key); + parent::remove(element); } + + /** + * String representation of object + * + * @link https://php.net/manual/en/serializable.serialize.php + */ + public final function serialize() -> string + { + return parent::serialize(); + } + + /** + * Set an element in the collection + */ + public final function set(string! element, var value) -> void + { + parent::set(element, value); + } + + /** + * Returns the object in an array format + */ + public final function toArray() -> array + { + return parent::toArray(); + } + + /** + * Returns the object in a JSON format + * + * The default string uses the following options for json_encode + * + * JSON_HEX_TAG, JSON_HEX_APOS, JSON_HEX_AMP, JSON_HEX_QUOT, JSON_UNESCAPED_SLASHES + * + * @see https://www.ietf.org/rfc/rfc4627.txt + */ + public final function toJson(int options = 74) -> string + { + return parent::toJson(options); + } + + /** + * Constructs the object + * + * @link https://php.net/manual/en/serializable.unserialize.php + */ + public final function unserialize(var serialized) -> void + { + parent::unserialize(serialized); + } } diff --git a/phalcon/session/bag.zep b/phalcon/session/bag.zep index 77307b323e1..8121464bad6 100644 --- a/phalcon/session/bag.zep +++ b/phalcon/session/bag.zep @@ -9,6 +9,7 @@ namespace Phalcon\Session; +use Phalcon\Collection; use Phalcon\Di; use Phalcon\DiInterface; use Phalcon\Di\InjectionAwareInterface; @@ -26,134 +27,52 @@ use Phalcon\Di\InjectionAwareInterface; * $user->age = 22; * */ -class Bag implements InjectionAwareInterface, BagInterface, \IteratorAggregate, \ArrayAccess, \Countable +class Bag extends Collection implements InjectionAwareInterface { + private container; - protected _dependencyInjector; + private name = null; - protected _data; - - protected _initialized = false; - - protected _name = null; - - protected _session; + private session; /** * Phalcon\Session\Bag constructor */ public function __construct(string! name) { - let this->_name = name; - } - - /** - * Magic getter to obtain values from the session bag - * - * - * echo $user->name; - * - */ - public function __get(string! property) -> var - { - return this->get(property); - } - - /** - * Magic isset to check whether a property is defined in the bag - * - * - * var_dump( - * isset($user["name"]) - * ); - * - */ - public function __isset(string! property) -> bool - { - return this->has(property); - } - - /** - * Magic setter to assign values to the session bag - * - * - * $user->name = "Kimbra"; - * - */ - public function __set(string! property, var value) - { - this->set(property, value); - } - - /** - * Magic unset to remove items using the array syntax - * - * - * unset($user["name"]); - * - */ - public function __unset(string! property) -> bool - { - return this->remove(property); - } + var container, data, session; + + let this->name = name, + session = this->session; + + if typeof session != "object" { + let container = this->container; + if typeof container != "object" { + let container = Di::getDefault(); + if typeof container != "object" { + throw new Exception("A dependency injection object is required to access the 'session' service"); + } + } - /** - * Return length of bag - * - * - * echo $user->count(); - * - */ - public final function count() -> int - { - if this->_initialized === false { - this->initialize(); + let session = container->getShared("session"), + this->session = session; } - return count(this->_data); - } - /** - * Destroys the session bag - * - * - * $user->destroy(); - * - */ - public function destroy() - { - if this->_initialized === false { - this->initialize(); + let data = session->get(this->name); + if typeof data != "array" { + let data = []; } - let this->_data = []; - this->_session->remove(this->_name); + + parent::__construct(data); } /** - * Obtains a value from the session bag optionally setting a default value - * - * - * echo $user->get("name", "Kimbra"); - * + * Destroys the session bag */ - public function get(string! property, var defaultValue = null) + public function clear() -> void { - var value; - - /** - * Check first if the bag is initialized - */ - if this->_initialized === false { - this->initialize(); - } - - /** - * Retrieve the data - */ - if fetch value, this->_data[property] { - return value; - } - - return defaultValue; + parent::clear(); + this->session->remove(this->name); } /** @@ -161,139 +80,40 @@ class Bag implements InjectionAwareInterface, BagInterface, \IteratorAggregate, */ public function getDI() -> { - return this->_dependencyInjector; - } - - /** - * Returns the bag iterator - */ - public final function getIterator() -> <\ArrayIterator> - { - if this->_initialized === false { - this->initialize(); - } - - return new \ArrayIterator(this->_data); + return this->container; } /** - * Check whether a property is defined in the internal bag - * - * - * var_dump( - * $user->has("name") - * ); - * - */ - public function has(string! property) -> bool - { - if this->_initialized === false { - this->initialize(); - } - - return isset this->_data[property]; - } - - /** - * Initializes the session bag. This method must not be called directly, the - * class calls it when its internal data is accessed + * Removes a property from the internal bag */ - public function initialize() - { - var session, dependencyInjector, data; - - let session = this->_session; - if typeof session != "object" { - - let dependencyInjector = this->_dependencyInjector; - if typeof dependencyInjector != "object" { - let dependencyInjector = Di::getDefault(); - if typeof dependencyInjector != "object" { - throw new Exception("A dependency injection object is required to access the 'session' service"); - } - } - - let session = dependencyInjector->getShared("session"), - this->_session = session; - } - - let data = session->get(this->_name); - if typeof data != "array" { - let data = []; - } - - let this->_data = data; - let this->_initialized = true; - } - - public final function offsetExists(var property) -> bool - { - return this->has(property); - } - - public final function offsetGet(var property) -> var + public function init(array! data = []) -> void { - return this->get(property); - } - - public final function offsetSet(var property, var value) - { - return this->set(property, value); - } - - public final function offsetUnset(var property) - { - return this->remove(property); + parent::init(data); } /** * Removes a property from the internal bag - * - * - * $user->remove("name"); - * */ - public function remove(string! property) -> bool + public function remove(string! element) -> void { - if this->_initialized === false { - this->initialize(); - } - - var data; - - let data = this->_data; - if isset data[property] { - unset data[property]; - this->_session->set(this->_name, data); - let this->_data = data; - return true; - } - - return false; + parent::remove(element); + this->session->set(this->name, this->data); } /** * Sets a value in the session bag - * - * - * $user->set("name", "Kimbra"); - * */ - public function set(string! property, var value) + public function set(string! element, var value) { - if this->_initialized === false { - this->initialize(); - } - - let this->_data[property] = value; - this->_session->set(this->_name, this->_data); + parent::set(element, value); + this->session->set(this->name, this->data); } /** * Sets the DependencyInjector container */ - public function setDI( dependencyInjector) + public function setDI( container) { - let this->_dependencyInjector = dependencyInjector; + let this->container = container; } } diff --git a/phalcon/session/baginterface.zep b/phalcon/session/baginterface.zep deleted file mode 100644 index 58d7c5f656a..00000000000 --- a/phalcon/session/baginterface.zep +++ /dev/null @@ -1,59 +0,0 @@ -/** - * This file is part of the Phalcon. - * - * (c) Phalcon Team - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -namespace Phalcon\Session; - -/** - * Phalcon\Session\BagInterface - * - * Interface for Phalcon\Session\Bag - */ -interface BagInterface -{ - /** - * Getter of values - */ - public function __get(string! property) -> var; - - /** - * Isset property - */ - public function __isset(string! property) -> bool; - - /** - * Setter of values - */ - public function __set(string! property, var value); - - /** - * Destroys the session bag - */ - public function destroy(); - - /** - * Getter of values - */ - public function get(string! property, defaultValue = null) -> var; - - /** - * Isset property - */ - public function has(string! property) -> bool; - - /** - * Initializes the session bag. This method must not be called directly, the - * class calls it when its internal data is accessed - */ - public function initialize(); - - /** - * Setter of values - */ - public function set(string! property, var value); -} diff --git a/tests/integration/Session/Bag/GetSetCest.php b/tests/integration/Session/Bag/ClearCest.php similarity index 50% rename from tests/integration/Session/Bag/GetSetCest.php rename to tests/integration/Session/Bag/ClearCest.php index 9058cc5253e..38c70a2b41c 100644 --- a/tests/integration/Session/Bag/GetSetCest.php +++ b/tests/integration/Session/Bag/ClearCest.php @@ -12,35 +12,45 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class GetSetCest + * Class ClearCest */ -class GetSetCest +class ClearCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: __get/__set() + * Tests Phalcon\Session\Bag :: clear() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagGetSet(IntegrationTester $I) + public function sessionBagClear(IntegrationTester $I) { - $I->wantToTest("Session\Bag - __get/__set()"); - $session = new Bag("SetterTest"); + $I->wantToTest('Session\Bag - clear()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); - $testValue = "TestValue"; - $session->test = $testValue; + $expected = $data; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); - $I->assertEquals($testValue, $session->get("test")); + $collection->clear(); + $expected = 0; + $actual = $collection->count(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/ConstructCest.php b/tests/integration/Session/Bag/ConstructCest.php index b121f8fdb95..1d55ab71efb 100644 --- a/tests/integration/Session/Bag/ConstructCest.php +++ b/tests/integration/Session/Bag/ConstructCest.php @@ -12,14 +12,19 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\DiTrait; +use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** * Class ConstructCest */ class ConstructCest { + use DiTrait; + use SessionBagTrait; + /** * Tests Phalcon\Session\Bag :: __construct() * @@ -30,8 +35,10 @@ class ConstructCest */ public function sessionBagConstruct(IntegrationTester $I) { - $I->wantToTest("Session\Bag - __construct()"); - $session = new Bag("test"); - $I->assertInstanceOf("\Phalcon\Session\Bag", $session); + $I->wantToTest('Session\Bag - __construct()'); + $collection = new Bag('BagTest'); + + $class = Bag::class; + $I->assertInstanceOf($class, $collection); } } diff --git a/tests/integration/Session/Bag/CountCest.php b/tests/integration/Session/Bag/CountCest.php index 4fdbf9123c5..fc53055a456 100644 --- a/tests/integration/Session/Bag/CountCest.php +++ b/tests/integration/Session/Bag/CountCest.php @@ -12,8 +12,8 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; @@ -35,13 +35,18 @@ class CountCest */ public function sessionBagCount(IntegrationTester $I) { - $I->wantToTest("Session\Bag - count()"); - $session = new Bag("CountTest"); - $session->test1 = "test"; - $session->test2 = "test"; - $session->test3 = "test"; + $I->wantToTest('Session\Bag - count()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); + + $I->assertCount(3, $collection->toArray()); - $expected = 3; - $I->assertEquals($expected, $session->count()); + $actual = $collection->count(); + $I->assertEquals(3, $actual); } } diff --git a/tests/integration/Session/Bag/GetCest.php b/tests/integration/Session/Bag/GetCest.php new file mode 100644 index 00000000000..a61f3921c8c --- /dev/null +++ b/tests/integration/Session/Bag/GetCest.php @@ -0,0 +1,63 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Session\Bag; + +use Phalcon\Session\Bag; +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\DiTrait; +use Phalcon\Test\Fixtures\Traits\SessionBagTrait; + +/** + * Class GetCest + */ +class GetCest +{ + use DiTrait; + use SessionBagTrait; + + /** + * Tests Phalcon\Session\Bag :: get() + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function sessionBagGet(IntegrationTester $I) + { + $I->wantToTest('Session\Bag - get()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); + + $expected = 'four'; + $actual = $collection->get('three'); + $I->assertEquals($expected, $actual); + + $actual = $collection->get('THREE'); + $I->assertEquals($expected, $actual); + + $actual = $collection['three']; + $I->assertEquals($expected, $actual); + + $actual = $collection->three; + $I->assertEquals($expected, $actual); + + $actual = $collection->offsetGet('three'); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/integration/Session/Bag/GetIteratorCest.php b/tests/integration/Session/Bag/GetIteratorCest.php index 2c80587c463..e506f5b3b8a 100644 --- a/tests/integration/Session/Bag/GetIteratorCest.php +++ b/tests/integration/Session/Bag/GetIteratorCest.php @@ -12,8 +12,8 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; @@ -35,12 +35,17 @@ class GetIteratorCest */ public function sessionBagGetIterator(IntegrationTester $I) { - $I->wantToTest("Session\Bag - getIterator()"); - $session = new Bag("CountTest"); - $session->test1 = "test"; - $session->test2 = "test"; - $session->test3 = "test"; + $I->wantToTest('Session\Bag - getIterator()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); - $I->assertInstanceOf("\ArrayIterator", $session->getIterator()); + foreach ($collection as $key => $value) { + $I->assertEquals($data[$key], $collection[$key]); + } } } diff --git a/tests/integration/Session/Bag/HasCest.php b/tests/integration/Session/Bag/HasCest.php index bfbf6cb537c..cbd993d3858 100644 --- a/tests/integration/Session/Bag/HasCest.php +++ b/tests/integration/Session/Bag/HasCest.php @@ -12,8 +12,8 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; @@ -35,13 +35,38 @@ class HasCest */ public function sessionBagHas(IntegrationTester $I) { - $I->wantToTest("Session\Bag - has()"); - $session = new Bag("SetTest"); + $I->wantToTest('Session\Bag - has()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + + $collection = new Bag('BagTest'); + $collection->init($data); + + $actual = $collection->has('three'); + $I->assertTrue($actual); + + $actual = $collection->has('THREE'); + $I->assertTrue($actual); + + $actual = $collection->has('THREE', false); + $I->assertFalse($actual); + + $actual = $collection->has('unknown'); + $I->assertFalse($actual); + + $actual = isset($collection['three']); + $I->assertTrue($actual); + + $actual = isset($collection['unknown']); + $I->assertFalse($actual); - $testValue = "TestValue"; - $session->set("test", $testValue); + $actual = $collection->offsetExists('three'); + $I->assertTrue($actual); - $I->assertTrue($session->has("test")); - $I->assertFalse($session->has("unknown")); + $actual = $collection->offsetExists('unknown'); + $I->assertFalse($actual); } } diff --git a/tests/integration/Session/Bag/OffsetGetSetCest.php b/tests/integration/Session/Bag/InitCest.php similarity index 52% rename from tests/integration/Session/Bag/OffsetGetSetCest.php rename to tests/integration/Session/Bag/InitCest.php index bfbc9ab8c5e..a44efba80fd 100644 --- a/tests/integration/Session/Bag/OffsetGetSetCest.php +++ b/tests/integration/Session/Bag/InitCest.php @@ -12,35 +12,45 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class OffsetGetSetCest + * Class InitCest */ -class OffsetGetSetCest +class InitCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: offsetGet()/offsetSet() + * Tests Phalcon\Session\Bag :: init() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagOffsetGet(IntegrationTester $I) + public function sessionBagInit(IntegrationTester $I) { - $I->wantToTest("Session\Bag - offsetGet()/offsetSet()"); - $session = new Bag("SetTest"); + $I->wantToTest('Session\Bag - init()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + + $collection = new Bag('BagTest'); - $testValue = "TestValue"; - $session->offsetSet("test", $testValue); + $expected = 0; + $actual = $collection->count(); + $I->assertEquals($expected, $actual); - $I->assertEquals($testValue, $session->offsetGet("test")); + $collection->init($data); + $expected = $data; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/InitializeCest.php b/tests/integration/Session/Bag/InitializeCest.php deleted file mode 100644 index 25f299538d4..00000000000 --- a/tests/integration/Session/Bag/InitializeCest.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Integration\Session\Bag; - -use ArrayAccess; -use Countable; -use IntegrationTester; -use IteratorAggregate; -use Phalcon\Di\InjectionAwareInterface; -use Phalcon\Session\Bag; -use Phalcon\Session\BagInterface; -use Phalcon\Test\Fixtures\Traits\DiTrait; -use Phalcon\Test\Fixtures\Traits\SessionBagTrait; - -/** - * Class InitializeCest - */ -class InitializeCest -{ - use DiTrait; - use SessionBagTrait; - - /** - * Tests Phalcon\Session\Bag :: initialize() - * - * @param IntegrationTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function sessionBagInitialize(IntegrationTester $I) - { - $I->wantToTest("Session\Bag - initialize()"); - $session = new Bag("SetTest"); - - $I->assertInstanceOf(InjectionAwareInterface::class, $session); - $I->assertInstanceOf(BagInterface::class, $session); - $I->assertInstanceOf(IteratorAggregate::class, $session); - $I->assertInstanceOf(ArrayAccess::class, $session); - $I->assertInstanceOf(Countable::class, $session); - } -} diff --git a/tests/integration/Session/Bag/OffsetExistsCest.php b/tests/integration/Session/Bag/JsonSerializeCest.php similarity index 54% rename from tests/integration/Session/Bag/OffsetExistsCest.php rename to tests/integration/Session/Bag/JsonSerializeCest.php index 41b9d2213f6..7be49c7b9fc 100644 --- a/tests/integration/Session/Bag/OffsetExistsCest.php +++ b/tests/integration/Session/Bag/JsonSerializeCest.php @@ -12,36 +12,40 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class OffsetExistsCest + * Class JsonSerializeCest */ -class OffsetExistsCest +class JsonSerializeCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: offsetExists() + * Tests Phalcon\Session\Bag :: jsonSerialize() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagOffsetExists(IntegrationTester $I) + public function sessionBagJsonSerialize(IntegrationTester $I) { - $I->wantToTest("Session\Bag - offsetExists()"); - $session = new Bag("SetTest"); - - $testValue = "TestValue"; - $session->set("test", $testValue); + $I->wantToTest('Session\Bag - jsonSerialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); - $I->assertTrue($session->offsetExists("test")); - $I->assertFalse($session->offsetExists("unknown")); + $expected = $data; + $actual = $collection->jsonSerialize(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/RemoveCest.php b/tests/integration/Session/Bag/RemoveCest.php index 75a905c84d7..6a523100968 100644 --- a/tests/integration/Session/Bag/RemoveCest.php +++ b/tests/integration/Session/Bag/RemoveCest.php @@ -12,8 +12,8 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; @@ -35,13 +35,43 @@ class RemoveCest */ public function sessionBagRemove(IntegrationTester $I) { - $I->wantToTest("Session\Bag - remove()"); - $session = new Bag("RemoveTest"); + $I->wantToTest('Session\Bag - remove()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); + + $expected = $data; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); + + $collection->remove('five'); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); - $testValue = "TestValue"; - $session->set("test", $testValue); - $session->remove("test"); + $collection->init($data); + unset($collection['five']); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); - $I->assertFalse($session->has("test")); + $collection->init($data); + $collection->offsetUnset('five'); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/UnderscoreUnsetCest.php b/tests/integration/Session/Bag/SerializeCest.php similarity index 56% rename from tests/integration/Session/Bag/UnderscoreUnsetCest.php rename to tests/integration/Session/Bag/SerializeCest.php index a315d007964..afcbd96eb36 100644 --- a/tests/integration/Session/Bag/UnderscoreUnsetCest.php +++ b/tests/integration/Session/Bag/SerializeCest.php @@ -12,37 +12,40 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class UnderscoreUnsetCest + * Class SerializeCest */ -class UnderscoreUnsetCest +class SerializeCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: __unset() + * Tests Phalcon\Session\Bag :: serialize() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagUnderscoreUnset(IntegrationTester $I) + public function sessionBagSerialize(IntegrationTester $I) { - $I->wantToTest("Session\Bag - __unset()"); - $session = new Bag("UnSetterTest"); - - $testValue = "TestValue"; - $session->set('test', $testValue); - unset($session->test); - - - $I->assertEquals(null, $session->get("test")); + $I->wantToTest('Session\Bag - serialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); + + $expected = serialize($data); + $actual = $collection->serialize(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/SetCest.php b/tests/integration/Session/Bag/SetCest.php new file mode 100644 index 00000000000..ac7e69bf0f9 --- /dev/null +++ b/tests/integration/Session/Bag/SetCest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Session\Bag; + +use Phalcon\Session\Bag; +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\DiTrait; +use Phalcon\Test\Fixtures\Traits\SessionBagTrait; + +/** + * Class SetCest + */ +class SetCest +{ + use DiTrait; + use SessionBagTrait; + + /** + * Tests Phalcon\Session\Bag :: set() + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function sessionBagSet(IntegrationTester $I) + { + $I->wantToTest('Session\Bag - set()'); + $collection = new Bag('BagTest'); + + $collection->set('three', 'two'); + $expected = 'two'; + $actual = $collection->get('three'); + $I->assertEquals($expected, $actual); + + $collection->three = 'Phalcon'; + $expected = 'Phalcon'; + $actual = $collection->get('three'); + $I->assertEquals($expected, $actual); + + $collection->offsetSet('three', 123); + $expected = 123; + $actual = $collection->get('three'); + $I->assertEquals($expected, $actual); + + $collection['three'] = true; + $actual = $collection->get('three'); + $I->assertTrue($actual); + } +} diff --git a/tests/integration/Session/Bag/OffsetUnsetCest.php b/tests/integration/Session/Bag/ToArrayCest.php similarity index 57% rename from tests/integration/Session/Bag/OffsetUnsetCest.php rename to tests/integration/Session/Bag/ToArrayCest.php index 13a28f1f0d0..8409e8f2414 100644 --- a/tests/integration/Session/Bag/OffsetUnsetCest.php +++ b/tests/integration/Session/Bag/ToArrayCest.php @@ -12,36 +12,40 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class OffsetUnsetCest + * Class ToArrayCest */ -class OffsetUnsetCest +class ToArrayCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: offsetUnset() + * Tests Phalcon\Session\Bag :: toArray() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagOffsetUnset(IntegrationTester $I) + public function sessionBagToArray(IntegrationTester $I) { - $I->wantToTest("Session\Bag - offsetUnset()"); - $session = new Bag("UnSetterTest"); - - $testValue = "TestValue"; - $session->set('test', $testValue); - $session->offsetUnset('test'); + $I->wantToTest('Session\Bag - toArray()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); - $I->assertEquals(null, $session->get("test")); + $expected = $data; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/integration/Session/Bag/ToJsonCest.php b/tests/integration/Session/Bag/ToJsonCest.php new file mode 100644 index 00000000000..aaafb596560 --- /dev/null +++ b/tests/integration/Session/Bag/ToJsonCest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Integration\Session\Bag; + +use Phalcon\Session\Bag; +use IntegrationTester; +use Phalcon\Test\Fixtures\Traits\DiTrait; +use Phalcon\Test\Fixtures\Traits\SessionBagTrait; + +/** + * Class ToJsonCest + */ +class ToJsonCest +{ + use DiTrait; + use SessionBagTrait; + + /** + * Tests Phalcon\Session\Bag :: toJson() + * + * @param IntegrationTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function sessionBagToJson(IntegrationTester $I) + { + $I->wantToTest('Session\Bag - toJson()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $collection = new Bag('BagTest'); + $collection->init($data); + + $expected = json_encode($data); + $actual = $collection->toJson(); + $I->assertEquals($expected, $actual); + + $expected = json_encode($data, JSON_PRETTY_PRINT); + $actual = $collection->toJson(JSON_PRETTY_PRINT); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/integration/Session/Bag/UnderscoreGetSetCest.php b/tests/integration/Session/Bag/UnderscoreGetSetCest.php deleted file mode 100644 index 2496238e3da..00000000000 --- a/tests/integration/Session/Bag/UnderscoreGetSetCest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Integration\Session\Bag; - -use IntegrationTester; -use Phalcon\Session\Bag; -use Phalcon\Test\Fixtures\Traits\DiTrait; -use Phalcon\Test\Fixtures\Traits\SessionBagTrait; - -/** - * Class UnderscoreGetSetCest - */ -class UnderscoreGetSetCest -{ - use DiTrait; - use SessionBagTrait; - - /** - * Tests Phalcon\Session\Bag :: __get()/__set() - * - * @param IntegrationTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function sessionBagUnderscoreGetSet(IntegrationTester $I) - { - $I->wantToTest("Session\Bag - __get()/__set()"); - $session = new Bag("SetterTest"); - - $testValue = "TestValue"; - $session->test = $testValue; - - $I->assertEquals($testValue, $session->test); - } -} diff --git a/tests/integration/Session/Bag/UnderscoreIssetCest.php b/tests/integration/Session/Bag/UnderscoreIssetCest.php deleted file mode 100644 index 320834dd90f..00000000000 --- a/tests/integration/Session/Bag/UnderscoreIssetCest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Integration\Session\Bag; - -use IntegrationTester; -use Phalcon\Session\Bag; -use Phalcon\Test\Fixtures\Traits\DiTrait; -use Phalcon\Test\Fixtures\Traits\SessionBagTrait; - -/** - * Class UnderscoreIssetCest - */ -class UnderscoreIssetCest -{ - use DiTrait; - use SessionBagTrait; - - /** - * Tests Phalcon\Session\Bag :: __isset() - * - * @param IntegrationTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function sessionBagUnderscoreIsset(IntegrationTester $I) - { - $I->wantToTest("Session\Bag - __isset()"); - $session = new Bag("SetterTest"); - - $testValue = "TestValue"; - $session->set("test", $testValue); - - $I->assertTrue(isset($session->test)); - $I->assertFalse(isset($session->unknown)); - } -} diff --git a/tests/integration/Session/Bag/DestroyCest.php b/tests/integration/Session/Bag/UnserializeCest.php similarity index 53% rename from tests/integration/Session/Bag/DestroyCest.php rename to tests/integration/Session/Bag/UnserializeCest.php index 596ecae9e03..797c43bf332 100644 --- a/tests/integration/Session/Bag/DestroyCest.php +++ b/tests/integration/Session/Bag/UnserializeCest.php @@ -12,36 +12,41 @@ namespace Phalcon\Test\Integration\Session\Bag; -use IntegrationTester; use Phalcon\Session\Bag; +use IntegrationTester; use Phalcon\Test\Fixtures\Traits\DiTrait; use Phalcon\Test\Fixtures\Traits\SessionBagTrait; /** - * Class DestroyCest + * Class SerializeCest */ -class DestroyCest +class UnserializeCest { use DiTrait; use SessionBagTrait; /** - * Tests Phalcon\Session\Bag :: destroy() + * Tests Phalcon\Session\Bag :: serialize() * * @param IntegrationTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function sessionBagDestroy(IntegrationTester $I) + public function sessionBagSerialize(IntegrationTester $I) { - $I->wantToTest("Session\Bag - destroy()"); - $session = new Bag("DestroyTest"); - // test using magic setter - $session->test = "test"; - $session->destroy(); + $I->wantToTest('Session\Bag - serialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $serialized = serialize($data); + $collection = new Bag('BagTest'); - $session = new Bag("DestroyTest"); - $I->assertEquals(null, $session->test); + $collection->unserialize($serialized); + $expected = $data; + $actual = $collection->toArray(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Collection/GetCest.php b/tests/unit/Collection/GetCest.php index d1ab7dfe367..787f91fc12c 100644 --- a/tests/unit/Collection/GetCest.php +++ b/tests/unit/Collection/GetCest.php @@ -42,6 +42,9 @@ public function collectionGet(UnitTester $I) $actual = $collection->get('three'); $I->assertEquals($expected, $actual); + $actual = $collection->get('THREE'); + $I->assertEquals($expected, $actual); + $actual = $collection['three']; $I->assertEquals($expected, $actual); diff --git a/tests/unit/Collection/HasCest.php b/tests/unit/Collection/HasCest.php index 1d0d8a0aeec..79234fb0a04 100644 --- a/tests/unit/Collection/HasCest.php +++ b/tests/unit/Collection/HasCest.php @@ -41,6 +41,12 @@ public function collectionHas(UnitTester $I) $actual = $collection->has('three'); $I->assertTrue($actual); + $actual = $collection->has('THREE'); + $I->assertTrue($actual); + + $actual = $collection->has('THREE', false); + $I->assertFalse($actual); + $actual = $collection->has('unknown'); $I->assertFalse($actual); diff --git a/tests/unit/Registry/KeyCest.php b/tests/unit/Registry/ClearCest.php similarity index 53% rename from tests/unit/Registry/KeyCest.php rename to tests/unit/Registry/ClearCest.php index ba4ab2c7360..b34c72dec78 100644 --- a/tests/unit/Registry/KeyCest.php +++ b/tests/unit/Registry/ClearCest.php @@ -16,35 +16,35 @@ use UnitTester; /** - * Class KeyCest - * - * @package Phalcon\Test\Unit\Registry + * Class ClearCest */ -class KeyCest +class ClearCest { /** - * Tests Phalcon\Registry :: key() + * Tests Phalcon\Registry :: clear() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryKey(UnitTester $I) + public function collectionClear(UnitTester $I) { - $I->wantToTest('Registry - key()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $I->wantToTest('Registry - clear()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $expected = 'one'; - $actual = $registry->key(); + $expected = $data; + $actual = $registry->toArray(); $I->assertEquals($expected, $actual); - $registry->next(); - $expected = 'two'; - $actual = $registry->key(); + $registry->clear(); + $expected = 0; + $actual = $registry->count(); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/ConstructCest.php b/tests/unit/Registry/ConstructCest.php index 8c6c3574f1d..7db80f0178a 100644 --- a/tests/unit/Registry/ConstructCest.php +++ b/tests/unit/Registry/ConstructCest.php @@ -17,8 +17,6 @@ /** * Class ConstructCest - * - * @package Phalcon\Test\Unit\Registry */ class ConstructCest { @@ -30,13 +28,12 @@ class ConstructCest * @author Phalcon Team * @since 2018-11-13 */ - public function registryConstruct(UnitTester $I) + public function collectionConstruct(UnitTester $I) { - $I->wantToTest('Registry - construct'); + $I->wantToTest('Registry - __construct()'); $registry = new Registry(); - $class = Registry::class; - $actual = $registry; - $I->assertInstanceOf($class, $actual); + $class = Registry::class; + $I->assertInstanceOf($class, $registry); } } diff --git a/tests/unit/Registry/CountCest.php b/tests/unit/Registry/CountCest.php index b51e95dfa72..db20116f0ce 100644 --- a/tests/unit/Registry/CountCest.php +++ b/tests/unit/Registry/CountCest.php @@ -17,8 +17,6 @@ /** * Class CountCest - * - * @package Phalcon\Test\Unit\Registry */ class CountCest { @@ -30,17 +28,19 @@ class CountCest * @author Phalcon Team * @since 2018-11-13 */ - public function registryCount(UnitTester $I) + public function collectionCount(UnitTester $I) { $I->wantToTest('Registry - count()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); + + $I->assertCount(3, $registry->toArray()); - $expected = 3; - $actual = $registry->count(); - $I->assertEquals($expected, $actual); - $I->assertCount($expected, $registry); + $actual = $registry->count(); + $I->assertEquals(3, $actual); } } diff --git a/tests/unit/Registry/GetCest.php b/tests/unit/Registry/GetCest.php new file mode 100644 index 00000000000..9eb73873fe4 --- /dev/null +++ b/tests/unit/Registry/GetCest.php @@ -0,0 +1,54 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Registry; + +use Phalcon\Registry; +use UnitTester; + +/** + * Class GetCest + */ +class GetCest +{ + /** + * Tests Phalcon\Registry :: get() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function collectionGet(UnitTester $I) + { + $I->wantToTest('Registry - get()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); + + $expected = 'four'; + $actual = $registry->get('three'); + $I->assertEquals($expected, $actual); + + $actual = $registry['three']; + $I->assertEquals($expected, $actual); + + $actual = $registry->three; + $I->assertEquals($expected, $actual); + + $actual = $registry->offsetGet('three'); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/unit/Registry/OffsetSetCest.php b/tests/unit/Registry/GetIteratorCest.php similarity index 50% rename from tests/unit/Registry/OffsetSetCest.php rename to tests/unit/Registry/GetIteratorCest.php index ce80085c7d7..d208c4083cc 100644 --- a/tests/unit/Registry/OffsetSetCest.php +++ b/tests/unit/Registry/GetIteratorCest.php @@ -16,28 +16,30 @@ use UnitTester; /** - * Class OffsetSetCest - * - * @package Phalcon\Test\Unit\Registry + * Class GetIteratorCest */ -class OffsetSetCest +class GetIteratorCest { /** - * Tests Phalcon\Registry :: offsetSet() + * Tests Phalcon\Registry :: getIterator() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryOffsetSet(UnitTester $I) + public function collectionGetIterator(UnitTester $I) { - $I->wantToTest('Registry - offsetSet()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $I->wantToTest('Registry - getIterator()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $I->assertCount(3, $registry); + foreach ($registry as $key => $value) { + $I->assertEquals($data[$key], $registry[$key]); + } } } diff --git a/tests/unit/Registry/HasCest.php b/tests/unit/Registry/HasCest.php new file mode 100644 index 00000000000..325204ad1f5 --- /dev/null +++ b/tests/unit/Registry/HasCest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Registry; + +use Phalcon\Registry; +use UnitTester; + +/** + * Class HasCest + */ +class HasCest +{ + /** + * Tests Phalcon\Registry :: has() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function collectionHas(UnitTester $I) + { + $I->wantToTest('Registry - has()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); + + $actual = $registry->has('three'); + $I->assertTrue($actual); + + $actual = $registry->has('unknown'); + $I->assertFalse($actual); + + $actual = isset($registry['three']); + $I->assertTrue($actual); + + $actual = isset($registry['unknown']); + $I->assertFalse($actual); + + $actual = $registry->offsetExists('three'); + $I->assertTrue($actual); + + $actual = $registry->offsetExists('unknown'); + $I->assertFalse($actual); + } +} diff --git a/tests/unit/Registry/UnderscoreGetCest.php b/tests/unit/Registry/InitCest.php similarity index 56% rename from tests/unit/Registry/UnderscoreGetCest.php rename to tests/unit/Registry/InitCest.php index 590a4483ad0..190e2c49385 100644 --- a/tests/unit/Registry/UnderscoreGetCest.php +++ b/tests/unit/Registry/InitCest.php @@ -16,34 +16,36 @@ use UnitTester; /** - * Class UnderscoreGetCest - * - * @package Phalcon\Test\Unit\Registry + * Class InitCest */ -class UnderscoreGetCest +class InitCest { /** - * Tests Phalcon\Registry :: __get() + * Tests Phalcon\Registry :: init() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryUnderscoreGet(UnitTester $I) + public function collectionInit(UnitTester $I) { - $I->wantToTest('Registry - __get()'); + $I->wantToTest('Registry - init()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); - $expected = 3; - $actual = $registry->three; + $expected = 0; + $actual = $registry->count(); $I->assertEquals($expected, $actual); - $expected = 2; - $actual = $registry['two']; + $registry->init($data); + $expected = $data; + $actual = $registry->toArray(); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/NextCest.php b/tests/unit/Registry/JsonSerializeCest.php similarity index 54% rename from tests/unit/Registry/NextCest.php rename to tests/unit/Registry/JsonSerializeCest.php index 9c090166f95..1a03ef0c66a 100644 --- a/tests/unit/Registry/NextCest.php +++ b/tests/unit/Registry/JsonSerializeCest.php @@ -16,31 +16,30 @@ use UnitTester; /** - * Class NextCest - * - * @package Phalcon\Test\Unit\Registry + * Class JsonSerializeCest */ -class NextCest +class JsonSerializeCest { /** - * Tests Phalcon\Registry :: next() + * Tests Phalcon\Registry :: jsonSerialize() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryNext(UnitTester $I) + public function collectionJsonSerialize(UnitTester $I) { - $I->wantToTest('Registry - next()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $I->wantToTest('Registry - jsonSerialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $registry->next(); - $expected = 'two'; - $actual = $registry->key(); + $expected = $data; + $actual = $registry->jsonSerialize(); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/OffsetExistsCest.php b/tests/unit/Registry/OffsetExistsCest.php deleted file mode 100644 index 609f2f71803..00000000000 --- a/tests/unit/Registry/OffsetExistsCest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class OffsetExistsCest - * - * @package Phalcon\Test\Unit\Registry - */ -class OffsetExistsCest -{ - /** - * Tests Phalcon\Registry :: offsetExists() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryOffsetExists(UnitTester $I) - { - $I->wantToTest('Registry - offsetExists()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - - $actual = $registry->offsetExists('one'); - $I->assertTrue($actual); - - $actual = $registry->offsetExists('unknown'); - $I->assertFalse($actual); - } -} diff --git a/tests/unit/Registry/OffsetUnsetCest.php b/tests/unit/Registry/OffsetUnsetCest.php deleted file mode 100644 index 2dbfc883501..00000000000 --- a/tests/unit/Registry/OffsetUnsetCest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class OffsetUnsetCest - * - * @package Phalcon\Test\Unit\Registry - */ -class OffsetUnsetCest -{ - /** - * Tests Phalcon\Registry :: offsetUnset() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryOffsetUnset(UnitTester $I) - { - $I->wantToTest('Registry - offsetUnset()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); - - $I->assertCount(3, $registry); - - $registry->offsetUnset('two'); - - $I->assertCount(2, $registry); - } -} diff --git a/tests/unit/Registry/RemoveCest.php b/tests/unit/Registry/RemoveCest.php new file mode 100644 index 00000000000..eafc18456de --- /dev/null +++ b/tests/unit/Registry/RemoveCest.php @@ -0,0 +1,71 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Registry; + +use Phalcon\Registry; +use UnitTester; + +/** + * Class RemoveCest + */ +class RemoveCest +{ + /** + * Tests Phalcon\Registry :: remove() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function collectionRemove(UnitTester $I) + { + $I->wantToTest('Registry - remove()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); + + $expected = $data; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); + + $registry->remove('five'); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); + + $registry->init($data); + unset($registry['five']); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); + + $registry->init($data); + $registry->offsetUnset('five'); + $expected = [ + 'one' => 'two', + 'three' => 'four', + ]; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/unit/Registry/RewindCest.php b/tests/unit/Registry/RewindCest.php deleted file mode 100644 index fee81dd4f61..00000000000 --- a/tests/unit/Registry/RewindCest.php +++ /dev/null @@ -1,54 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class RewindCest - * - * @package Phalcon\Test\Unit\Registry - */ -class RewindCest -{ - /** - * Tests Phalcon\Registry :: rewind() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryRewind(UnitTester $I) - { - $I->wantToTest('Registry - rewind()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); - - $registry->next(); - $registry->next(); - - $expected = 3; - $actual = $registry->current(); - $I->assertEquals($expected, $actual); - - $registry->rewind(); - - $expected = 1; - $actual = $registry->current(); - $I->assertEquals($expected, $actual); - } -} diff --git a/tests/unit/Registry/OffsetGetCest.php b/tests/unit/Registry/SerializeCest.php similarity index 54% rename from tests/unit/Registry/OffsetGetCest.php rename to tests/unit/Registry/SerializeCest.php index d240ea3af05..22b21a8d881 100644 --- a/tests/unit/Registry/OffsetGetCest.php +++ b/tests/unit/Registry/SerializeCest.php @@ -16,30 +16,30 @@ use UnitTester; /** - * Class OffsetGetCest - * - * @package Phalcon\Test\Unit\Registry + * Class SerializeCest */ -class OffsetGetCest +class SerializeCest { /** - * Tests Phalcon\Registry :: offsetGet() + * Tests Phalcon\Registry :: serialize() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryOffsetGet(UnitTester $I) + public function collectionSerialize(UnitTester $I) { - $I->wantToTest('Registry - offsetGet()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $I->wantToTest('Registry - serialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $expected = 3; - $actual = $registry->offsetGet('three'); + $expected = serialize($data); + $actual = $registry->serialize(); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/SetCest.php b/tests/unit/Registry/SetCest.php new file mode 100644 index 00000000000..6c742cc4d03 --- /dev/null +++ b/tests/unit/Registry/SetCest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Registry; + +use Phalcon\Registry; +use UnitTester; + +/** + * Class SetCest + */ +class SetCest +{ + /** + * Tests Phalcon\Registry :: set() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function collectionSet(UnitTester $I) + { + $I->wantToTest('Registry - set()'); + $registry = new Registry(); + + $registry->set('three', 'two'); + $expected = 'two'; + $actual = $registry->get('three'); + $I->assertEquals($expected, $actual); + + $registry->three = 'Phalcon'; + $expected = 'Phalcon'; + $actual = $registry->get('three'); + $I->assertEquals($expected, $actual); + + $registry->offsetSet('three', 123); + $expected = 123; + $actual = $registry->get('three'); + $I->assertEquals($expected, $actual); + + $registry['three'] = true; + $actual = $registry->get('three'); + $I->assertTrue($actual); + } +} diff --git a/tests/unit/Registry/UnderscoreSetCest.php b/tests/unit/Registry/ToArrayCest.php similarity index 52% rename from tests/unit/Registry/UnderscoreSetCest.php rename to tests/unit/Registry/ToArrayCest.php index 395a09822c1..3026a0191d1 100644 --- a/tests/unit/Registry/UnderscoreSetCest.php +++ b/tests/unit/Registry/ToArrayCest.php @@ -16,26 +16,30 @@ use UnitTester; /** - * Class UnderscoreSetCest + * Class ToArrayCest */ -class UnderscoreSetCest +class ToArrayCest { /** - * Tests Phalcon\Registry :: __set() + * Tests Phalcon\Registry :: toArray() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryUnderscoreSet(UnitTester $I) + public function collectionToArray(UnitTester $I) { - $I->wantToTest('Registry - __set()'); - $registry = new Registry(); - $registry->one = 1; - $registry->two = 2; - $registry['three'] = 3; + $I->wantToTest('Registry - toArray()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $I->assertCount(3, $registry); + $expected = $data; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/CurrentCest.php b/tests/unit/Registry/ToJsonCest.php similarity index 52% rename from tests/unit/Registry/CurrentCest.php rename to tests/unit/Registry/ToJsonCest.php index 50e56547225..1a274a50695 100644 --- a/tests/unit/Registry/CurrentCest.php +++ b/tests/unit/Registry/ToJsonCest.php @@ -16,35 +16,34 @@ use UnitTester; /** - * Class CurrentCest - * - * @package Phalcon\Test\Unit\Registry + * Class ToJsonCest */ -class CurrentCest +class ToJsonCest { /** - * Tests Phalcon\Registry :: current() + * Tests Phalcon\Registry :: toJson() * * @param UnitTester $I * * @author Phalcon Team * @since 2018-11-13 */ - public function registryCurrent(UnitTester $I) + public function collectionToJson(UnitTester $I) { - $I->wantToTest('Registry - current()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - $registry->offsetSet('two', 2); - $registry->offsetSet('three', 3); + $I->wantToTest('Registry - toJson()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $registry = new Registry($data); - $expected = 1; - $actual = $registry->current(); + $expected = json_encode($data); + $actual = $registry->toJson(); $I->assertEquals($expected, $actual); - $registry->next(); - $expected = 2; - $actual = $registry->current(); + $expected = json_encode($data, JSON_PRETTY_PRINT); + $actual = $registry->toJson(JSON_PRETTY_PRINT); $I->assertEquals($expected, $actual); } } diff --git a/tests/unit/Registry/UnderscoreIssetCest.php b/tests/unit/Registry/UnderscoreIssetCest.php deleted file mode 100644 index 3f96c1b8b8b..00000000000 --- a/tests/unit/Registry/UnderscoreIssetCest.php +++ /dev/null @@ -1,45 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class UnderscoreIsset - * - * @package Phalcon\Test\Unit\Registry - */ -class UnderscoreIsset -{ - /** - * Tests Phalcon\Registry :: __isset() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryUnderscoreIsset(UnitTester $I) - { - $I->wantToTest('Registry - __isset()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - - $actual = isset($registry['one']); - $I->assertTrue($actual); - - $actual = isset($registry['unknown']); - $I->assertFalse($actual); - } -} diff --git a/tests/unit/Registry/UnderscoreUnsetCest.php b/tests/unit/Registry/UnderscoreUnsetCest.php deleted file mode 100644 index 7dded623fcd..00000000000 --- a/tests/unit/Registry/UnderscoreUnsetCest.php +++ /dev/null @@ -1,49 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class UnderscoreUnsetCest - */ -class UnderscoreUnsetCest -{ - /** - * Tests Phalcon\Registry :: __unset() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryUnderscoreUnset(UnitTester $I) - { - $I->wantToTest('Registry - __unset()'); - $registry = new Registry(); - $registry->one = 1; - $registry->two = 2; - $registry->three = 3; - - $I->assertCount(3, $registry); - - unset($registry->two); - - $I->assertCount(2, $registry); - - unset($registry['one']); - - $I->assertCount(1, $registry); - } -} diff --git a/tests/unit/Registry/UnserializeCest.php b/tests/unit/Registry/UnserializeCest.php new file mode 100644 index 00000000000..7bb5550c2b5 --- /dev/null +++ b/tests/unit/Registry/UnserializeCest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE.txt + * file that was distributed with this source code. + */ + +namespace Phalcon\Test\Unit\Registry; + +use Phalcon\Registry; +use UnitTester; + +/** + * Class SerializeCest + */ +class UnserializeCest +{ + /** + * Tests Phalcon\Registry :: serialize() + * + * @param UnitTester $I + * + * @author Phalcon Team + * @since 2018-11-13 + */ + public function collectionSerialize(UnitTester $I) + { + $I->wantToTest('Registry - serialize()'); + $data = [ + 'one' => 'two', + 'three' => 'four', + 'five' => 'six', + ]; + $serialized = serialize($data); + $registry = new Registry(); + + $registry->unserialize($serialized); + $expected = $data; + $actual = $registry->toArray(); + $I->assertEquals($expected, $actual); + } +} diff --git a/tests/unit/Registry/ValidCest.php b/tests/unit/Registry/ValidCest.php deleted file mode 100644 index d07c0715d60..00000000000 --- a/tests/unit/Registry/ValidCest.php +++ /dev/null @@ -1,48 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -namespace Phalcon\Test\Unit\Registry; - -use Phalcon\Registry; -use UnitTester; - -/** - * Class ValidCest - * - * @package Phalcon\Test\Unit\Registry - */ -class ValidCest -{ - /** - * Tests Phalcon\Registry :: valid() - * - * @param UnitTester $I - * - * @author Phalcon Team - * @since 2018-11-13 - */ - public function registryValid(UnitTester $I) - { - $I->wantToTest('Registry - valid()'); - $registry = new Registry(); - $registry->offsetSet('one', 1); - - $registry->rewind(); - - $actual = $registry->valid(); - $I->assertTrue($actual); - - $registry->next(); - $actual = $registry->valid(); - $I->assertFalse($actual); - } -}