Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.

Commit 100dc52

Browse files
committed
Update examples; Add session; More docs
1 parent 4122733 commit 100dc52

File tree

6 files changed

+58
-10
lines changed

6 files changed

+58
-10
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,22 @@ See the provided [composer.json](src/composer.json) for examples.
3131
All tests go in the **tests/** directory. There are two generic subfolders for you,
3232
**unit** and **database** but feel free to make your own. Tests must extend the appropriate
3333
test case:
34-
* For basic tests extend `CodeIgniter\Test\CIDatabaseTestCase`
34+
* For basic tests extend `CodeIgniter\Test\CIUnitTestCase`
3535
* For database tests extend `CIModuleTests\Support\DatabaseTestCase`
3636

3737
Tests are individual methods within each file. Method names must start with the word "test":
3838
`testUserSync()` `testOutputColor()` `testFooBar()`
3939

40-
## Database Tests
40+
### Database Tests
4141

4242
If you are using database tests that require a live database connect you will need to edit
4343
**phpunit.xml.dist**, uncomment the database configuration lines and add your connection
4444
details. Example directories and files are provided for test Seeds and Models, which you
4545
can modify or replace with your own. Also be sure to modify
4646
**tests/_support/DatabaseTestCase.php** to point to your seed and include any additional
4747
steps in `setUp()`.
48+
49+
### Session Tests
50+
51+
Similarly there is a pre-configured test case available with a mock session configured to
52+
make testing sessions easy: **tests/_support/SessionTestCase.php**.

src/tests/_support/DatabaseTestCase.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,8 @@ class DatabaseTestCase extends \CodeIgniter\Test\CIDatabaseTestCase
3030
*/
3131
protected $namespace = 'CIModuleTests\Support';
3232

33-
/**
34-
* @var SessionHandler
35-
*/
36-
protected $session;
37-
3833
public function setUp(): void
3934
{
4035
parent::setUp();
41-
4236
}
4337
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php namespace CIModuleTests\Support;
2+
3+
use CodeIgniter\Session\Handlers\ArrayHandler;
4+
use Tests\Support\Session\MockSession;
5+
6+
class SessionTestCase extends \CodeIgniter\Test\CIDatabaseTestCase
7+
{
8+
/**
9+
* @var SessionHandler
10+
*/
11+
protected $session;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->mockSession();
18+
}
19+
20+
/**
21+
* Pre-loads the mock session driver into $this->session.
22+
*
23+
* @var string
24+
*/
25+
protected function mockSession()
26+
{
27+
require_once ROOTPATH . 'tests/_support/Session/MockSession.php';
28+
$config = config('App');
29+
$this->session = new MockSession(new ArrayHandler($config, '0.0.0.0'), $config);
30+
\Config\Services::injectMock('session', $this->session);
31+
}
32+
}

src/tests/database/ExampleDatabaseTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class ExampleDatabaseTest extends CIModuleTests\Support\DatabaseTestCase
55
public function setUp(): void
66
{
77
parent::setUp();
8-
98
}
109

1110
public function testDatabaseSimple()
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
class ExampleSessionTest extends CIModuleTests\Support\SessionTestCase
4+
{
5+
public function setUp(): void
6+
{
7+
parent::setUp();
8+
}
9+
10+
public function testSessionSimple()
11+
{
12+
$this->session->set('logged_in', 123);
13+
14+
$value = $this->session->get('logged_in')
15+
16+
$this->assertEquals(123, $value);
17+
}
18+
}

src/tests/unit/ExampleTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
class ExampleTest extends \CodeIgniter\Test\CIDatabaseTestCase
3+
class ExampleTest extends \CodeIgniter\Test\CIUnitTestCase
44
{
55
public function setUp(): void
66
{

0 commit comments

Comments
 (0)