Skip to content

Commit 083eb14

Browse files
committed
Updates the unit tests
1 parent 6b19321 commit 083eb14

File tree

8 files changed

+112
-24
lines changed

8 files changed

+112
-24
lines changed

Tests/FunctionsTest.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@
88
class FunctionsTest extends TestCase
99
{
1010

11-
public function test_session_function()
12-
{
13-
$this->assertInstanceOf(Session::class, session());
14-
$this->assertSame(session()->token(), session()->token());
15-
}
16-
1711
public function test_should_throw_exception_on_invalid_handler_class()
1812
{
1913
$this->expectException(SessionException::class);
20-
$this->expectExceptionCode(SessionException::E_HANDLER_NOT_FOUND);
14+
$this->expectExceptionCode(0);
2115
$this->expectExceptionMessage('Failed to load the session handler class. Requested Koded\Session\Handler\StdClassHandler');
2216

2317
$config = (new Config)->import([

Tests/Handler/FilesHandlerTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ protected function setUp()
2222

2323
'use_cookies' => false,
2424
'cache_limiter' => '',
25-
// 'cookie_path' => '/tmp',
2625
'gc_maxlifetime' => 60,
2726
]
2827
]);

Tests/Handler/MemcachedHandlerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class MemcachedHandlerTest extends TestCase
1313

1414
protected function setUp()
1515
{
16+
if (false === extension_loaded('memcached')) {
17+
$this->markTestSkipped('Memcached extension is not loaded');
18+
}
19+
1620
if (false === extension_loaded('memcached')) {
1721
$this->markTestSkipped('Memcached extension is not loaded.');
1822
}

Tests/Handler/RedisHandlerTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ class RedisHandlerTest extends TestCase
1414

1515
protected function setUp()
1616
{
17+
if (false === extension_loaded('redis')) {
18+
$this->markTestSkipped('Redis extension is not loaded');
19+
}
20+
1721
$config = (new Config)->import([
1822
'session' => [
1923
'name' => 'test',
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Koded\Session;
4+
5+
use Koded\Http\{ServerRequest, ServerResponse, StatusCode};
6+
use Koded\Stdlib\Config;
7+
use PHPUnit\Framework\TestCase;
8+
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
9+
use Psr\Http\Server\RequestHandlerInterface;
10+
11+
class SessionAuthenticatedMiddlewareTest extends TestCase
12+
{
13+
14+
/** @var SessionAuthenticatedMiddleware */
15+
private $middleware;
16+
17+
public function test__construct()
18+
{
19+
$this->assertAttributeEquals('/signin', 'redirectTo', $this->middleware);
20+
}
21+
22+
public function test_process_when_not_authenticated()
23+
{
24+
$handler = new class implements RequestHandlerInterface
25+
{
26+
public function handle(ServerRequestInterface $request): ResponseInterface
27+
{
28+
return new ServerResponse('hello');
29+
}
30+
};
31+
32+
$this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
33+
34+
$response = $this->middleware->process(new ServerRequest, $handler);
35+
36+
$this->assertEquals('/signin', $response->getHeaderLine('location'));
37+
$this->assertSame(StatusCode::TEMPORARY_REDIRECT, $response->getStatusCode());
38+
$this->assertEmpty((string)$response->getBody());
39+
}
40+
41+
public function test_process_ajax_when_not_authenticated()
42+
{
43+
$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XmlHttpRequest';
44+
45+
$handler = new class implements RequestHandlerInterface
46+
{
47+
public function handle(ServerRequestInterface $request): ResponseInterface
48+
{
49+
return new ServerResponse();
50+
}
51+
};
52+
53+
$this->assertFalse(isset($_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED]));
54+
55+
$response = $this->middleware->process(new ServerRequest, $handler);
56+
57+
$this->assertEquals('', $response->getHeaderLine('location'));
58+
$this->assertSame(StatusCode::UNAUTHORIZED, $response->getStatusCode());
59+
$this->assertSame('{"location":"\/signin","status":401}', (string)$response->getBody());
60+
}
61+
62+
public function test_process_when_authenticated()
63+
{
64+
$_SESSION[SessionAuthenticatedMiddleware::AUTHENTICATED] = true;
65+
$handler = new class implements RequestHandlerInterface
66+
{
67+
public function handle(ServerRequestInterface $request): ResponseInterface
68+
{
69+
return new ServerResponse('hello');
70+
}
71+
};
72+
73+
$response = $this->middleware->process(new ServerRequest, $handler);
74+
75+
$this->assertEquals('', $response->getHeaderLine('location'));
76+
$this->assertSame(StatusCode::OK, $response->getStatusCode());
77+
$this->assertEquals('hello', (string)$response->getBody());
78+
}
79+
80+
protected function setUp()
81+
{
82+
$this->middleware = new SessionAuthenticatedMiddleware((new Config)->import([
83+
SessionAuthenticatedMiddleware::LOGIN_URI => '/signin',
84+
]));
85+
}
86+
}

Tests/SessionMiddlewareTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Koded\Http\{ServerRequest, ServerResponse};
66
use Koded\Stdlib\Config;
7+
use PHPUnit\Framework\Assert;
78
use PHPUnit\Framework\TestCase;
89
use Psr\Http\Message\{ResponseInterface, ServerRequestInterface};
910
use Psr\Http\Server\RequestHandlerInterface;
@@ -23,12 +24,13 @@ public function test_should_start_the_session_and_close_the_session()
2324
{
2425
public function handle(ServerRequestInterface $request): ResponseInterface
2526
{
27+
Assert::assertTrue($request->getAttribute(SessionMiddleware::SESSION_STARTED), 'Session is started');
2628
return new ServerResponse;
2729
}
2830
});
2931

3032
$this->assertSame(PHP_SESSION_NONE, session_status());
31-
$this->assertTrue($request->getAttribute(SessionMiddleware::SESSION_STARTED));
33+
3234
}
3335

3436
public function test_should_start_the_session_and_keep_the_session()
@@ -40,20 +42,18 @@ public function test_should_start_the_session_and_keep_the_session()
4042
{
4143
public function handle(ServerRequestInterface $request): ResponseInterface
4244
{
45+
Assert::assertTrue($request->getAttribute(SessionMiddleware::SESSION_STARTED), 'Session is started');
4346
return new ServerResponse('', 500);
4447
}
4548
});
4649

4750
$this->assertSame(PHP_SESSION_ACTIVE, session_status());
48-
$this->assertTrue($request->getAttribute(SessionMiddleware::SESSION_STARTED));
4951
}
5052

5153
protected function setUp()
5254
{
5355
$settings = new SessionConfiguration((new Config)->import([
54-
'session' => [
55-
'use_cookies' => false
56-
]
56+
'session' => ['use_cookies' => false]
5757
]));
5858

5959
$this->middleware = new SessionMiddleware($settings);

Tests/SessionTestCaseTrait.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ public function test_constructor()
1616
$this->assertNotEmpty($this->SUT->token());
1717
}
1818

19-
// public function test_multiple_session_creation() {
20-
// $this->expectException(\RuntimeException::class);
21-
// $this->expectExceptionMessage('[BUG] The session is already created. Please fix your code.');
22-
// new PhpSession;
23-
// }
24-
25-
/*
26-
*
27-
* (aliases for ArrayObject methods)
28-
*
29-
*/
3019
public function test_get()
3120
{
3221
$this->assertSame('bar', $this->SUT->get('foo'));
@@ -81,6 +70,16 @@ public function test_has()
8170
$this->assertFalse($this->SUT->has('this_is_not_set'));
8271
}
8372

73+
public function test_toData()
74+
{
75+
$data = $this->SUT->toData();
76+
77+
$this->assertEquals('bar', $data->foo);
78+
$this->assertNull($data->get('_stamp'));
79+
$this->assertNull($data->get('_agent'));
80+
$this->assertNull($data->get('_token'));
81+
}
82+
8483
/*
8584
*
8685
* (mutator methods)

Tests/loader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
22

3+
error_reporting(E_ALL);
4+
35
require_once __DIR__ . '/../vendor/autoload.php';
46
require_once __DIR__ . '/../vendor/koded/stdlib/functions-dev.php';

0 commit comments

Comments
 (0)