How to Unit test CodeIgniter code - php

I need to do unit testing of CodeIgniter. I tried CIUnit but coulnd't get the fixtures set up in v1.7.2.
I tried pure PHPUnit but could not create stub of model class as it was inheriting active-record and it was throwing error that activerecord not found. Obviously there is no controller which loads the activer record helper.
Is there a way to do unit testing in Codeigniter. I know about simple test library but how good is it when compared to phpunit.
Is there any means to do integration testing also.

FooStack is an add-on that may help you. It includes instructions for fixtures and is compatible with 1.7.2 (which is an old release, BTW).
http://www.knollet.com/foostack/

Related

Is there a way to test Symfony application other than PHPUnit?

During an interview, project manager asked me the following question:
I see in your resume that you are using PHPUnit 4 for tests? Why don't
you use the built-in testing of Symfony?
I answered:
What I know is that Symfony app testing is done via PHPUnit, which is
recommended from the docs of 2.3 and 2.7. I don't know if Symfony3
comes with a testing component!
Was my answer correct? Thanks for the explanations.
Yes, you are correct. Symfony has a WebTestCase (extending the KernelTestCase where you can find that it extends PhpUnit) for functional testing. Additionally Symfony provides a PhpUnitBridge that provides helpful stuff for writing PHPUnit-Tests for a Symfony app, e.g. to make tests fail that use deprecated components. Both rely on PHPUnit and provide additional capabalities on top of "vanilla" phpunit.
There are alternative testing tools, such as phpspec for writing unit tests and Behat for higher level tests (functional and acceptance testing), but both are not used inside Symfony and therefore don't really seem to be what was being asked about.
Additional information to your answer:
In my experience, WebTestCase also bootstrap kernel as static method in all test. Due to that, is not so good to Unit testing by that tool, as this test should be fast. They design this with functional testing controllers and actions but to not need start HTTP server.
Then for:
unit testing, build-in in is not good option
functional testing, it was original intention, but i think, Behat is better solution
system testing - not so good.
Summary, that is why i don't use this tool.

How to integrate Symfony 1.4 with Codeception

I want to be able to integrate Symfony 1.4 with Codeception unit tests.
I created init files but was unable to include Symfony library classes to be used for testing.
This page Symfony 1.4 Unit testing explains a little but it doesn't show an example of instantiating a class even with Lime test. I tried this with Lime and it didn't work. Is there a way to auto include classes using Symfony Core which allows me to instantiate classes for my needs when creating unit tests?
I tried including unit.php file from Symfony test folder but got the same result of not being able to instantiate existing library classes.
If it is too hard to do it with Codeception then I'm looking for options in Lime test also. I just want to be able to unit test...
How would I do this?
Running these will load all your classes:
require_once __DIR__.'/lib/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();
Of course you should set the paths properly.
You can also try to use the Symfony 1.5. maintained by L'express (https://github.com/LExpress/symfony1). It uses composer and have the autoloading prepared for you. (you just need to include autoload.php)

php kohana functional testing database environment

I've got an application developed in kohana 3.2. I want to write some functional tests that will affect database content. I would like to load the database from a dump file each time I run all functional test suite (so that I'm sure I can write and remove from the database as much as I want).
How can I do that in Kohana? Does it support functional testing anyhow?
Kohana supports unit testing with the unit test module and php unit installed. It sounds like you may be possibly want to do unit testing with mock objects. You could also set up your database using your models or a dump file too. Full functional testing is possible as well. A good place to start is enable the testing module and then start here...
https://github.com/kohana/unittest/tree/3.2/master/guide
And phpunit here...
https://github.com/sebastianbergmann/phpunit/
I use Codeception for all of my acceptance test, functional tests, and unit tests which is powered by PHPUnit. It is the best testing framework that I have found for PHP. You can pre-load sql dumps prior to functional tests and query directly against the database. It also integrates easily with Selenium for testing browsers.
I released a vagrant development environment with an empty checkout of Kohana 3.3.1 if you want to try it out Codeception. The tests are incredibly easy to write.
Intro to Vagrant with Kohana and Zen Kommerce
Codeception

BDD/TDD with CodeIgniter

I recently joined a company that uses codeigniter. I have been using nodejs and rails a ton so I feel like I am missing BDD like tools (rspec/jasmine/mocha). Does PHP and/or CodeIgniter have any sort of CLI based testing suite. If so, how should I structure my app? Any good conventions that can be followed?
There's a project called CIUnit that integrates phpunit to codeigniter and provides some helpful functions, and make it a generally pleasant experience. Unfortunately the official branch is stopped at CI version 1.7.2, but there's a fork that I've been using for 2.1.2.
This gives you the usual xUnit style test suites, code coverage report generators, selenium integration, mock and stub objects and such.
Two most popular PHP BDD frameworks should work just fine with any PHP project:
Behat (StoryBDD)
PHPSpec (SpecBDD)
Today's best solution would be PHPUnit and Selenium. I wrote articles covering the usage here:
On PHPUnit: http://taiar.github.io/php/2013/11/08/testing-codeigniter-applications-with-phpunit/
On PHPUnit with Selenium: http://taiar.github.io/php/2014/04/21/acceptance-tests-on-codeigniter-with-phpunit-and-selenium/

Working with PHPUnit and CodeIgniter 1.7.2

I recently started with the unit testing for my php website made in CodeIgniter 1.7.2. I want to test my web site using PHPUnit.
So I Googled it and came to know that there is a library for CodeIgniter & PHPUnit Bridge: http://www.foostack.com/foostack/. I have followed all the steps, and got the code running, but when I execute a test case from console, my default controller is executed and the html for the page is printed on the console.
Can any one please tell me what exactly is happening or am I doing something wrong? Also if any one has any other ideas to test the model without using the foostack library please suggest it. Please let me know if any other details are required.
Thanks in Advance.
CodeIgniter is not an ideal framework where you can unit test your code with PHPUnit. I have myself tried using FooStack once and was successful then, but it is too cumbersome. There is no way of testing a model isolated in CI because of the god object CI makes.
You can however try SimpleTest, here is a good library that can help you with it: https://github.com/ericbarnes/codeigniter-simpletest
Other than that there is the Unit Test library provided with CI - which in nowhere near a full unit testing framework, but does let you do basic assertions, reporting etc.
If you're really serious about unit testing, I'd suggest you move to frameworks that value this. My favorite is Zend Framework, but there are other good ones as well: Symfony, Kohana, Lithium, etc.

Categories