Welcome Guest, Not a member yet? Register   Sign In
Security and Testing
#1

[eluser]ericrjones1[/eluser]
This topic may be a duplicate topic that is discussed else where, if so please point it out because I would like to research this topic more. I looked in the forum but nothing seemed to answer my questions after a few different searches.

I am in the midst of building my second project using the CodeIgniter framework. For this second project, Unit Testing is required. I have looked into a bunch of different testing packages for the framework.

The built-in Unit Testing library, as the manual states, is not a full-blown test suite. I need a full-blown testing framework since this application involves a team of developers and a business critical application. With that, we have choose not to utilize the CI Unit Testing Library.

Toast is a prettier version of the built-in testing library. I would use Toast if the project was not business critical. In particular the Toast project does not have a way to test references to the same PHP object when using the singleton design pattern within the CodeIgniter framework.

CIUnit is a wrapper around PHPUnit. CIUnit has merit but the documentation is seriously lacking just following the online documentation the implementation fails. Not to mention the CIUnit implementation into the CodeIgniter framework is extremely obtrusive and makes me feel dirty (not a serious consideration). My team has tested CIUnit on multiple machines and with various "testing" projects. CIUnit's implementation was inconsistent because it worked on 1 out of 3 implementations. Granted I am not without error, but when three intelligent people implement a solution all following the same documentation and only one half-way works (all three failed to generate database fixtures) the project has some issues.

My team would like to utilize the PHPUnit Framework, but we have found that due to the line of code,
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
at the beginning of almost every framework file using PHPUnit or any other commonly utilized Unit Testing Framework is difficult to implement without re-working the CodeIgniter framework or at least implementing the solution better then CIUnit. We understand the merits of why direct script access is not allowed so that discussion need not occur.

So in group discussion our team wanted to know from the community some answers.

1) How does everyone else resolve Unit Testing issues with the CodeIgniter framework? Everyone tests their code with Unit Tests right? If there any examples, or tutorials on your solution to Unit Testing implementation please provide those in your replies.

2) How does Ellis Labs test the CodeIgniter framework? They must test the framework when changes are made so how do they do it. I want to know so I know how they test so I can test my project using their framework.

I know I have bashed CIUnit a lot, but please don't consider that project a bust. Like I said, the project has merit. Ultimately we will have to use CIUnit for this project, because my team currently does not have the time to create and implement our own solution to the CodeIgniter Unit Testing dilema. I will attempt to contribute back the CIUnit project when the time permits.

Thanks for the help
#2

[eluser]Dyllon[/eluser]
Have you looked into the Improved CodeIgniter Unit Testing on the forums?
#3

[eluser]rafsoaken[/eluser]
Hi slingshoteric,
I'm the guy who started CIUnit, because I felt like you about the serious lack of testing options offered by Codeigniter..
CIUnit depends on my free time, which I do not currently have much of - on the other hand contributions are welcome and I will give my best effort to integrate them. Also if you have any other trouble just send me a private message via my forum profile.

Hope this helps
rafsoaken
#4

[eluser]n0xie[/eluser]
[quote author="slingshoteric" date="1263255140"]
1) How does everyone else resolve Unit Testing issues with the CodeIgniter framework? Everyone tests their code with Unit Tests right? If there any examples, or tutorials on your solution to Unit Testing implementation please provide those in your replies.
[/quote]
We don't. We have found that though TDD is in theorie a nice idea, it is not practical for webdevelopment. Since the expected outcome can be tested on the fly (i.e. browser refresh) a lot of the speed involved with TDD (which works better where you compile code, and you don't want to break the build) gets lost. So you are basically adding time to your project for trivial tests. What are you going to test?

Models?
If they don't output the desired output, you will see so immediatly. Unless you are making a really complicated webapplication, I don't really see how the processing of data can be that difficult to write bug-free.

Controllers?
They are very hard to test and since they basically only serve as entry point you can test them by typing in the desired url. If it show content it works (yay!). If it doesn't, your controller is broken. (boo!). If you want to test the business logic inside Controllers you either write too complex Controllers (fat models, thin controllers principle) or there is something wrong with your logic: controllers should only hold a few key flowpoints determined by the input from either the user or the outcome of data generated by a model.

Views?
There shouldn't be any logic in them to begin with, so you should only test if the representation is as intended which you can't do with tests.

I have heard all the arguments for TDD and Unit Testing in general. I'm all for it in software development, just not while using a 'simple' framework like CI and a scripting language like PHP. I had a long discussion about it with Jamie Rambelow,a pretty strong advocate of TDD, and even he couldn't convince me. To me, if you write decent code, and know about the pitfalls in PHP (weak typed and all that), the benefit is minimal, while the added time for writing unit tests will range anywhere from 15 to 35% of development time (which means costs).
#5

