Skip to content

Commit

Permalink
tests: uses new session api
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 30, 2024
1 parent 93b3773 commit c779293
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 96 deletions.
4 changes: 2 additions & 2 deletions tests/Http/Session.handler.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ $session->setHandler(new MySessionStorage);
$session->start();

$namespace = $session->getSection('one');
$namespace->a = 'apple';
$namespace->set('a', 'apple');
$session->close();
unset($_SESSION);

$session->start();
$namespace = $session->getSection('one');
Assert::same('apple', $namespace->a);
Assert::same('apple', $namespace->get('a'));
2 changes: 1 addition & 1 deletion tests/Http/Session.id.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $session->start();
Assert::same($sessionId, $session->getId());
Assert::same([$sessionName => $leet], $_COOKIE);

Assert::same('yes', $session->getSection('temp')->value);
Assert::same('yes', $session->getSection('temp')->get('value'));
$session->close();

// session was not regenerated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ file_put_contents(getTempDir() . '/sess_' . $sessionId, '__NF|a:1:{s:4:"DATA";a:
$session = new Session(new Http\Request(new Http\UrlScript('http://nette.org'), [], [], $cookies), new Http\Response);
$session->setOptions(['readAndClose' => true]);
$session->start();
Assert::same('yes', $session->getSection('temp')->value);
Assert::same('yes', $session->getSection('temp')->get('value'));

