5 years ago

What should I be testing? Laravel 5 with a command architecture

Posted 5 years ago by BenSmith

Apologies for the length of this post, I wanted to try and get my current thoughts down concisely and ended up just doing a brain dump!

I’m just about to start a new project using the command bus architecture and I’m going to force myself to use TDD/BDD as much as possible. My problem is I don’t know what I should be testing and with what testing framework! Below are the types of testing I am thinking about from highest granularity down to the lowest, including my (possibly incorrect) definition of each and an example of usage:

Acceptance Testing

My plan at the moment is to use Behat 3 for my acceptance tests. These tests have no knowledge of the internals and interact with my code much like a user of the site would. I am most confident with these type of tests given I have just spent a few days learning Behat.

If I was testing the registration functionality of the site I would be testing that when a user fills out the registration form they then see a welcome message.

Functional Testing

I must confess I am still somewhat unclear on the difference between functional and acceptance testing (especially given that they mean different things to different people). In my mind, functional testing is similar to acceptance testing in that it interacts predominantly with the project through the user interface, however, it also has some access to the internals. For example, in a functional test we could check whether a record exists in the database.

Using the user registration example again, my functional test would check that when the user fills out the registration form: they are redirected to a certain page, a welcome message is shown but also we could check in the database if a user record was created.

I am unsure whether functional tests are truly necessary. I think that as long as I provide a number of scenarios for each acceptance test feature that this should cover most of the necessary checks. Please let me know if you think this is wrong.

Integration Testing

I think of integration tests as tests that cover a chunk of functionality that will involve multiple classes interacting with each other.

An example would be testing that a user repository persists a user. The test would involve creating a fake user, passing it to the repository, and then checking the database to see if a record was added.

I have in the past used Codeception for these type of tests however given that I plan on using Behat and PHPSpec (see below) I am somewhat apprehensive to add yet another testing framework. I think this type of test is important but is there any alternative method to do this with either of the previously mentioned tools?

Unit Testing

I am going to be using PHPSpec for my unit tests, I have used it a number of times on small learning projects and for the code katas on this site but never in a major project. I really like it but I am having troubles working out what I am should be testing within the command architecture. Here is a list of components that I am expecting to use within the command architecture/Laravel 5 and my current feelings on whether I should be testing them:

  • Command Objects - No need to test as they are are just data objects.
  • Request Objects - No need to test as they are part of the framework
  • Command Handlers - I usually set up my command handlers with a number of dependencies and then the handler is just used to pass the relevant information to these objects. Does this mean that I should just be mocking the dependencies and checking that the methods are called? This seems to me like it is tying me to my implementation too much.
  • Controllers - If I am just transforming a request object to a command object and passing it to the command bus, is there any need to test the controller given that it will be covered by the acceptance tests?

I think basically I need to see a fully tested project that uses the command architecture. I have done the Larabook series on here and while very helpful, unit testing wasn’t mentioned. If you’ve read this far, many thanks, any advice would be appreciated.

TL;DR: What types of testing should I do, what tools should I use to for each type and what should I be unit testing within Laravel 5 / the command architecture?

Please sign in or create an account to participate in this conversation.