Skip to content

Commit

Permalink
Merge pull request #31 from PhpGt/30-remove-store
Browse files Browse the repository at this point in the history
Self-removal of SessionStore
  • Loading branch information
g105b authored Feb 22, 2019
2 parents 14378ca + c63cea4 commit 92f0497
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 27 deletions.
4 changes: 2 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2
jobs:
build:
docker:
- image: circleci/php:7.1-cli
- image: circleci/php:7.2-cli

working_directory: ~/repo

Expand All @@ -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 ./test/unit/_coverage -d memory_limit=512M

- store_test_results:
path: test/unit/_junit
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",

"require": {
"php": "^7.1"
"php": ">=7.2"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
Expand Down
16 changes: 8 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion src/Session.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Gt\Session;

use ArrayAccess;
use SessionHandlerInterface;

class Session {
Expand Down
28 changes: 22 additions & 6 deletions src/SessionStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -81,8 +88,7 @@ public function getStore(
}

public function setStore(
string $namespace,
SessionStore $newStore = null
string $namespace
):void {
$namespaceParts = explode(".", $namespace);
$store = $this;
Expand All @@ -96,7 +102,8 @@ public function setStore(
if (is_null($nextStore)) {
$nextStore = new SessionStore(
$storeName,
$this->session
$this->session,
$store
);
$store->stores[$storeName] = $nextStore;
}
Expand Down Expand Up @@ -169,7 +176,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, ".");

Expand Down
7 changes: 5 additions & 2 deletions src/SessionStoreFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
6 changes: 6 additions & 0 deletions test/unit/Helper/FunctionOverride/session_id.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php
namespace Gt\Session;
function session_id() {
\Gt\Session\Test\Helper\FunctionMocker::$mockCalls["session_id"] []= func_get_args();
return "TEST";
}
31 changes: 30 additions & 1 deletion test/unit/SessionStoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,26 @@

use Gt\Session\Handler;
use Gt\Session\Session;
use Gt\Session\SessionStore;
use Gt\Session\Test\Helper\FunctionMocker;
use Gt\Session\Test\Helper\DataProvider\KeyValuePairProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use SessionHandler;

class SessionStoreTest extends TestCase {
use KeyValuePairProvider;

public function setUp() {
public function setUp():void {
FunctionMocker::mock("session_start");
FunctionMocker::mock("session_id");
}

/**
* @dataProvider data_randomKeyValuePairs
*/
public function testGetSetDotNotation(array $keyValuePairs):void {
/** @var MockObject|SessionHandler $handler */
$handler = $this->getMockBuilder(Handler::class)
->getMock();
$session = new Session($handler);
Expand All @@ -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")
);
}
}
2 changes: 1 addition & 1 deletion test/unit/SessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SessionTest extends TestCase {
use StringProvider;
use ConfigProvider;

public function setUp() {
public function setUp():void {
FunctionMocker::mock("session_start");
}

Expand Down
5 changes: 0 additions & 5 deletions test/unit/phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@
</testsuite>
</testsuites>

<logging>
<log type="coverage-text" target="php://stdout" showUncoveredFiles="true" />
<log type="coverage-html" target="./_coverage" charset="UTF-8" highlight="false" lowUpperBound="35" highLowerBound="70" />
</logging>

<filter>
<whitelist>
<directory suffix=".php">../../src</directory>
Expand Down

0 comments on commit 92f0497

Please sign in to comment.