// session was not regenerated
Assert::same($session->getId(), $sessionId);
Expand Down
2 changes: 1 addition & 1 deletion tests/Http/Session.regenerate-empty-session.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ file_put_contents(getTempDir() . '/sess_' . $sessionId, '__NF|a:1:{s:4:"DATA";a:

$session = new Session(new Http\Request(new Http\UrlScript('http://nette.org'), [], [], $cookies), new Http\Response);
$session->start();
Assert::same('yes', $session->getSection('temp')->value);
Assert::same('yes', $session->getSection('temp')->get('value'));

$newSessionId = $session->getId();
$session->close();
Expand Down
4 changes: 2 additions & 2 deletions tests/Http/Session.restart-after-regenerate.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ $session = new Http\Session(new Http\Request(new Http\UrlScript, [], [], $cookie

$session->start();
Assert::same($sessionId, $session->getId());
Assert::same('yes', $session->getSection('temp')->value);
Assert::same('yes', $session->getSection('temp')->get('value'));

$session->regenerateId();
Assert::notSame($sessionId, $session->getId());
Assert::same(session_id(), $session->getId());
$session->close();

$session->start();
Assert::same('yes', $session->getSection('temp')->value);
Assert::same('yes', $session->getSection('temp')->get('value'));

Assert::true(file_exists(getTempDir() . '/sess_' . $session->getId()));
Assert::count(1, glob(getTempDir() . '/sess_*'));
4 changes: 2 additions & 2 deletions tests/Http/Session.sections.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ $section = $session->getSection('trees');
Assert::type(Nette\Http\SessionSection::class, $section);
Assert::false($session->hasSection('trees')); // hasSection() should have returned false for a section with no keys set

$section->hello = 'world';
$section->set('hello', 'world');
Assert::true($session->hasSection('trees')); // hasSection() should have returned true for a section with keys set

$section = $session->getSection('default');
Assert::same(['trees'], $session->getSectionNames());

$section->hello = 'world';
$section->set('hello', 'world');
Assert::same(['trees', 'default'], $session->getSectionNames());
23 changes: 8 additions & 15 deletions tests/Http/SessionSection.basic.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ require __DIR__ . '/../bootstrap.php';
$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript), new Nette\Http\Response);

$namespace = $session->getSection('one');
$namespace->a = 'apple';
$namespace->set('a', 'apple');
$namespace->set('p', 'pear');
$namespace['o'] = 'orange';

Assert::same('apple', $namespace->a);
$namespace->set('o', 'orange');
Assert::same('pear', $namespace->get('p'));
Assert::same('orange', $namespace['o']);
Assert::null($namespace->get('undefined'));

foreach ($namespace as $key => $val) {
$tmp[] = "$key=$val";
Expand All @@ -33,16 +31,11 @@ Assert::same([
'o=orange',
], $tmp);

$namespace->remove('a');
Assert::same('p=pear&o=orange', http_build_query(iterator_to_array($namespace->getIterator())));

Assert::true(isset($namespace['p']));
Assert::true(isset($namespace->o));
Assert::false(isset($namespace->undefined));

unset($namespace['a'], $namespace->o, $namespace->undef);
$namespace->remove('p');
$namespace->remove(['x']);



$namespace->remove(['x', 'p']);
Assert::same('o=orange', http_build_query(iterator_to_array($namespace->getIterator())));

$namespace->remove();
Assert::same('', http_build_query(iterator_to_array($namespace->getIterator())));
28 changes: 0 additions & 28 deletions tests/Http/SessionSection.remove.phpt

This file was deleted.

4 changes: 2 additions & 2 deletions tests/Http/SessionSection.removeExpiration().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ $session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript), new Net
$session->setExpiration('+10 seconds');

$section = $session->getSection('expireRemove');
$section->a = 'apple';
$section->b = 'banana';
$section->set('a', 'apple');
$section->set('b', 'banana');

$section->setExpiration('+2 seconds', 'a');
$section->removeExpiration('a');
Expand Down
28 changes: 15 additions & 13 deletions tests/Http/SessionSection.separated.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,18 @@ require __DIR__ . '/../bootstrap.php';

$session = new Session(new Nette\Http\Request(new Nette\Http\UrlScript), new Nette\Http\Response);

$namespace1 = $session->getSection('namespace1');
$namespace1b = $session->getSection('namespace1');
$namespace2 = $session->getSection('namespace2');
$namespace2b = $session->getSection('namespace2');
$namespace3 = $session->getSection('default');
$namespace3b = $session->getSection('default');
$namespace1->a = 'apple';
$namespace2->a = 'pear';
$namespace3->a = 'orange';
Assert::true($namespace1->a !== $namespace2->a && $namespace1->a !== $namespace3->a && $namespace2->a !== $namespace3->a);
Assert::same($namespace1->a, $namespace1b->a);
Assert::same($namespace2->a, $namespace2b->a);
Assert::same($namespace3->a, $namespace3b->a);
$section1 = $session->getSection('namespace1');
$section1b = $session->getSection('namespace1');
$section2 = $session->getSection('namespace2');
$section2b = $session->getSection('namespace2');
$section3 = $session->getSection('default');
$section3b = $session->getSection('default');
$section1->set('a', 'apple');
$section2->set('a', 'pear');
$section3->set('a', 'orange');
Assert::same('apple', $section1->get('a'));
Assert::same('apple', $section1b->get('a'));
Assert::same('pear', $section2->get('a'));
Assert::same('pear', $section2b->get('a'));
Assert::same('orange', $section3->get('a'));
Assert::same('orange', $section3b->get('a'));
12 changes: 6 additions & 6 deletions tests/Http/SessionSection.setExpiration().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ $session->setExpiration('+10 seconds');

test('try to expire whole namespace', function () use ($session) {
$namespace = $session->getSection('expire');
$namespace->a = 'apple';
$namespace->p = 'pear';
$namespace['o'] = 'orange';
$namespace->set('a', 'apple');
$namespace->set('p', 'pear');
$namespace->setExpiration('+ 1 seconds');

$session->close();
Expand All @@ -35,16 +34,17 @@ test('try to expire whole namespace', function () use ($session) {
test('try to expire only 1 of the keys', function () use ($session) {
$namespace = $session->getSection('expireSingle');
$namespace->setExpiration('1 second', 'g');
$namespace->g = 'guava';
$namespace->p = 'plum';
$namespace->set('g', 'guava');
$namespace->set('p', 'plum');
$namespace->setExpiration('1 second', 'p');
$namespace->set('a', 'apple', '1 second');

$session->close();
sleep(2);
$session->start();

$namespace = $session->getSection('expireSingle');
Assert::same('p=plum', http_build_query(iterator_to_array($namespace->getIterator())));
Assert::same('g=guava', http_build_query(iterator_to_array($namespace->getIterator())));
});


Expand Down
20 changes: 0 additions & 20 deletions tests/Http/SessionSection.undefined.phpt

This file was deleted.

3 changes: 0 additions & 3 deletions tests/Http/Url.httpScheme.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,11 @@ Assert::same('hostname', $url->host);
Assert::same(60, $url->port);
Assert::same(80, $url->getDefaultPort());
Assert::same('/p%61th/script.php', $url->path);
Assert::same('/p%61th/', $url->basePath);
Assert::same('arg=value', $url->query);
Assert::same('anchor', $url->fragment);
Assert::same('username%3A:password%3A@hostname:60', $url->authority);
Assert::same('http://username%3A:password%3A@hostname:60', $url->hostUrl);
Assert::same('http://username%3A:password%3A@hostname:60/p%61th/script.php?arg=value#anchor', $url->absoluteUrl);
Assert::same('http://username%3A:password%3A@hostname:60/p%61th/', $url->baseUrl);
Assert::same('script.php?arg=value#anchor', $url->relativeUrl);

$url->setPath('');
Assert::same('/', $url->getPath());
Expand Down

0 comments on commit c779293

Please sign in to comment.