Skip to content

Commit

Permalink
Merge pull request #233 from kenjis/add-unit-test-case-class
Browse files Browse the repository at this point in the history
Add UnitTestCase class
  • Loading branch information
kenjis committed Mar 18, 2018
2 parents ac85d37 + f402f87 commit 3c4aa17
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 110 deletions.
5 changes: 5 additions & 0 deletions application/tests/UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

class UnitTestCase extends CIPHPUnitTestUnitTestCase
{
}
6 changes: 6 additions & 0 deletions application/tests/_ci_phpunit_test/CIPHPUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ public static function init(array $autoload_dirs = null)
require $db_test_case_file;
}

$unit_test_case_file = TESTPATH . 'UnitTestCase.php';
if (is_readable($unit_test_case_file))
{
require $unit_test_case_file;
}

// Replace a few Common functions
require __DIR__ . '/replacing/core/Common.php';
require BASEPATH . 'core/Common.php';
Expand Down
56 changes: 0 additions & 56 deletions application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,62 +81,6 @@ public function resetInstance()
$this->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))
Expand Down
68 changes: 68 additions & 0 deletions application/tests/_ci_phpunit_test/CIPHPUnitTestUnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Part of ci-phpunit-test
*
* @author Kenji Suzuki <https://github.com/kenjis>
* @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;
}
}
4 changes: 4 additions & 0 deletions application/tests/_ci_phpunit_test/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
116 changes: 62 additions & 54 deletions docs/FunctionAndClassReference.md
Original file line number Diff line number Diff line change
@@ -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) |
Expand All @@ -22,9 +23,6 @@ version: **v0.15.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)`](#testcasenewlibraryclassname)
- [`TestCase::newController($classname)`](#testcasenewcontrollerclassname)
- [`TestCase::request($method, $argv, $params = [])`](#testcaserequestmethod-argv-params--)
- [`request->setHeader()`](#request-setheader)
- [`request->setFiles($files)`](#request-setfilesfiles)
Expand All @@ -51,6 +49,10 @@ version: **v0.15.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)
Expand Down Expand Up @@ -147,55 +149,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)`

| param | type | description |
|-------------|--------------|------------------------------------------|
|`$classname` | string | library classname |

`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 |
Expand Down Expand Up @@ -470,7 +423,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:
Expand Down Expand Up @@ -682,6 +634,62 @@ 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

**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 |
|-------------|--------------|------------------------------------------|
|`$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.
Expand Down

0 comments on commit 3c4aa17

Please sign in to comment.