From 2148ac20062f6182f5439ca78456e9bb27415435 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Mar 2018 11:37:17 +0900 Subject: [PATCH 1/5] feat: Add UnitTestCase class BC break: move newController(),newModel(),newLibrary() to UnitTestCase class --- application/tests/UnitTestCase.php | 5 ++ .../tests/_ci_phpunit_test/CIPHPUnitTest.php | 6 ++ .../_ci_phpunit_test/CIPHPUnitTestCase.php | 56 --------------- .../CIPHPUnitTestUnitTestCase.php | 68 +++++++++++++++++++ 4 files changed, 79 insertions(+), 56 deletions(-) create mode 100644 application/tests/UnitTestCase.php create mode 100644 application/tests/_ci_phpunit_test/CIPHPUnitTestUnitTestCase.php diff --git a/application/tests/UnitTestCase.php b/application/tests/UnitTestCase.php new file mode 100644 index 00000000..9066d54e --- /dev/null +++ b/application/tests/UnitTestCase.php @@ -0,0 +1,5 @@ +CI =& get_instance(); } - /** - * Create a controller instance - * - * @param string $classname - * @return CI_Controller - */ - public function newController($classname) - { - reset_instance(); - $controller = new $classname; - $this->CI =& get_instance(); - return $controller; - } - - /** - * Create a model instance - * - * @param string $classname - * @return CI_Model - */ - public function newModel($classname) - { - $this->resetInstance(); - $this->CI->load->model($classname); - - // Is the model in a sub-folder? - if (($last_slash = strrpos($classname, '/')) !== FALSE) - { - $classname = substr($classname, ++$last_slash); - } - - return $this->CI->$classname; - } - - /** - * Create a library instance - * - * @param string $classname - * @param array $args - * @return object - */ - public function newLibrary($classname, $args = null) - { - $this->resetInstance(); - $this->CI->load->library($classname, $args); - - // Is the library in a sub-folder? - if (($last_slash = strrpos($classname, '/')) !== FALSE) - { - $classname = substr($classname, ++$last_slash); - } - $classname = strtolower($classname); - - return $this->CI->$classname; - } - protected function tearDown() { if (class_exists('MonkeyPatch', false)) diff --git a/application/tests/_ci_phpunit_test/CIPHPUnitTestUnitTestCase.php b/application/tests/_ci_phpunit_test/CIPHPUnitTestUnitTestCase.php new file mode 100644 index 00000000..4d651882 --- /dev/null +++ b/application/tests/_ci_phpunit_test/CIPHPUnitTestUnitTestCase.php @@ -0,0 +1,68 @@ + + * @license MIT License + * @copyright 2015 Kenji Suzuki + * @link https://github.com/kenjis/ci-phpunit-test + */ + +class CIPHPUnitTestUnitTestCase extends CIPHPUnitTestCase +{ + /** + * Create a controller instance + * + * @param string $classname + * @return CI_Controller + */ + public function newController($classname) + { + reset_instance(); + $controller = new $classname; + $this->CI =& get_instance(); + return $controller; + } + + /** + * Create a model instance + * + * @param string $classname + * @return CI_Model + */ + public function newModel($classname) + { + $this->resetInstance(); + $this->CI->load->model($classname); + + // Is the model in a sub-folder? + if (($last_slash = strrpos($classname, '/')) !== FALSE) + { + $classname = substr($classname, ++$last_slash); + } + + return $this->CI->$classname; + } + + /** + * Create a library instance + * + * @param string $classname + * @param array $args + * @return object + */ + public function newLibrary($classname, $args = null) + { + $this->resetInstance(); + $this->CI->load->library($classname, $args); + + // Is the library in a sub-folder? + if (($last_slash = strrpos($classname, '/')) !== FALSE) + { + $classname = substr($classname, ++$last_slash); + } + $classname = strtolower($classname); + + return $this->CI->$classname; + } +} From 9e9813741bd826ca73c7a91bad9b889359a2253e Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Mar 2018 12:36:52 +0900 Subject: [PATCH 2/5] docs: update TestCase::newLibrary --- docs/FunctionAndClassReference.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/docs/FunctionAndClassReference.md b/docs/FunctionAndClassReference.md index 49557c54..5eac6760 100644 --- a/docs/FunctionAndClassReference.md +++ b/docs/FunctionAndClassReference.md @@ -1,6 +1,7 @@ # ci-phpunit-test for CodeIgniter 3.x -version: **v0.15.0** | +version: **v0.16.0** | +[v0.15.0](https://github.com/kenjis/ci-phpunit-test/blob/v0.15.0/docs/FunctionAndClassReference.md) | [v0.14.0](https://github.com/kenjis/ci-phpunit-test/blob/v0.14.0/docs/FunctionAndClassReference.md) | [v0.13.0](https://github.com/kenjis/ci-phpunit-test/blob/v0.13.0/docs/FunctionAndClassReference.md) | [v0.12.2](https://github.com/kenjis/ci-phpunit-test/blob/v0.12.2/docs/FunctionAndClassReference.md) | @@ -23,7 +24,7 @@ version: **v0.15.0** | - [*class* TestCase](#class-testcase) - [`TestCase::resetInstance()`](#testcaseresetinstance) - [`TestCase::newModel($classname)`](#testcasenewmodelclassname) - - [`TestCase::newLibrary($classname)`](#testcasenewlibraryclassname) + - [`TestCase::newLibrary($classname, $args)`](#testcasenewlibraryclassname-args) - [`TestCase::newController($classname)`](#testcasenewcontrollerclassname) - [`TestCase::request($method, $argv, $params = [])`](#testcaserequestmethod-argv-params--) - [`request->setHeader()`](#request-setheader) @@ -164,11 +165,12 @@ public function setUp() } ~~~ -#### `TestCase::newLibrary($classname)` +#### `TestCase::newLibrary($classname, $args)` | param | type | description | |-------------|--------------|------------------------------------------| |`$classname` | string | library classname | +|`$args` | array | constructor argments | `returns` library object From 1ca97a4b9e3d51e11e64de7df9788c6f2e83650a Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Mar 2018 12:39:37 +0900 Subject: [PATCH 3/5] docs: add UnitTestCase and move methods --- docs/FunctionAndClassReference.md | 103 +++++++++++++++--------------- 1 file changed, 52 insertions(+), 51 deletions(-) diff --git a/docs/FunctionAndClassReference.md b/docs/FunctionAndClassReference.md index 5eac6760..09cc50ec 100644 --- a/docs/FunctionAndClassReference.md +++ b/docs/FunctionAndClassReference.md @@ -148,56 +148,6 @@ When you use the way, you use the same CodeIgniter instance and the same `Catego 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::newModel($classname)` - -| param | type | description | -|-------------|--------------|------------------------------------------| -|`$classname` | string | model classname | - -`returns` model object - -Resets CodeIgniter instance and return new model instance. This method is for model unit testing. - -~~~php -public function setUp() -{ - $this->obj = $this->newModel('Category_model'); -} -~~~ - -#### `TestCase::newLibrary($classname, $args)` - -| param | type | description | -|-------------|--------------|------------------------------------------| -|`$classname` | string | library classname | -|`$args` | array | constructor argments | - -`returns` library object - -Resets CodeIgniter instance and return new library instance. This method is for library unit testing. - -~~~php -public function setUp() -{ - $this->obj = $this->newLibrary('Foo_library'); -} -~~~ - -#### `TestCase::newController($classname)` - -| param | type | description | -|-------------|--------------|-----------------------------------------------| -|`$classname` | string | controller classname | - -`returns` controller object - -Resets CodeIgniter instance and return new controller instance. This method is for controller unit testing. - -~~~php -$controller = $this->newController('Some_controller'); -$actual = $controller->some_method(); -~~~ - #### `TestCase::request($method, $argv, $params = [])` | param | type | description | @@ -472,7 +422,6 @@ $mock = $this->getDouble('CI_Email', [ ]); ~~~ - **Upgrade Note for v0.10.0** v0.10.0 has changed the default behavior of `$this->getDouble()` and disabled original constructor. If the change causes errors, update your test code like below: @@ -684,6 +633,58 @@ Inserts a row into to the database. This row will be removed after the test has Fetches a single column from a database row with criteria matching `$where`. +### *class* UnitTestCase + +#### `UnitTestCase::newModel($classname)` + +| param | type | description | +|-------------|--------------|------------------------------------------| +|`$classname` | string | model classname | + +`returns` model object + +Resets CodeIgniter instance and return new model instance. This method is for model unit testing. + +~~~php +public function setUp() +{ + $this->obj = $this->newModel('Category_model'); +} +~~~ + +#### `UnitTestCase::newLibrary($classname, $args)` + +| param | type | description | +|-------------|--------------|------------------------------------------| +|`$classname` | string | library classname | +|`$args` | array | constructor argments | + +`returns` library object + +Resets CodeIgniter instance and return new library instance. This method is for library unit testing. + +~~~php +public function setUp() +{ + $this->obj = $this->newLibrary('Foo_library'); +} +~~~ + +#### `UnitTestCase::newController($classname)` + +| param | type | description | +|-------------|--------------|-----------------------------------------------| +|`$classname` | string | controller classname | + +`returns` controller object + +Resets CodeIgniter instance and return new controller instance. This method is for controller unit testing. + +~~~php +$controller = $this->newController('Some_controller'); +$actual = $controller->some_method(); +~~~ + ### *class* ReflectionHelper This class provides helper methods to access private or protected properties and methods. From 82cd098cbd187d7315aa8d33d942df1ae055ea81 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Mar 2018 12:44:50 +0900 Subject: [PATCH 4/5] docs: update TOC --- docs/FunctionAndClassReference.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/FunctionAndClassReference.md b/docs/FunctionAndClassReference.md index 09cc50ec..f2888e2b 100644 --- a/docs/FunctionAndClassReference.md +++ b/docs/FunctionAndClassReference.md @@ -23,9 +23,6 @@ version: **v0.16.0** | - [*function* `load_class_instance($classname, $instance)`](#function-load_class_instanceclassname-instance) - [*class* TestCase](#class-testcase) - [`TestCase::resetInstance()`](#testcaseresetinstance) - - [`TestCase::newModel($classname)`](#testcasenewmodelclassname) - - [`TestCase::newLibrary($classname, $args)`](#testcasenewlibraryclassname-args) - - [`TestCase::newController($classname)`](#testcasenewcontrollerclassname) - [`TestCase::request($method, $argv, $params = [])`](#testcaserequestmethod-argv-params--) - [`request->setHeader()`](#request-setheader) - [`request->setFiles($files)`](#request-setfilesfiles) @@ -52,6 +49,10 @@ version: **v0.16.0** | - [`DbTestCase::seeNumRecords($expected, $table, $where = [])`](#dbtestcaseseenumrecordsexpected-table-where--) - [`DbTestCase::hasInDatabase($table, $data)`](#dbtestcasehasindatabasetable-data) - [`DbTestCase::grabFromDatabase($table, $column, $where)`](#dbtestcasegrabfromdatabasetable-column-where) +- [*class* UnitTestCase](#class-unittestcase) + - [`UnitTestCase::newModel($classname)`](#unittestcasenewmodelclassname) + - [`UnitTestCase::newLibrary($classname, $args)`](#unittestcasenewlibraryclassname-args) + - [`UnitTestCase::newController($classname)`](#unittestcasenewcontrollerclassname) - [*class* ReflectionHelper](#class-reflectionhelper) - [`ReflectionHelper::getPrivateProperty($obj, $property)`](#reflectionhelpergetprivatepropertyobj-property) - [`ReflectionHelper::setPrivateProperty($obj, $property, $value)`](#reflectionhelpersetprivatepropertyobj-property-value) From f402f870765a937b7caf9f5994d7f7ee6b011d08 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sun, 18 Mar 2018 12:50:23 +0900 Subject: [PATCH 5/5] docs: add Upgrade Note --- application/tests/_ci_phpunit_test/ChangeLog.md | 4 ++++ docs/FunctionAndClassReference.md | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/application/tests/_ci_phpunit_test/ChangeLog.md b/application/tests/_ci_phpunit_test/ChangeLog.md index c78ec1ad..15a6231e 100644 --- a/application/tests/_ci_phpunit_test/ChangeLog.md +++ b/application/tests/_ci_phpunit_test/ChangeLog.md @@ -2,6 +2,10 @@ ## v0.16.0 (Not Released) +### Upgrade Note + +* If you use `$this->newModel()`, `$this->newLibrary()`, `$this->newController()` in your test cases, please install `tests/UnitTestCase.php` manually, and change the base classname of the test cases to `UnitTestCase` class. + ### Added * Now you can pass more than 5 arguments to `$this->verifyInvoked*()`. See [#192](https://github.com/kenjis/ci-phpunit-test/pull/192). diff --git a/docs/FunctionAndClassReference.md b/docs/FunctionAndClassReference.md index f2888e2b..12b08fd2 100644 --- a/docs/FunctionAndClassReference.md +++ b/docs/FunctionAndClassReference.md @@ -636,6 +636,10 @@ Fetches a single column from a database row with criteria matching `$where`. ### *class* UnitTestCase +**Upgrade Note for v0.16.0** + +To use this test case, you must install `application/tests/UnitTestCase.php` manually. + #### `UnitTestCase::newModel($classname)` | param | type | description |