[eluser]jiffier[/eluser]
While I may agree that on some times, you can have a library or a model that does some calculations susceptible of being unit tested, I cannot but get surprised when people try to use the singleton pattern in PHP. I think trying to use the singleton pattern in PHP is like being unable to have an erection, but still insist on buying condoms (sorry about my language, that meant to be funny Smile
#6

[eluser]thomasp[/eluser]
Hi,

[quote author="n0xie" date="1263407782"]
We don't. We have found that though TDD is in theorie a nice idea, it is not practical for webdevelopment.[/quote]
I am using CodeIgniter everyday in a 'web-dev' work area and I am not agree with you for that.
In heavy or long support projects, when there are some developers who works together on the same sources, sometimes a new features break some old features and unit testing can permit to check these sort of issue/regression.

CodeIgniter documentation insist to minimize model and you finally just make SQL request and all logical part is put in controller. So it is difficult to test logical things in controller.
But If you look like other PHP framework like symfony or rails, the unit tests are very advanced and big part of web apps logic is put in model.

I think CI developers don't want to develop unit testing because CI doesn't have native ORM solution and just a SQL request constructor. And I agree with you when you say it is useless to test a model function because it is just a SQL request result.

Finally, I will use a Doctrine plugin until CI dev team decide to integrate an ORM and better testing library in CI core.

Sorry for my bad english.

Thanks for your work.

Best regards
#7

[eluser]n0xie[/eluser]
[quote author="thomasp" date="1284499794"]Hi,

[quote author="n0xie" date="1263407782"]
We don't. We have found that though TDD is in theorie a nice idea, it is not practical for webdevelopment.[/quote]
I am using CodeIgniter everyday in a 'web-dev' work area and I am not agree with you for that.
In heavy or long support projects, when there are some developers who works together on the same sources, sometimes a new features break some old features and unit testing can permit to check these sort of issue/regression.[/quote]
Can you give me an example?

If you decouple your controllers from your models, this by definition should prevent any form of 'breakage'. For one if you change code in the controller, it shouldn't affect the model call. In the reverse case, working on a model, you only have 1 entry point. You should be able to edit the model any way you want without affecting your application, as long as the return value type is the same.
#8

[eluser]Eric Barnes[/eluser]
For me personally I like unit testing but I really believe it depends on the end result of your code. Take an application like pyrocms if it has unit tests in place then every time they are ready for a release the tests could be ran and confirm everything is working. I know it sounds like a pain to setup but once it is done they are always there and ready.

This also gets more chaotic when more than a couple of developers start contributing and the application gets bigger and bigger. I know I have personally ran into issues where I have added code from a pull request that works as they say but it breaks something else. Then the only way to see the breakage is by actually testing the other situation. If it was unit tested it would immediately be seen before a new release is made.
#9

[eluser]thomasp[/eluser]
[quote author="n0xie" date="1284523366"]
Can you give me an example?

If you decouple your controllers from your models, this by definition should prevent any form of 'breakage'. For one if you change code in the controller, it shouldn't affect the model call. In the reverse case, working on a model, you only have 1 entry point. You should be able to edit the model any way you want without affecting your application, as long as the return value type is the same.[/quote]

Ok, so imagine you have a 'car' entity in your database. You represent this entity by a model class. There is some controllers which use this model class. Imagine, there is two developers on this project and there are many others entities in the database.

First, If you minimize the model class and only do 'active-recorded' sql request with it, your controllers can became very heavy! Contrary to that, if you centralize application logic in the model, this will allow to factorize the code. (CI docs do not talk about that)

Next, if this 'car' class model centralize some application logic, it can depend or use other model class entities. It can also depend some personal library ...
Now, if some other developers must do some changes in others class, this can affect 'car' class involuntarily. And without unit testing, you not realize that your 'car' class model is broken.

An other case : You must refactor some method of your 'car' class because your first source code result was not optimized or unreadable. After that, your must do some tests to check if you have not break something. An if your code logic is very heavy, there is probably many cases to test! (Example :"Ok, if I try to delete a care which have current passengers affected? If I try to [...]" many many cases ..) At least, unit test can disclose some issues.

After that, there are also many others advantages from TDD theories but this is more contestable.
I am agree that this explanation have no sense on small web project but I think CI can be used on heavy projects and I just want to expose my opinion.

I tried to explain what I think but I have not a good English and I am sorry for that.

Regards




Theme © iAndrew 2016 - Forum software by © MyBB