You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/HowToWriteTests.md
+23-23Lines changed: 23 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -167,7 +167,7 @@ See [Controller with Hooks](#controller-with-hooks) for details.
167
167
168
168
*CI PHPUnit Test* has an autoloader for class files.
169
169
170
-
To change the search paths, change the line [`CIPHPUnitTest::init();`](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.0/application/tests/Bootstrap.php#L336) in `tests/Bootstrap.php` like below:
170
+
To change the search paths, change the line [`CIPHPUnitTest::init();`](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.1/application/tests/Bootstrap.php#L336) in `tests/Bootstrap.php` like below:
171
171
172
172
~~~php
173
173
CIPHPUnitTest::init([
@@ -251,7 +251,7 @@ class Inventory_model_test extends TestCase
251
251
}
252
252
~~~
253
253
254
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/models/Category_model_test.php).
254
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/models/Category_model_test.php).
255
255
256
256
#### Database Seeding
257
257
@@ -272,7 +272,7 @@ You can use them like below:
272
272
}
273
273
~~~
274
274
275
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/models/Category_model_test.php).
275
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/models/Category_model_test.php).
276
276
277
277
#### Using PHPUnit Mock Objects
278
278
@@ -337,7 +337,7 @@ If you don't know well about PHPUnit Mock Objects, see [Test Doubles](https://ph
337
337
}
338
338
~~~
339
339
340
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/models/Category_model_mocking_db_test.php).
340
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/models/Category_model_mocking_db_test.php).
341
341
342
342
### Libraries
343
343
@@ -379,7 +379,7 @@ You can use [$this->request()](FunctionAndClassReference.md#testcaserequestmetho
379
379
380
380
**Note:** If you pass URI string to the 2nd argument of `$this->request()`, it invokes the routing. If the resolved controller has `_remap()` and/or `_output()` methods, they will be invoked, too.
381
381
382
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/sub/Sub_test.php).
382
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/sub/Sub_test.php).
383
383
384
384
If you want to call a controller method directly, you can pass an array to the 2nd argument of `$this->request()`.
385
385
@@ -399,7 +399,7 @@ class Welcome_test extends TestCase
399
399
400
400
**Note:** If you pass an array to the 2nd argument of `$this->request()`, it does not invokes the routing. The `_remap()` and/or `_output()` methods in a controller are not invoked, too.
401
401
402
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Welcome_test.php).
402
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Welcome_test.php).
403
403
404
404
#### REST Request
405
405
@@ -437,7 +437,7 @@ You can set request header with [$this->request->setHeader()](FunctionAndClassRe
437
437
}
438
438
~~~
439
439
440
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/api/Example_test.php).
440
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/api/Example_test.php).
441
441
442
442
#### Ajax Request
443
443
@@ -452,7 +452,7 @@ You can use [$this->ajaxRequest()](FunctionAndClassReference.md#testcaseajaxrequ
452
452
}
453
453
~~~
454
454
455
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Ajax_test.php).
455
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Ajax_test.php).
456
456
457
457
#### Request and Use Mocks
458
458
@@ -482,7 +482,7 @@ You can use [$this->request->setCallable()](FunctionAndClassReference.md#request
482
482
483
483
**Note:** When you have not loaded a class with CodeIgniter loader, if you make a mock object for the class, your application code may not work correclty. If you have got an error, please try to load it with CodeIgniter loader, before getting the mock object.
484
484
485
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Mock_phpunit_test.php).
485
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Mock_phpunit_test.php).
486
486
487
487
The function you set by `$this->request->setCallable()` runs after controller instantiation. So you can't inject mocks into controller constructor.
488
488
@@ -532,7 +532,7 @@ In this case, You can use [$this->request->setCallablePreConstructor()](Function
532
532
}
533
533
~~~
534
534
535
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Auth_check_in_construct_test.php).
535
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Auth_check_in_construct_test.php).
536
536
537
537
**Note:** If you can't create mocks or it is too hard to create mocks, it may be better to use Monkey Patching.
538
538
@@ -596,7 +596,7 @@ I recommend using PHPUnit mock objects. [$this->getDouble()](FunctionAndClassRef
596
596
}
597
597
~~~
598
598
599
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Auth_test.php).
599
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Auth_test.php).
600
600
601
601
#### `redirect()`
602
602
@@ -616,7 +616,7 @@ If you use `redirect()` in *CI PHPUnit Test*, you can write tests like this:
616
616
617
617
[$this->assertRedirect()](FunctionAndClassReference.md#testcaseassertredirecturi-code--null) is a method in *CI PHPUnit Test*.
618
618
619
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Redirect_test.php).
619
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Redirect_test.php).
620
620
621
621
##### Upgrade Note for v0.4.0
622
622
@@ -658,7 +658,7 @@ You can use [$this->assertResponseCode()](FunctionAndClassReference.md#testcasea
658
658
}
659
659
~~~
660
660
661
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Nocontroller_test.php).
661
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Nocontroller_test.php).
662
662
663
663
If you don't call `$this->request()` in your tests, `show_error()` throws `CIPHPUnitTestShowErrorException` and `show_404()` throws `CIPHPUnitTestShow404Exception`. So you must expect the exceptions. You can use `@expectedException` annotation in PHPUnit.
664
664
@@ -708,7 +708,7 @@ If you want to enable hooks, call [$this->request->enableHooks()](FunctionAndCla
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Hook_test.php).
711
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Hook_test.php).
712
712
713
713
#### Controller with Name Collision
714
714
@@ -734,7 +734,7 @@ class sub_Welcome_test extends TestCase
734
734
}
735
735
~~~
736
736
737
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/sub/Welcome_test.php).
737
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/sub/Welcome_test.php).
738
738
739
739
### Mock Libraries
740
740
@@ -870,15 +870,15 @@ A test case could be like this:
870
870
}
871
871
~~~
872
872
873
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Exit_to_exception_test.php).
873
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Exit_to_exception_test.php).
874
874
875
875
#### Patching Functions
876
876
877
877
This patcher allows replacement of global functions that can't be mocked by PHPUnit.
878
878
879
879
But it has a few limitations. Some functions can't be replaced and it might cause errors.
880
880
881
-
So by default we can replace only a dozen pre-defined functions in [FunctionPatcher](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.0/application/tests/_ci_phpunit_test/patcher/Patcher/FunctionPatcher.php#L27).
881
+
So by default we can replace only a dozen pre-defined functions in [FunctionPatcher](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.1/application/tests/_ci_phpunit_test/patcher/Patcher/FunctionPatcher.php#L27).
882
882
883
883
~~~php
884
884
public function test_index()
@@ -891,7 +891,7 @@ So by default we can replace only a dozen pre-defined functions in [FunctionPatc
891
891
892
892
[MonkeyPatch::patchFunction()](FunctionAndClassReference.md#monkeypatchpatchfunctionfunction-return_value-class_method) replaces PHP native function `mt_rand()` in `Welcome::index` method, and it will return `100` in the test method.
893
893
894
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Patching_on_function_test.php).
894
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Patching_on_function_test.php).
895
895
896
896
**Note:** If you call `MonkeyPatch::patchFunction()` without 3rd argument, all the functions (located in `include_paths` and not in `exclude_paths`) called in the test method will be replaced. So, for example, a function in CodeIgniter code might be replaced and it results in unexpected outcome.
897
897
@@ -924,11 +924,11 @@ You could change return value of patched function using PHP closure:
924
924
);
925
925
~~~
926
926
927
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Patching_on_function_test.php#L59-L80).
927
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Patching_on_function_test.php#L59-L80).
928
928
929
929
**Patch on Other Functions**
930
930
931
-
If you want to patch other functions, you can add them to [functions_to_patch](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.0/application/tests/Bootstrap.php#L323) in `MonkeyPatchManager::init()`.
931
+
If you want to patch other functions, you can add them to [functions_to_patch](https://github.com/kenjis/ci-phpunit-test/blob/v0.11.1/application/tests/Bootstrap.php#L323) in `MonkeyPatchManager::init()`.
932
932
933
933
But there are a few known limitations:
934
934
@@ -953,13 +953,13 @@ This patcher allows replacement of methods in user-defined classes.
953
953
954
954
[MonkeyPatch::patchMethod()](FunctionAndClassReference.md#monkeypatchpatchmethodclassname-params) replaces `get_category_list()` method in `Category_model`, and it will return `[(object) ['name' => 'Nothing']]` in the test method.
955
955
956
-
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.0/application/tests/controllers/Patching_on_method_test.php).
956
+
See [working sample](https://github.com/kenjis/ci-app-for-ci-phpunit-test/blob/v0.11.1/application/tests/controllers/Patching_on_method_test.php).
0 commit comments