From 1049e41575e786e8ed18a5969e1cdc77432e4101 Mon Sep 17 00:00:00 2001 From: kenjis Date: Fri, 7 Aug 2015 09:02:30 +0900 Subject: [PATCH] Add docs about $this->resetInstance() --- README.md | 4 ++-- docs/FunctionAndClassReference.md | 33 +++++++++++++++++++++++++++++++ docs/HowToWriteTests.md | 8 ++------ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d581c6b6..fca9ac59 100644 --- a/README.md +++ b/README.md @@ -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 CI =& get_instance(); + $this->resetInstance(); $this->CI->load->model('Inventory_model'); $this->obj = $this->CI->Inventory_model; } diff --git a/docs/FunctionAndClassReference.md b/docs/FunctionAndClassReference.md index 4d7c972f..83cf6c1b 100644 --- a/docs/FunctionAndClassReference.md +++ b/docs/FunctionAndClassReference.md @@ -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) @@ -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 | @@ -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 | diff --git a/docs/HowToWriteTests.md b/docs/HowToWriteTests.md index b6433ced..675bccb7 100644 --- a/docs/HowToWriteTests.md +++ b/docs/HowToWriteTests.md @@ -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; } @@ -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).