How to setup phpunit testing? I followed by book at Install PHPUnit with Composer but not success
When you created it should’ve included phpunit.
What cakephp version are you using and what version of php are you running?
I installed the phpunit 9.5. Currently I am using framework the CakePHP 4. And following book CakePHP 4 testing. I couldn’t understand how to testing the file UsersController.php. also what will do process the file testing.
To test controller see Controller Integration Testing
And create fixtures if your controllers use models.
As how to run the tests you can call
vendor/bin/phpunit in your terminal
Based on this thread so far, I’m going to assume you are new to unit testing. Apologies in advance you are already experienced.
I think 'Controller Integration Testing` is the deep end of the pool. That section will give you some notion of how to proceed, but you shouldn’t look at it as a ‘typical’ case. It is actually a pretty advanced case.
Tests in general follow a pattern:
- set up the fixture (data and other environmental components)
- instantiate the system you want to test and call the method of interest
- evaluate the result of the call
- tear down the fixture elements (this is pretty well handled by
It’s easy to have a Controller method that expect large amounts of interrelated data to exist. There may be Authentication to negotiate before the call can proceed. There may be Components or other modules that need to be properly prepared.
In short, the fixture requirements for controller/action testing can be high!
Beyond that though, there may be multiple paths through a controller’s action. The
if (request->is(post) and
if (table->save(entity)) branches of a baked
edit method are good examples. All the possible routes and outcomes should be tested. Not a trivial task.
Learning to test in smaller, isolated objects will be easier.
Again, assuming you’re just beginning in testing; be patient. The benefits are undeniable but they take time to realize.
I found I needed to write my systems differently to make them testable. In the long run this has made my programs more maintainable and easier to understand. But it is a long road.
Also, you may want to look at vierge-noire / cakephp-fixture-factories , an excellent plugin that I have found makes database fixture setups MUCH more maintainable.