From 50f24576410af5f4f29a63730e477ee72282693e Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:31:24 +0000 Subject: [PATCH 1/7] Add tests for #30 --- .../Helper/FunctionOverride/session_id.php | 6 ++++ test/unit/SessionStoreTest.php | 29 +++++++++++++++++++ 2 files changed, 35 insertions(+) create mode 100644 test/unit/Helper/FunctionOverride/session_id.php diff --git a/test/unit/Helper/FunctionOverride/session_id.php b/test/unit/Helper/FunctionOverride/session_id.php new file mode 100644 index 0000000..a2c22a9 --- /dev/null +++ b/test/unit/Helper/FunctionOverride/session_id.php @@ -0,0 +1,6 @@ +getMockBuilder(Handler::class) ->getMock(); $session = new Session($handler); @@ -34,4 +39,28 @@ public function testGetSetDotNotation(array $keyValuePairs):void { self::assertEquals($value, $session->get($fullKey)); } } + + public function testRemoveSelf():void { + /** @var MockObject|SessionHandler $handler */ + $handler = $this->getMockBuilder(Handler::class) + ->getMock(); + $session = new Session($handler); + + $leafStore = $session->getStore( + "gt.test.session.trunk.leaf", + true + ); + $trunkStore = $session->getStore( + "gt.test.session.trunk" + ); + + $trunkStore->remove(); + + self::assertNull($session->getStore("gt.test.session.trunk")); + self::assertNull($session->getStore("gt.test.session.trunk.leaf")); + self::assertInstanceOf( + SessionStore::class, + $session->getStore("gt.test.session") + ); + } } \ No newline at end of file From d23a59095fb201709640ac6efd9f651ab9c50ff9 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:31:44 +0000 Subject: [PATCH 2/7] Implement SessionStore self-removal, closes #30 --- src/SessionStore.php | 25 +++++++++++++++++++++---- src/SessionStoreFactory.php | 7 +++++-- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/SessionStore.php b/src/SessionStore.php index c06d642..02d5039 100644 --- a/src/SessionStore.php +++ b/src/SessionStore.php @@ -10,10 +10,17 @@ class SessionStore { protected $stores; /** @var string[] */ protected $data; - - public function __construct(string $name, Session $session) { + /** @var SessionStore */ + protected $parentStore; + + public function __construct( + string $name, + Session $session, + self $parentStore = null + ) { $this->name = $name; $this->session = $session; + $this->parentStore = $parentStore; } public function setData(string $key, $value):void { @@ -96,7 +103,8 @@ public function setStore( if (is_null($nextStore)) { $nextStore = new SessionStore( $storeName, - $this->session + $this->session, + $store ); $store->stores[$storeName] = $nextStore; } @@ -169,7 +177,16 @@ public function contains(string $key):bool { return $store->containsData($key); } - public function remove(string $key):void { + public function remove(string $key = null):void { + if(is_null($key)) { + foreach($this->stores as $i => $childStore) { + unset($this->stores[$i]); + } + + $this->parentStore->remove($this->name); + return; + } + $store = $this; $lastDotPosition = strrpos($key, "."); diff --git a/src/SessionStoreFactory.php b/src/SessionStoreFactory.php index 8fbc183..10bdae1 100644 --- a/src/SessionStoreFactory.php +++ b/src/SessionStoreFactory.php @@ -4,10 +4,13 @@ class SessionStoreFactory { public static function create(string $namespace, Session $session):SessionStore { $namespaceParts = explode(".", $namespace); - $store = new SessionStore(array_shift($namespaceParts), $session); + $store = new SessionStore( + array_shift($namespaceParts), + $session + ); foreach($namespaceParts as $part) { - $innerStore = $store->createStore($part, $session); + $innerStore = $store->createStore($part); $store = $innerStore; } From 244064063b33139c8c9c6fd67e1b8a9961d7f497 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:33:28 +0000 Subject: [PATCH 3/7] Tidy code style --- src/Session.php | 1 - src/SessionStore.php | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Session.php b/src/Session.php index 2d7b622..5a47b47 100644 --- a/src/Session.php +++ b/src/Session.php @@ -1,7 +1,6 @@ Date: Fri, 22 Feb 2019 14:37:39 +0000 Subject: [PATCH 4/7] Fix dependencies --- composer.json | 2 +- composer.lock | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index c4b6b15..5f37b3b 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "license": "MIT", "require": { - "php": "^7.1" + "php": ">=7.2" }, "require-dev": { "phpunit/phpunit": "^8.0" diff --git a/composer.lock b/composer.lock index 2775d0c..a13f63a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "29c7e25a17d15ac24ea43ef2c331c4f8", + "content-hash": "39b657e48de8b924fed3a9a11ac33f16", "packages": [], "packages-dev": [ { @@ -582,16 +582,16 @@ }, { "name": "phpunit/php-timer", - "version": "2.0.0", + "version": "2.1.1", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-timer.git", - "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f" + "reference": "8b389aebe1b8b0578430bda0c7c95a829608e059" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/8b8454ea6958c3dee38453d3bd571e023108c91f", - "reference": "8b8454ea6958c3dee38453d3bd571e023108c91f", + "url": "https://api.github.com/repos/sebastianbergmann/php-timer/zipball/8b389aebe1b8b0578430bda0c7c95a829608e059", + "reference": "8b389aebe1b8b0578430bda0c7c95a829608e059", "shasum": "" }, "require": { @@ -603,7 +603,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.0-dev" + "dev-master": "2.1-dev" } }, "autoload": { @@ -627,7 +627,7 @@ "keywords": [ "timer" ], - "time": "2018-02-01T13:07:23+00:00" + "time": "2019-02-20T10:12:59+00:00" }, { "name": "phpunit/php-token-stream", @@ -1485,7 +1485,7 @@ "prefer-stable": false, "prefer-lowest": false, "platform": { - "php": "^7.1" + "php": ">=7.2" }, "platform-dev": [] } From ef11de085e9f734e3afd475c08c18b2afde43843 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:38:16 +0000 Subject: [PATCH 5/7] Update CI to match version requirement --- .circleci/config.yml | 4 ++-- test/unit/phpunit.xml | 5 ----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 180b379..2ce927e 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -3,7 +3,7 @@ version: 2 jobs: build: docker: - - image: circleci/php:7.1-cli + - image: circleci/php:7.2-cli working_directory: ~/repo @@ -23,7 +23,7 @@ jobs: key: v1-dependencies-{{ checksum "composer.json" }} - run: mkdir test/unit/_junit - - run: vendor/bin/phpunit -c test/unit/phpunit.xml --log-junit test/unit/_junit/junit.xml -d memory_limit=512M + - run: vendor/bin/phpunit -c test/unit/phpunit.xml --log-junit test/unit/_junit/junit.xml --coverage-html ./_coverage -d memory_limit=512M - store_test_results: path: test/unit/_junit diff --git a/test/unit/phpunit.xml b/test/unit/phpunit.xml index 2aceb67..bb2e951 100644 --- a/test/unit/phpunit.xml +++ b/test/unit/phpunit.xml @@ -6,11 +6,6 @@ - - - - - ../../src From c5266edd65eb6a77bf4e1d0996e20efd4aea8586 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:39:31 +0000 Subject: [PATCH 6/7] PHPUnit 8 tweaks --- test/unit/SessionStoreTest.php | 2 +- test/unit/SessionTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/test/unit/SessionStoreTest.php b/test/unit/SessionStoreTest.php index 333df72..6be18ad 100644 --- a/test/unit/SessionStoreTest.php +++ b/test/unit/SessionStoreTest.php @@ -13,7 +13,7 @@ class SessionStoreTest extends TestCase { use KeyValuePairProvider; - public function setUp() { + public function setUp():void { FunctionMocker::mock("session_start"); FunctionMocker::mock("session_id"); } diff --git a/test/unit/SessionTest.php b/test/unit/SessionTest.php index 46d0282..1d4fc1d 100644 --- a/test/unit/SessionTest.php +++ b/test/unit/SessionTest.php @@ -14,7 +14,7 @@ class SessionTest extends TestCase { use StringProvider; use ConfigProvider; - public function setUp() { + public function setUp():void { FunctionMocker::mock("session_start"); } From c63cea48b2d90d95694d31bcc34e33aa7bc53f24 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Fri, 22 Feb 2019 14:41:31 +0000 Subject: [PATCH 7/7] Store Unit Test coverage within unit directory --- .circleci/config.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2ce927e..68a7c5c 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -23,7 +23,7 @@ jobs: key: v1-dependencies-{{ checksum "composer.json" }} - run: mkdir test/unit/_junit - - run: vendor/bin/phpunit -c test/unit/phpunit.xml --log-junit test/unit/_junit/junit.xml --coverage-html ./_coverage -d memory_limit=512M + - run: vendor/bin/phpunit -c test/unit/phpunit.xml --log-junit test/unit/_junit/junit.xml --coverage-html ./test/unit/_coverage -d memory_limit=512M - store_test_results: path: test/unit/_junit