2. What’s in store?
• Look at API concepts within Laravel
• Look at testing concepts within Laravel
• How we can gradually build an API with confidence in its functionality,
without ever opening an API client
3. @michaelpeacock
• Organiser of PHP North East
• Occasional author and speaker
• Freelance developer
• Consultant CTO for early stage SaaS start-ups
12. Model integration tests
• A good starting point
• Give us confidence that models, migrations, relations and factories
are setup correctly
• Especially important if we are providing custom field names for
relations, or working with models outside of the root app directory
• Factories are important for API tests, so its good that we test the basis
setup before we move onto that.
• Inspired by Amo Chohan: 12 tried and tested top tips for better
testing
13. Key things to test initially
• We can instantiate the class
• Any relations
40. Better responses
• Venue name and description should be included
• Event id should not be included
• Start and end date should be formatted a specific way
Discuss how these work, and the impact on database transactions (i.e. how that works)
Explain about the relationships first, putting thought into the modelling of the app.
Emphasise how many times I've been caught out by a migration where it was linked to the wrong FK because I copy and pasted a migration!
We can then iterate between tests and code, adding in the relationships that we need, until we get passing tests.
Returning a collection or a model, combined with API request headers, results in the data being converted to JSON
By default the JSON response includes all fields in the model, we can test that we have some fields that we want
Error: date fields are not cast as dates, fix that, then get failing tests.
Resources: transformers for our eloquent models
Failing test because nothing is created, its an error not a fail as such because we called findOrFail, in our test, so our test has thrown an exception