Skip to content

Commit

Permalink
Add docs about $this->resetInstance()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Aug 7, 2015
1 parent ce46307 commit 1049e41
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -106,7 +106,7 @@ To generate coverage report, Xdebug is needed.

## How to Write Tests

As an example, a test case class for Inventory_model would be as follows:
As an example, a test case class for `Inventory_model` would be as follows:

~~~php
<?php
Expand All @@ -115,7 +115,7 @@ class Inventory_model_test extends TestCase
{
public function setUp()
{
$this->CI =& get_instance();
$this->resetInstance();
$this->CI->load->model('Inventory_model');
$this->obj = $this->CI->Inventory_model;
}
Expand Down
33 changes: 33 additions & 0 deletions docs/FunctionAndClassReference.md
Expand Up @@ -12,6 +12,7 @@ version: **master** |
- [*function* `set_is_cli($return)`](#function-set_is_clireturn)
- [*function* `load_class_instance($classname, $instance)`](#function-load_class_instanceclassname-instance)
- [*class* TestCase](#class-testcase)
- [`TestCase::resetInstance()`](#testcaseresetinstance)
- [`TestCase::request($method, $argv, $params = [], $callable = null)`](#testcaserequestmethod-argv-params---callable--null)
- [`request->setCallable()`](#request-setcallable)
- [`request->setCallablePreConstructor()`](#request-setcallablepreconstructor)
Expand Down Expand Up @@ -46,6 +47,8 @@ $controller = new Welcome();
$this->CI =& get_instance();
~~~

Normally, you don't have to use this function. Use [`TestCase::resetInstance()`](#testcaseresetinstance) method instead.

### *function* `set_is_cli($return)`

| param | type | description |
Expand Down Expand Up @@ -78,6 +81,36 @@ load_class_instance('email', $email);

### *class* TestCase

#### `TestCase::resetInstance()`

Reset CodeIgniter instance and assign new CodeIgniter instance as `$this->CI`.

~~~php
public function setUp()
{
$this->resetInstance();
$this->CI->load->model('Category_model');
$this->obj = $this->CI->Category_model;
}
~~~

**Upgrade Note for v0.6.0**

Before v0.6.0, we write `setUp()` method like this:

~~~php
public function setUp()
{
$this->CI =& get_instance();
$this->CI->load->model('Category_model');
$this->obj = $this->CI->Category_model;
}
~~~

When you use the way, you use the same CodeIgniter instance and the same `Category_model` instance in every test method.

In contrast, if you use `$this->resetInstance()`, it resets CodeIgniter instance and `Category_model`. So you use new CodeIgniter instance and new `Category_model` instance in every test method.

#### `TestCase::request($method, $argv, $params = [], $callable = null)`

| param | type | description |
Expand Down
8 changes: 2 additions & 6 deletions docs/HowToWriteTests.md
Expand Up @@ -203,7 +203,7 @@ If you don't know well about PHPUnit Mock Objects, see [Test Doubles](https://ph
~~~php
public function setUp()
{
$this->CI =& get_instance();
$this->resetInstance();
$this->CI->load->model('Category_model');
$this->obj = $this->CI->Category_model;
}
Expand Down Expand Up @@ -254,14 +254,10 @@ If you don't know well about PHPUnit Mock Objects, see [Test Doubles](https://ph
foreach ($list as $category) {
$this->assertEquals($expected[$category->id], $category->name);
}

// Reset CI object for next test case, unless property db won't work
reset_instance();
new CI_Controller();
}
~~~

**Note:** Once you have replaced CodeIgniter object's property with your mock, the mock remains until you call `reset_instance()` and instantiate a controller.
[$this->resetInstance()](FunctionAndClassReference.md#testcaseresetinstance) method in *CI PHPUnit Test* is a helper method to reset CodeIgniter instance and assign new CodeIgniter instance as `$this->CI`.

See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/master/application/tests/models/Category_model_mocking_db_test.php).

Expand Down

0 comments on commit 1049e41

Please sign in to comment.