Finding Lauren
Side mission to rescue some guy’s wife — Lauren. I love these side missions. Sneaking in to places to hack something / rescue someone / steal something.
Side mission to rescue some guy’s wife — Lauren. I love these side missions. Sneaking in to places to hack something / rescue someone / steal something.
Listened to 3 tracks
Listened to 10 tracks
Listened to 13 tracks
Listened to 6 tracks
This is the absolute bare bones minimum implementation for my inventory keeping: saving items to my inventory list. Super simple, but meant only as an example of how I’d work when working on an API. Here are the changes made to my Inventory Manager. Those changes include the test and logic for the initial index
This is the absolute bare bones minimum implementation for my inventory keeping: saving items to my inventory list.
Super simple, but meant only as an example of how I’d work when working on an API.
Here are the changes made to my Inventory Manager. Those changes include the test and logic for the initial index endpoint too. I may blog about that part in a separate post soon.
One of Laravel’s many strengths is how well it is set up for testing and just how nice those tests can read. Especially now that I’ve started using Pest.
Here is the test I wrote for the store endpoint I was yet to write:
test('inventory items can be created', function () {
$response = $this->postJson(route(name: 'inventory.store'), [
'name' => 'My Special Item',
]);
$response->assertStatus(201);
$this->assertDatabaseHas(Inventory::class, [
'name' => 'My Special Item',
]);
});
Firstly I post to an endpoint, that I am yet to create, with the most minimal payload I want: an item’s name:
$response = $this->postJson(route(name: 'inventory.store'), [
'name' => 'My Special Item',
]);
Then I can check I have the correct status code: an HTTP Created 201 status:
$response->assertStatus(201);
Finally I check that the database table where I will be saving my inventory items has the item I have created in the test:
$this->assertDatabaseHas(Inventory::class, [
'name' => 'My Special Item',
]);
The first argument to the assertDatabaseHas method is the model class, which Laravel will use to determine the name of the table for that model. Either by convention, or by the value you override it with on the model.
The second argument is an array that should match the table’s column name and value. Your model can have other columns and still pass. It will only validate that the keys and values you pass to it are correct; you don’t need to pass every column and value — that would become tedious.
There is a term I’ve heard in Test-driven development called “sliming it out”. If I remember correctly, this is when you let the test feedback errors dictate every single piece of code you add.
You wouldn’t add any code at all until a test basically told you too.
I wont lie – I actually love this idea, but it soon becomes tiresome. It’s great to do when you start out in TDD, in my opinion, but soon you’ll start seeing things you can add before running the test.
For example, you know you’ll need a database table and a model class, and most likely a Model Factory for upcoming tests, so you could run the artisan command to generate those straight away:
php artisan make:model -mf Inventory
# or with sail
./vendor/bin/sail artisan make:model -mf Inventory
I dont tend to generate my Controller classes with these, as I now use single-action controllers for personal projects.
Within the routes/web.php file, I add the following:
use App\Http\Controllers\Inventory\StoreController;
Route::post('inventory', StoreController::class)->name('inventory.store');
Using a single-action class here to keep logic separated. Some would see this as over-engineering, especially if keeping controller code to a minimum anyway, but I like the separation.
Adding an explicit “name” to the endpoint, means I can just refer to it throughout the app with that name. Like in the test code above where I generate the endpoint with the “route” helper function:
route(name: 'inventory.store')
<?php
declare(strict_types = 1);
namespace App\Http\Controllers\Inventory;
use App\Http\Requests\InventoryStoreRequest;
use App\Models\Inventory;
use Illuminate\Contracts\Routing\ResponseFactory;
use Illuminate\Http\Response;
class StoreController
{
public function __invoke(InventoryStoreRequest $request): Response|ResponseFactory
{
Inventory::create([
'name' => $request->get(key: 'name'),
]);
return response(content: 'Inventory item created', status: 201);
}
}
Super straight forward at the moment. After receiving the request via the custom request class (code below), I just create an inventory item with the name on the request.
I then return a response with a message and an HTTP Created 201 status.
This code does assume that it was created fine so I might look at a better implementation of this down the line…
…but not before I have a test telling me it needs to change.
This is a standard generated request class with the following rules method:
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'name' => 'required',
];
}
Again, nothing much to it. It makes sure that a name is required to be passed.
Its not saying anything about what that value could be. We could pass a date time or a mentally-long string.
I’ll fix that in a future post.
In order to be “belt and braces”, I have also added a test that proves that we require a name to be passed. Pest makes this laughable simple:
test('inventory items require a name', function () {
$this->postJson(route(name: 'inventory.store'))
->assertJsonValidationErrorFor('name');
});
This just performs a post request to the store endpoint, but passes no data. We then just chain the assertJsonValidationErrorFor method, giving it the parameter that should have caused the failed validation. In this case “name”.
As the validation becomes more sophisticated I will look at adding more of these tests, and even possibly running all “required” fields through the some test method with Pests data functionality. Essentially the same as how PHPUnit’s Data Providers work.
Complete changes in git for when I added the store and the index endpoints to my Inventory app.
Listened to 15 Insane Horror Games On The PS4 You Absolutely Need To Play by GamingBolt
Earned 1 trophy in Everybody's Gone To The Rapture
Listened to 2 tracks
Earned 1 trophy in Everybody's Gone To The Rapture
Earned 1 trophy in RESIDENT EVIL 2
Earned 2 trophies in RESIDENT EVIL 2
Listened to 8 tracks
Playing the first Uncharted game on PS4. I’m enjoying everything about it except for the combat. I realise it’s an old game and isn’t representative of how awesome Naughty Dog are now. Just getting bit grindy at times. Looks beautiful too.
Listened to 11 tracks
Listened to 10 tracks
Listened to OutRun by Kavinsky
Listened to Anna Netrebko
Listened to Alligator by The National
Listened to 16 tracks
Listened to 6 tracks
Listened to 5 tracks
Listened to 4 tracks
Listened to Black Mirror - Nosedive (Music From The Original TV Series) by Max Richter
Listened to #UNLESHED 2 by Lady Leshurr
Thanks to a friend of mine, I got the chance to see Epica live at Shepherds Bush Empire last week. This was the second time I can remember going to see a band without having heard the music much before hand. However even though I only knew one of their songs, I still had a great
Thanks to a friend of mine, I got the chance to see Epica live at Shepherds Bush Empire last week. This was the second time I can remember going to see a band without having heard the music much before hand. However even though I only knew one of their songs, I still had a great night. All three bands were entertaining: Epica, of course, and the two supporting bands: “Beyond The Black” and “Powerwolf“.

