Getting Started With Laravel 4, Chapter 5: Testing - It's Easier Than You Think
Testing in PHP is hard. PHP is hard. PHP apps are tightly coupled and that makes them hard to test. They can also be heavily reliant on databases and other outside dependencies. This is really outside the scope of unit testing, and really is called integration testing. Laravel was built from the ground up to make testing easier
Links:
Links:
http://phpunit.readthedocs.io/en/7.1/assertions.html#
Links:
http://phpunit.readthedocs.io/en/7.1/installation.html
We learn more about the call() method, and the assertRedirectTo() function.
Links:
Links:
- https://stackoverflow.com/questions/2741832/unit-tests-vs-functional-tests
- https://www.softwaretestinghelp.com/the-difference-between-unit-integration-and-functional-testing/
- https://codeutopia.net/blog/2015/04/11/what-are-unit-testing-integration-testing-and-functional-testing/
The benefits of testing
Testing has many benefits, as projects grow in size and shape, tests can insure everything keeps working, and we know that nothing is broken. A failing test should allow you to quickly know what the problem is, and where the problem is. This will allow considerable less downtime when deploying new apps.The anatomy of a test
- Arrange initial data
- Execute function on data
- Assert (verify) the data is correct
Unit testing with PHPUnit
Unit testing is the practice of splitting code into manageable dependencies so you can test them in isolation. Done right, unit testing will help your code work in small manageable chunks. This will allow each function you design to do one thing, and do it well.Defining what you expect with assertions
Assertions are used to compare expected output with actual output. There are over forty different types of asserts.Links:
http://phpunit.readthedocs.io/en/7.1/assertions.html#
Preparing the scene and cleaning up objects
The setUp() allows you to setup initial test data. The tearDown() function can be to remove anything you set up in your tests.Expecting exceptions
You can also setup tests where you expect exceptions. These tests won't have assertsTesting interdependent classes in isolation
You can use Mock to call other classes you need. Mock allows us to:- methods to be called
- how many times to call it
- what parameters the method should receive
- which parameters they should return
End-to-End testing
This type of testing simulates how an end user would use our app.Testing - batteries included
To run tests, you need PhpUnit installed. You can do this on a project basis, and use composer, or you can do it system wide.Links:
http://phpunit.readthedocs.io/en/7.1/installation.html
Framework assertions
We will write a few tests to test our cats app, and demonstrate how testing should work.We learn more about the call() method, and the assertRedirectTo() function.
Impersonating users
We can instantiate a User or any other class, and use that in conjuction with the be() function to test methods where we need a user.Testing with a database
Testing against the database is a hotly debated subject. A true unit test would not hit the database, as mentioned earlier, this is more of functional or integration test. With that said, Laravel makes this kind of testing simple and easy. You can use your migration scripts for creating tables, and your seeding scripts (or write new ones) to quickly populate the database. Laravel even makes it easy to use another database to run your tests on.Inspecting the rendered views
Laravel ships with Symfony's DomCrawler and CssSelector. The will allow you to test your views.Links:
Comments
Post a Comment