kenjis / ci-phpunit-test Goto Github PK
View Code? Open in Web Editor NEWAn easier way to use PHPUnit with CodeIgniter 3.x.
Home Page: http://kenjis.github.io/ci-phpunit-test/
License: MIT License
An easier way to use PHPUnit with CodeIgniter 3.x.
Home Page: http://kenjis.github.io/ci-phpunit-test/
License: MIT License
Hi,
I have an issue: I have a controller name which is used in a library I use. The CIPHPUNIT autoload feature loads libraries first to search for a class. I think Controller and models are more important than libraries which can be third party code.
I resolved it putting libraries scope below the others in this file CIPHPUnitTestAutoloader.php:71
protected function loadApplicationClass($class)
{
$dirs = [
APPPATH.'controllers',
APPPATH.'models',
APPPATH.'libraries',
];
I want to write tests for routes.php. But my test was failed.
class Welcome_test extends TestCase
{
protected $CI;
protected function setUp()
{
$this->CI =& get_instance();
}
public function tearDown()
{
// Reset CI object for next test case, unless property won't work
reset_instance();
new CI_Controller();
}
public function test_index()
{
$output = $this->request('GET', ['Welcome', 'index']);
$this->assertEquals('Welcome/index', $this->CI->uri->ruri_string());
}
}
There was 1 failure:
1) Welcome_test::test_index
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'Welcome/index'
+'_dummy/_dummy'
I did well like below that I called the get_instance
after the request
method.
Is this best solution?
public function test_index()
{
$output = $this->request('GET', ['Welcome', 'index']);
$this->CI =& get_instance();
$this->assertEquals('Welcome/index', $this->CI->uri->ruri_string());
}
I have some supermethods in the MY_Controller class.
For example the getCurrentAppID()
which returns the application ID the user is working on. I want to Mock that method to make it return 218 for example.
How to do that ? I tried to double the MY_Controller class but unsuccessfully.
If you read my issue about the exit function, you already know my context. I have a isLoggedIn method in the controller constructor. I want to mock it to make it return true to test the method underneath.
THE problem is that the Mock functions are called AFTER this controller initialization.
Here is my test code:
public function test_index_logged_in() { $this->request->setCallable( function ($CI) { // Get mock object $auth = $this->getDouble( 'Tank_auth', ['is_logged_in' => TRUE] ); // Inject mock object $CI->tank_auth = $auth; } ); $output = $this->request('GET', ['Main', 'index']); $this->assertContains('result OK', $output); }
But the Main controller extends MY_Controller which contains a is_logged_in in its constructor. So when I call the $this->request method, here is the stack trace:
$this->request('GET', ['Main', 'index']); CIPHPUnitTestCase.php:106 CIPHPUnitTestRequest.php:120 CIPHPUnitTestRequest.php:200 CIPHPUnitTestRequest.php:277 Main.php // Here is my code, it extends M_Controller then calls its constructor
And finally, here is the MY_Controller constructor:
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
$this->output->append_output($this->load->view('auth/timeOutV2', $this->timeOut(), true));
echo $this->output->get_output();
exit;
}
}
}
Hello,
Is it possible to handle the exit; function ?
I have to call it when the user is not loggedin to abort a controller execution. Here is my simplified code:
class MY_Controller extends CI_Controller {
function __construct() {
parent::__construct();
if (!$this->tank_auth->is_logged_in()) {
$this->output->append_output($this->load->view('auth/timeOutV2', $this->timeOut(), true));
echo $this->output->get_output();
exit;
}
}
}
This Issue is only to know if its possible to handle the exit php instruction as it brutally stops the phpunit process.
I think it's not possible but I prefer to ask you first.
I have the following error during controller test execution :
<p>Message: ini_set(): A session is active. You cannot change the session module's ini settings at this time</p>
<p>Filename: Session/Session.php:313</p>
Does anyone know how to get PHPUnit working with PHPStorm and GAE?
I have a custom language loading using a post_controller_constructor hook :
$hook['post_controller_constructor'] = array( 'class' => 'LanguageLoader', 'function' => 'initialize', 'filename' => 'LanguageLoader.php', 'filepath' => 'hooks' );
I then decided to use the commit you did yesterday but you added a return before all the hook calls, you can't abort the bootstrap like this, all the hooks are bypassed !
therefore, all my lang() calls fails.
PS: Just before upgrading my code to your last commit, the new CI_Controller()
function call was resetting the language arrays because it rebuilds the CI Single Instance. self::$instance =& $this;
My hack seems to work. Here is my CIPHPUnitTest file:
try { // Request to 404 route // This is needed for not to call Welcome::index() // If controller Welcome is called in bootstrap, we can't test // the same name sub controller Welcome even when we use // `@runInSeparateProcess` and `@preserveGlobalState disabled` require_once BASEPATH . 'core/CodeIgniter.php'; } catch (CIPHPUnitTestShow404Exception $e) { // Catch 404 exception //new CI_Controller(); COMMENTED BY NTH Because it resets the post_controllers hooks }
Sorry for writing this message while debugging your code. I just found that you rebootstrap Codeigniter when calling the the $this->request(...)
method. My problem is that $this->enableHooks
Hi!, I installed phpunit test, I followed the instructions but I got this error.
felipe@Lenovo-Y50-70:/application/tests$ ../../vendor/bin/phpunit --debug
PHP Fatal error: Call to a member function item() on null in /system/core/Utf8.php on line 48
PHP Stack trace:
PHP 1. {main}() /vendor/phpunit/phpunit/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /vendor/phpunit/phpunit/phpunit:36
PHP 3. PHPUnit_TextUI_Command->run() /vendor/phpunit/phpunit/src/TextUI/Command.php:101
PHP 4. PHPUnit_TextUI_Command->handleArguments() /vendor/phpunit/phpunit/src/TextUI/Command.php:111
PHP 5. PHPUnit_TextUI_Command->handleBootstrap() /vendor/phpunit/phpunit/src/TextUI/Command.php:590
PHP 6. PHPUnit_Util_Fileloader::checkAndLoad() /vendor/phpunit/phpunit/src/TextUI/Command.php:755
PHP 7. PHPUnit_Util_Fileloader::load() /vendor/phpunit/phpunit/src/Util/Fileloader.php:36
PHP 8. include_once() /vendor/phpunit/phpunit/src/Util/Fileloader.php:52
PHP 9. CIPHPUnitTest::init() /application/tests/Bootstrap.php:296
PHP 10. require_once() /application/tests/_ci_phpunit_test/CIPHPUnitTest.php:62
PHP 11. load_class() /system/core/CodeIgniter.php:159
PHP 12. CI_Utf8->__construct() /application/tests/_ci_phpunit_test/replacing/core/Common.php:98
Fatal error: Call to a member function item() on null in /system/core/Utf8.php on line 48
Call Stack:
0.0001 235664 1. {main}() /vendor/phpunit/phpunit/phpunit:0
0.0061 866368 2. PHPUnit_TextUI_Command::main() /vendor/phpunit/phpunit/phpunit:36
0.0061 866992 3. PHPUnit_TextUI_Command->run() /vendor/phpunit/phpunit/src/TextUI/Command.php:101
0.0061 869712 4. PHPUnit_TextUI_Command->handleArguments() /vendor/phpunit/phpunit/src/TextUI/Command.php:111
0.0081 1289032 5. PHPUnit_TextUI_Command->handleBootstrap() /vendor/phpunit/phpunit/src/TextUI/Command.php:590
0.0082 1297344 6. PHPUnit_Util_Fileloader::checkAndLoad() /vendor/phpunit/phpunit/src/TextUI/Command.php:755
0.0082 1297544 7. PHPUnit_Util_Fileloader::load() /vendor/phpunit/phpunit/src/Util/Fileloader.php:36
0.0083 1323600 8. include_once('/application/tests/Bootstrap.php') /vendor/phpunit/phpunit/src/Util/Fileloader.php:52
0.0084 1337648 9. CIPHPUnitTest::init() /application/tests/Bootstrap.php:296
0.0102 1737704 10. require_once('/system/core/CodeIgniter.php') /application/tests/_ci_phpunit_test/CIPHPUnitTest.php:62
0.0113 1874528 11. load_class() /system/core/CodeIgniter.php:159
0.0114 1892072 12. CI_Utf8->__construct() /application/tests/_ci_phpunit_test/replacing/core/Common.php:98
Hello,
I am having problems setting up ci-phpunit-test with PHPUnit 4.7 and CodeIgniter 3. I have gone through the documentation here and on the PHPUnit site to ensure I haven't missed any setup steps, but maybe that is all that is happening here. When I run a specific test case file with phpunit, I receive the following error message:
vagrant@vagrant-ubuntu-trusty-64:/var/www/html/application/tests/models/User$ phpunit User_model_test.php
/*
** Unit tests for the User Model.
**
**
*/
PHP Fatal error: Class 'TestCase' not found in /var/www/html/application/tests/models/User/User_model_test.php on line 11
PHP Stack trace:
PHP 1. {main}() /usr/local/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/local/bin/phpunit:537
PHP 3. PHPUnit_TextUI_Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:99
PHP 4. PHPUnit_Runner_BaseTestRunner->getTest() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:121
PHP 5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php:66
PHP 6. PHPUnit_Runner_StandardTestSuiteLoader->load() phar:///usr/local/bin/phpunit/phpunit/Runner/BaseTestRunner.php:121
PHP 7. PHPUnit_Util_Fileloader::checkAndLoad() phar:///usr/local/bin/phpunit/phpunit/Runner/StandardTestSuiteLoader.php:37
PHP 8. PHPUnit_Util_Fileloader::load() phar:///usr/local/bin/phpunit/phpunit/Util/Fileloader.php:36
PHP 9. include_once() phar:///usr/local/bin/phpunit/phpunit/Util/Fileloader.php:52
This clears up if I add "include '../TestCase.php';" but then it can't find another class, and so on down the rabbit hole.
The code below is a meaningless test as I am trying to get phpunit to run and be able to find the necessary classes for core functionality.
<?php
// include '../TestCase.php';
class User_model_test extends TestCase
{
public function setUp()
{
$this->CI =& get_instance();
$this->load->model('User_model');
$this->obj = $this->CI->User_model;
}
public function testSignUp()
{
$actual = $this->obj->signUp();
$this->assertTrue($actual);
}
}
?>
Please let me know if I can provide additional relevant details. My PHP knowledge is weak, but I am experienced with JUnit.
Thank You!
We have to implement support hooks for unit tests.
I have problem with auto load form_validation.
Severity: ErrorMessage: Call to a member function helper() on a non-object
Filename: /var/www/ci-phpunit-test/system/libraries/Form_validation.php
Line Number: 147
Most of the code in this class already exists in the parent class.
After I do:
$ cd /path/to/codeigniter/
$ cd application/tests/
$ phpunit
I'm getting this error: Unable to locate the specified class: Session.php-sh-4.1
In application, session is configured to autoload as a library ($autoload['libraries'] = array('database', 'session',... ).
Will be appreciated if you help me to configure application correctly!
Hi
Sorry for my stupid question.
How mock REST_Controller _detect_api_key method to return true.
Thanks
I am using this command:
phpunit --bootstrap Bootstrap.php controllers/Welcome_test.php
And show me this:
PHPUnit 4.1.6 by Sebastian Bergmann.
how can I run the tests correctly ?
I see duplicates codes in CIPHPUnitTest.php, it's correct?
@kejins, how about this?
Hi Kenjis,
I have a proposal : To be able to call many times the $this->request->setCallable()
method.
My goal is to add many mocks, at differents moments during the testcase.
What do you think about this?
I'm using an library(https://github.com/chriskacerguis/codeigniter-restserver/) for create restfull application, but that not working with ci-phpunit-test.
Hi.
The codeigniter-rest-server not accept the GET parameter from array (['name' => 'Mike'])
$output = $this->request('GET', 'query_string/index', ['name' => 'Mike']);
because rest-server don't use $_GET variable - if i saw good.
and
$output = $this->request('GET', 'query_string/index?name=Mike');
not work.
$output will be NULL
Sorry my bad English
Thanks.
I think that dev-master
is not good versioning.
I propose to give a version number for each a bunch of commits.
Is it possible to override CI config values within tests?
E.g. A controller has below code.
$someValue = $this->config->item("xyz");
Can I override "xyz"'s value inside controller test?
Thank you.
Hi,
My project is restful api style. So, my controller method ended with like below code:
$this->output
->set_status_header(200)
->set_content_type('application/json', 'utf-8')
->set_output($output)
->_display();
exit;
I wrote a test for this method, but it did not run correctly.
I found the test stopped at "call_user_func_array" in CIPHPUnitTestRequest/createAndCallController.
Now, i don't know how to go on.
Please help me, thanks advanced.
I followed your instructions for version v0.8.2 and tried to run unit tests on a fresh codeigniter 3.0.1 installation with:
$ cd application/tests/
$ phpunit
I got this error:
PHP Fatal error: Call to undefined function get_instance() in /media/sf_deals/application/tests/_ci_phpunit_test/replacing/core/Common.php on line 259
The function get_instance() is defined in application\tests_ci_phpunit_test\replacing\core\CodeIgniter.php.
In application\tests_ci_phpunit_test\CIPHPUnitTest.php the file Common.php (line 36) is loaded before the file CodeIgniter.php (line 59).
I'm wondering, why this works for you.
HI,
I'm having trouble getting a controller test to run.
The message is :
Unable to locate the specified class: Session.php
The line from my autoload.php is :
$autoload['libraries'] = array('database', 'session', 'PERM/Perm_hrpws');
The Session library is in "system/libraries/Session/Session.php"
I also had a use library at "application/libraries/PERM/Perm_ws.php"
, If I comment out 'session'
The message is :
Unable to locate the specified class: Perm_ws.php
If I comment both out I don't get the message above, but get the error:
Fatal error: Call to a member function flashdata() on null
Any thoughts on what I'm doing wrong?
I tried both v0.8.2 and 1.0x-dev builds
thanks
Jim
Q | A |
---|---|
version | 0.6.2 and before / dev-master (2015-09-02) and before |
If you use monkey patching and you have a class named *test.php
, you get Fatal error: Cannot redeclare class *test
.
Hi when I try these codes for testing my model.
$db = $this->getMockBuilder('CI_DB_pdo_sqlite_driver')
->disableOriginalConstructor()
->getMock();
There is an error saying PHP Fatal error: Class 'CI_DB' not found
. Any idea why is this happening?
from: kenjis/ci-app-for-ci-phpunit-test@efcebe2#commitcomment-13151224
hi sir kenji my name is donal,
I was integrate your phpunit to mycodeigniter 3.0 and it run very well. but when i load database in my controller. and when i test the controller with syntax phpunit from my terminal i have an error like below.
cbn-dev@cbndev-ThinkPad-E420:/opt/lampp/htdocs/ciunit/application/tests$ phpunit
PHP Fatal error: Call to undefined function mysqli_init() in /opt/lampp/htdocs/ciunit/system/database/drivers/mysqli/mysqli_driver.php on line 125
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:138
PHP 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:657
PHP 6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:789
PHP 7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:873
PHP 8. PHPUnit_Framework_TestSuite->addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:416
PHP 9. PHPUnit_Framework_TestSuite->addTestSuite() /usr/share/php/PHPUnit/Framework/TestSuite.php:389
PHP 10. PHPUnit_Framework_TestSuite->__construct() /usr/share/php/PHPUnit/Framework/TestSuite.php:315
PHP 11. PHPUnit_Framework_TestSuite->addTestMethod() /usr/share/php/PHPUnit/Framework/TestSuite.php:212
PHP 12. PHPUnit_Framework_TestSuite::createTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:834
PHP 13. Welcome_test->__construct() /usr/share/php/PHPUnit/Framework/TestSuite.php:473
PHP 14. CI_Loader->model() /opt/lampp/htdocs/ciunit/application/tests/controllers/Welcome_test.php:19
PHP 15. Test->__construct() /opt/lampp/htdocs/ciunit/application/tests/_ci_phpunit_test/replacing/core/Loader.php:325
PHP 16. CI_Loader->database() /opt/lampp/htdocs/ciunit/application/models/Test.php:11
PHP 17. DB() /opt/lampp/htdocs/ciunit/application/tests/_ci_phpunit_test/replacing/core/Loader.php:365
PHP 18. CI_DB_driver->initialize() /opt/lampp/htdocs/ciunit/system/database/DB.php:216
PHP 19. CI_DB_mysqli_driver->db_connect() /opt/lampp/htdocs/ciunit/system/database/DB_driver.php:401
Fatal error: Call to undefined function mysqli_init() in /opt/lampp/htdocs/ciunit/system/database/drivers/mysqli/mysqli_driver.php on line 125
Call Stack:
0.0003 233320 1. {main}() /usr/bin/phpunit:0
0.0062 523312 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
0.0062 523936 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
0.0062 526344 4. PHPUnit_TextUI_Command->handleArguments() /usr/share/php/PHPUnit/TextUI/Command.php:138
0.0507 3226248 5. PHPUnit_Util_Configuration->getTestSuiteConfiguration() /usr/share/php/PHPUnit/TextUI/Command.php:657
0.0508 3227480 6. PHPUnit_Util_Configuration->getTestSuite() /usr/share/php/PHPUnit/Util/Configuration.php:789
0.0821 3419240 7. PHPUnit_Framework_TestSuite->addTestFiles() /usr/share/php/PHPUnit/Util/Configuration.php:873
0.0822 3420136 8. PHPUnit_Framework_TestSuite->addTestFile() /usr/share/php/PHPUnit/Framework/TestSuite.php:416
0.0841 3619168 9. PHPUnit_Framework_TestSuite->addTestSuite() /usr/share/php/PHPUnit/Framework/TestSuite.php:389
0.0841 3619384 10. PHPUnit_Framework_TestSuite->__construct() /usr/share/php/PHPUnit/Framework/TestSuite.php:315
0.0844 3721064 11. PHPUnit_Framework_TestSuite->addTestMethod() /usr/share/php/PHPUnit/Framework/TestSuite.php:212
0.0844 3721976 12. PHPUnit_Framework_TestSuite::createTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:834
0.0857 3818280 13. Welcome_test->__construct() /usr/share/php/PHPUnit/Framework/TestSuite.php:473
0.0857 3818696 14. CI_Loader->model() /opt/lampp/htdocs/ciunit/application/tests/controllers/Welcome_test.php:19
0.0862 3827264 15. Test->__construct() /opt/lampp/htdocs/ciunit/application/tests/_ci_phpunit_test/replacing/core/Loader.php:325
0.0862 3827800 16. CI_Loader->database() /opt/lampp/htdocs/ciunit/application/models/Test.php:11
0.0866 3859616 17. DB() /opt/lampp/htdocs/ciunit/application/tests/_ci_phpunit_test/replacing/core/Loader.php:365
0.0937 4704456 18. CI_DB_driver->initialize() /opt/lampp/htdocs/ciunit/system/database/DB.php:216
0.0937 4705160 19. CI_DB_mysqli_driver->db_connect() /opt/lampp/htdocs/ciunit/system/database/DB_driver.php:401
A PHP Error was encountered
Severity: Error
Message: Call to undefined function mysqli_init()
Filename: /opt/lampp/htdocs/ciunit/system/database/drivers/mysqli/mysqli_driver.php
Line Number: 125
Backtrace:
please help me?
where i get wrong to integrate the phpunit?
Hi,
I required "config_dict.php" in my "config.php" ,it used some constant;
CI will load "config/constants.php" first,and then load "config.php"
In unit test ,we will use it in "CIPHPUnitTest.php" line 101 : replcaeHelpers() ;
so,I got this message :
A PHP Error was encountered
Severity: NoticeMessage: Use of undefined constant "XXXX" - assumed 'XXXX'Filename: D:\XXX\apps\config\config_dict.phpLine Number: 7
Notice: Use of undefined constant XXXX - assumed 'XXXX' in D:\XXXX\apps\config\config_dict.php on line 8
config.php
require_once APPPATH.'config/config_dict.php';
config_dict.php
$config['sys.role_id'] = array (
XXXX => 'Admin',
);
Q | A |
---|---|
version | 1.0.x@dev (dev-master; after v0.5.0) |
PHP | PHP 5.5.9-1ubuntu4.11 (cli) (built: Jul 2 2015 15:17:32) |
OS | Ubuntu 14.04 |
The warnings like below appear.
PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
This error is related to monkey patching. If I turn it off, the errors will be gone.
And if I run tests again, the errors also will be gone. That is, if once patched source files are cached, the errors will be gone.
How to reproduce:
$ git clone https://github.com/kenjis/ci-app-for-ci-phpunit-test.git
$ cd ci-app-for-ci-phpunit-test
$ composer update
$ ./install.php
$ cd application/tests/
$ ../../vendor/bin/phpunit
Output:
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Warning: The Xdebug extension is not loaded
No code coverage will be generated.
..........................PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
...PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
....PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 87
................PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 87
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 87
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
.PHP Warning: Cannot modify header information - headers already sent by (output started at /home/kenji/tmp/ci-app-for-ci-phpunit-test/vendor/phpunit/phpunit/src/Util/Printer.php:133) in /home/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/_ci_phpunit_test/replacing/helpers/url_helper.php on line 84
............. 65 / 70 ( 92%)
.....
Time: 7.15 seconds, Memory: 19.25Mb
OK (70 tests, 101 assertions)
I've tried every possible solution i can think of, but nothing seems to work. I keep getting the CI_Lang class not found since its extended by MX/Lang.
I'd appreciate any guidelines to help understand how to debug the problem.
Move the ci-phpunit-test library files to directory third_party and rename for CIUnit.
codeigniter/
├── application/
│ └── tests/
│ ├── _ci_phpunit_test/ ... don't touch! files CI PHPUnit Test uses
│ ├── Bootstrap.php ... bootstrap file for PHPUnit
│ ├── TestCase.php ... TestCase class
│ ├── controllers/ ... put your controller tests
│ ├── mocks/
│ │ └── libraries/ ... mock libraries
│ ├── models/ ... put your model tests
│ └── phpunit.xml ... config file for PHPUnit
└── vendor/
codeigniter/
├── application/
│ ├── tests/
│ │ ├── controllers/ ... put your controller tests
│ │ ├── mocks/
│ │ │ └── libraries/ ... mock libraries
│ │ ├── models/ ... put your model tests
│ │ ├── libraries/ ... put your library tests
│ │ └── phpunit.xml ... config file for PHPUnit
│ └── third_party/
│ └── CIUnit
│ ├── _ci_phpunit_test/ ... don't touch! files CI PHPUnit Test uses
│ ├── Bootstrap.php ... bootstrap file for PHPUnit
│ └── TestCase.php ... TestCase class
└── vendor/
Hi @kenjis ,
I've followed https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md#request-and-use-mocks in order to make a request and mock the CI email library. Howver, I got the following error Call to undefined method Mock_CI_Email
Here's my test :
public function test_signup() {
$this->request->setCallable(
function ($CI) {
$email = $this->getDouble('CI_Email', ['send' => TRUE]);
$this->verifyInvokedOnce($email, 'send');
$CI->email = $email;
}
);
$output = $this->request(
'POST',
['Ajax', 'signup'],
[
'email' => '[email protected]',
'password' => 'password',
'name' => 'name',
'firstname' => 'firstname',
'timezone' => 'timezone',
'country' => 'country',
'mailingList' => 'false',
]
);
$this->assertContains('{"success":true}', $output);
}
The complete error is A PHP Error was encountered Severity: ErrorMessage: Call to undefined method Mock_CI_Email_fc780022::method()Filename: /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestDouble.phpLine Number: 45
when I run phpunit
The complete call stack is :
[11:28 math@math-HP tests] > phpunit
PHPUnit 3.7.28 by Sebastian Bergmann.
Configuration read from /var/www/tw/application/tests/phpunit.xml
.PHP Fatal error: Call to undefined method Mock_CI_Email_fc780022::method() in /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestDouble.php on line 45
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_TextUI_TestRunner->doRun() /usr/share/php/PHPUnit/TextUI/Command.php:176
PHP 5. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/TextUI/TestRunner.php:349
PHP 6. PHPUnit_Framework_TestSuite->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:705
PHP 7. PHPUnit_Framework_TestSuite->runTest() /usr/share/php/PHPUnit/Framework/TestSuite.php:745
PHP 8. PHPUnit_Framework_TestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:775
PHP 9. PHPUnit_Framework_TestResult->run() /usr/share/php/PHPUnit/Framework/TestCase.php:783
PHP 10. PHPUnit_Framework_TestCase->runBare() /usr/share/php/PHPUnit/Framework/TestResult.php:648
PHP 11. PHPUnit_Framework_TestCase->runTest() /usr/share/php/PHPUnit/Framework/TestCase.php:838
PHP 12. ReflectionMethod->invokeArgs() /usr/share/php/PHPUnit/Framework/TestCase.php:983
PHP 13. Ajax_test->test_signup() /usr/share/php/PHPUnit/Framework/TestCase.php:983
PHP 14. CIPHPUnitTestCase->request() /var/www/tw/application/tests/controllers/Ajax_test.php:52
PHP 15. CIPHPUnitTestRequest->request() /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:60
PHP 16. CIPHPUnitTestRequest->callControllerMethod() /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:83
PHP 17. CIPHPUnitTestRequest->createAndCallController() /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:187
PHP 18. Ajax_test->{closure:/var/www/tw/application/tests/controllers/Ajax_test.php:33-37}() /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestRequest.php:272
PHP 19. CIPHPUnitTestCase->getDouble() /var/www/tw/application/tests/controllers/Ajax_test.php:34
PHP 20. CIPHPUnitTestDouble->getDouble() /var/www/tw/application/tests/_ci_phpunit_test/CIPHPUnitTestCase.php:95
Am I missing something obvious ?
Thanks for your help and your library !
M.
Hi.
I have controller in 2 folder depth from controllers folder (ex. controllers/api/aa/Example.php). I make a request the following form
$this->request('GET', ['Example', 'users_get']);
i will get
Example_test::test_users_get
Status code is not 200 but 404.
Failed asserting that 404 is identical to 200.
If i move the controller one folder up (ex. controller/api/Example.php) the above form work ok.
OR i make the request in the following form
$this->request('GET', '/api/bb/example/users_get');
work ok.
Sorry my bad English.
If you add in setUp
a mock and call the request
method, the mock is ignored.
CI PHPUnit Test does not care about redirect()
now.
But as a sample, MY_url_helper.php
is included: https://github.com/kenjis/ci-phpunit-test/blob/master/application/helpers/MY_url_helper.php#L89
The current redirect()
in MY_url_helper.php
does nothing but return. It does not stop phpunit, but difficult to know redirect()
called.
How about changing it like this?
if (ENVIRONMENT !== 'testing')
{
exit;
}
else
{
while (ob_get_level() > 1)
{
ob_end_clean();
}
throw new PHPUnit_Framework_Exception('Redirect to ' . $uri, $code);
}
hi,
i installed the ci-phpunit-test through copy application/tests to my project's application folder, without use composer.
so, when cd to tests folder of my project, and then call phpunit, the message "The configuration file does not exist" show me, then nothing to happen.
please help me, thanks advanced.
Hi!
Everything seems to be working great, but for some reason, it takes pretty long to complete in my homestead/vagrant box.
Example single test in Codeigniter project (logging removed from phpunit.xml):
Time: 11.97 seconds, Memory: 6.75Mb
One single test with logging:
Time: 16.86 seconds, Memory: 12.50Mb
And building code coverage report takes several minutes.
I have Laravel project on same server and runnig 18 tests take:
Time: 2.5 seconds, Memory: 33.50Mb
Any ideas how I could make it run a bit faster?
When I follow the head tutorial, I can't get the bootstrap to work when there is a redirect() in my default controller. My Auth/index was redirecting to Auth/login.
I removed it and called directly $this->login() in Auth/index().
Following the main tutoriel, I can't get the testsuite to be ran.
It searches for test files with the default suffixes .phpt ans Test.php, therefore it tries to use CIPHPUnitTest.php as a testCase...
How do you specify the default test suite to run ?
Hi kenjis:
I used Base_Input to extens CI_Input,and I have a function:
public function get_action_path($glue='/'){
global $RTR;
$module = $RTR->fetch_directory();
}
when i used index.php global $RTR
is {CI_Router}[9]
,but now use "Welcome_test" , it's null。
why?
Hi,
I'm currently searching for a way to develop using CodeIgniter >= 3.0 with Netbeans and Unit Test.
So far, i didn't found any good tutorial which worked. All your tests are working in CLI in my application/tests folder. I've the same result, but it seems I can't run them on Netbeans.
I've always the same result when i'm trying to execute tests :
unrecognized option --run
In Testing properties, I have enabled PHPUnit and added the application/tests folder as test directory
In Netbeans's Options : PHP > Frameworks & Tools :
- I added the path of the phpunit.phar (4.7 if no mistake)
If you have any idea, I'd be very happy :D
Thanks.
Hi, found a bug - module won't load if some app configs are movet by environment folder, for example - I move $config['log_threshold'] from application/config/config.php to application/config/development/config.php and to application/config/development/config.php and terminal returnet error " Undefined index: log_threshold". Same situation is with database.php config file
Q | A |
---|---|
version | 1.0.x@dev (dev-master; after v0.5.0) |
PHP | PHP 5.5.10 (cli) (built: Apr 10 2014 17:49:22) / MAMP 3.0.5 |
OS | Mac OS X 10.10.4 |
How to reproduce:
$ git clone https://github.com/kenjis/ci-app-for-ci-phpunit-test.git
$ cd ci-app-for-ci-phpunit-test
$ composer update
$ ./install.php
$ ./test.sh
Output:
PHPUnit 4.7.7 by Sebastian Bergmann and contributors.
Runtime: PHP 5.5.10 with Xdebug 2.2.4
Configuration: /Users/kenji/tmp/ci-app-for-ci-phpunit-test/application/tests/phpunit.xml
Starting test 'CIPHPUnitTestRequest_test::test_getStatus'.
../test.sh: line 6: 61739 Segmentation fault: 11 ../../vendor/bin/phpunit -v --debug $@
I called show_404
in a controller, but risky was occurred.
I should close object in show_404
.
Any good ideas?
message:
$ phpunit -v --debug controllers/Welcome_test.php
R
Time: 416 ms, Memory: 8.50Mb
There was 1 risky test:
1) Welcome_test::test_method_404
Test code or tested code did not (only) close its own output buffers
/Users/trsw/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:153
/Users/trsw/.composer/vendor/phpunit/phpunit/src/TextUI/Command.php:105
OK, but incomplete, skipped, or risky tests!
Tests: 1, Assertions: 2, Risky: 1.
Generating code coverage report in Clover XML format ... done
Generating code coverage report in HTML format ... done
application code:
# application/config/routes.php
$route['foo/(.*)'] = "welcome/index/$1";
# application/controllers/Welcome.php
public function index()
{
$rsegment_array = $this->uri->rsegment_array();
$method = $rsegment_array[3];
if ($method == 'bar') {
show_404();
}
$this->load->view('welcome_message');
}
testing code:
/**
* @expectedException PHPUnit_Framework_Exception
* @expectedExceptionCode 404
*/
public function test_method_404()
{
$this->request('GET', 'foo/bar');
}
@kenjis, I was thinking it would be good create a base class for ci-phpunit-test library?
Hi kenjis,
Can you have a look at this stackOverflow post, is it a ci-phpunit-test drawback ?
http://stackoverflow.com/questions/33146362/coverage-phpunit-0-with-jetbrains-phpstorm
Hi
Sorry my bad english!
I'm using an library(https://github.com/chriskacerguis/codeigniter-restserver/) for create restfull application. my question is how make a model-mock in controller test?
I try some similar https://github.com/kenjis/ci-phpunit-test/blob/master/docs/HowToWriteTests.md#request-and-use-mocks but don't work.
Hi,
I got a message "Cannot redeclare class class_name" only for controller tests. I think this happens because I'm using "require" for load some libraries on my autoload.php.
After some tests I saw this problem coming from request() method.
It seems that ci-phpunit needs to call autoload again, that's correct?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.