Despite the idiots that were stood behind us moaning about how crap the support would probably be, Beyond The Black were actually pretty good I thought. They seemed to be going for the same look and sound as the main act but that’s fine.
They sounded pretty tight and the lady had a great voice to boot. Although they were the first support, and pulling in a lesser amount of dedicated supporters than Powerwolf – judging by the cheers, I have to say that I actually preferred Beyond the Black out of the two.

Hailing from Germany, Powerwolf are probably one of the most animated bands I’ve seen live. The first thing that we saw of them was their back drops depicting a ruined cathedral. I whispered to Grant (the aforementioned friend) “I’m going to be really disappointed if they don’t come out in full robes and the like”… I was not disappointed.
They came out dressed in full priest garb and even face paint. I immediately expected much death metal growls and screams, however the lead singer came across more operatic than anything. There were still the odd growls but that was pretty much par for the course that evening.
Although the songs did sort of start merging into one another for me, Powerwolf were still entertaining to see and had a great rapport with the audience throughout. Also pretty much the whole room of people knew most of their songs – including the older woman sat next to me, who was absolutely going mental for them.
She was an inspiration.
The one song that I knew, “Edge of the Blade”, sounding great live. As did the rest of the set. Throughout the whole show the band were forever switching places, including the keyboardist up on a stage-surrounding ledge, which he had rigged to a track. It seemed that every three or four minutes he would be rolling around the ledge on his keyboard.
Even though I couldn’t name a single song of theirs, I still enjoyed the show. The band seemed to have a great chemistry on stage and this came through in the performances and their unending energy.
I was very grateful we had seated tickets too. After our full-day walkabout in London, I don’t think I would have survived the “Wall of Death” and following Mosh Pit that was in full swing by the end. At least that’s how it looked from the safety of our balcony seats. The band kept the energy and positivity up till the very end of the night. The crowd too were pumped up until the end, until the moment came to say goodbye and we were all ushered out into the cold London night.
Given the chance I would probably see them again.