DANIEL WERNER

Senior Software Developer | TDD and Clean Code enthusiast | Open-source contributor and maintainer

Let's do TDD (with video)

I am a big fan of Test Driven Development and trying to promote it on every possible occassion. I am doing it because having a good test suite makes me confortable when refactoring or making changes in my code. Also it saved me a couple of times from creating a hidden bug while implementing or changing something. In this blog post I'll guide you through the process how to start with TDD in any PHP project, how to install phpunit, and even how to make it fun. But let's start with a short introduction about the test driven development. TLDR; If you...


Under the hood: How states work in Laravel factories

Since Laravel 8 we can use the new and reworked model factories. They allow us to create multiple interconnected model instances using a simple and easy to read syntax. As usual Laravel has a great documentation about how to create and write these factories. In this article I don’t describe the usage of the factories, if you are interested in that check out the documentation. Instead we will go a little deeper and understand how the states work in factories. Also we will show an interesting example we run into lately, where the states worked differently as we expected. What...


Testing with Mocha – 5. BDD style assertions with Chai

Chai is a TDD and BDD library, and comes with several flavors for assertions: should, expect and assert. In the following lessons we will take a look at both styles and see how to use them in practice. Installation and first example First we need to install chai using a package manager, for example npm: npm install chai When it is done, let’s take a look at our first example with chai. See the Pen Testing with Mocha – Example 5.1 by Daniel Werner (@daniel-werner-the-decoder) on CodePen. As we see in this example we are using expect instead of assert, let’s see...


Testing with Mocha – 4. Testing error handling

In this post we will take a look how can we expect that an error is thrown and how to make sure no error is thrown when when everything should be fine. Asserting an error is thrown Let’s take a look on how to make sure the expected error is thrown in case when we need it. As an example lets extend our generic collection class, and create a specific collection which can store only numeric values. See the Pen Testing with Mocha – Example 4.1 by Daniel Werner (@daniel-werner-the-decoder) on CodePen. As we can see in the example above,...


Testing with Mocha – 3. Testing asynchronous code

In JavaScript we often write asynchronous code, for example ajax calls, database operations etc. When testing our code we want to be able to test this asynchronous parts of our code as well. Let’s see how we can achieve this when testing with Mocha. Asynchronous code with callbacks For testing async code which uses callbacks we can use Mocha’s done function. When passing done in the test Mocha knows that it should wait for that callback to be called. Let’s see and example. Let’s add a save method to our collection, which will operate asynchronously. See the Pen Testing with Mocha – Example 3.1...


Testing with Mocha – 2. Using the assert library

In the previous lesson we’ve created our first test, but used only one assertion to check if the result equals to a predefined value. Let’s take a look at the capabilities of the Chai’s assert library, and explore them through some more advanced examples. Using strictEqual and notStrictEqual In the first example we’ve already used the equal assertion. The basic difference between equal and strictEqual is that the latter compares the types as well. The strictEqual only accept the value if it’s type and value is the same as the expected one. Let’s see and example of how to use...


Testing with Mocha – 1. Installation and set up

  Introduction Mocha can be run both from the command line or from the browser. If you want to run it from the terminal you can use npm or yarn to install it. You can install mocha globally and use it in all your projects or install as as dev dependency in the project locally. I’d recommend installing it locally because this way you can have different versions of Mocha in different projects, and also it makes it easier to install Mocha in continuous integration system to automate the test suite. Installing Mocha Installing via command line Let’s see how...


Under the hood: How database transactions work in Laravel

Continuing the under the hood series, this time about database transactions in Laravel. I won’t repeat all the things on how you can use the transactions in Laravel. If you are not familiar with the topic you can find everything in the documentation here. We’re now focusing on how these implementations work in the background, what caused us some headache lately and how to avoid it. So let’s dive in. Transactions with closure As you are probably already familiar with it, you can use closures in the DB facade’s transaction method, like this: DB::transaction(function () { DB::update('update users set votes...


Oh ****, I should have had a backup

Just like the unit or integration tests in development, the backups may seem like unnecessary wasting time, money and effort… until a certain point. With tests this point usually comes when we do a “quick fix” on Friday afternoon, deploy it to production and realize that we unintentionally broke the whole system. The following weekend is not going to be nice for the dev team. The situation with the lack of backup is even worse. Someone hacks into your system. The database server crashes. A developer runs an update script on production but forgets the where clause. These things happen...


How to authorize static files in Laravel with Nginx auth_request

When it is necessary to authenticate and authorize the access of static files, such as downloadable content, images etc, the common approach is to read the file with PHP and return the content. Laravel’s download method also works this way. There is nothing wrong with this approach. However it could lead to some performance issues if there is a high load on the server, or when serving big files. Using Nginx http_auth_request_module If you use Nginx built with the http_auth_request_module you can utilize the auth_request directive to create authentication based on subrequest result. As the official documentation says: To perform...


Case study – building a simple webshop with Laravel

Originally published at 42coders.com on Jul 15, 2020 Introduction This is a case study of building the webshop kapes.rs, so this is not a detailed tutorial how to build a webshop with Laravel from scratch. Instead, I am going to show what technologies and packages I used to build it, and some details which I find interesting enough for sharing. This shop is my side project built it for my wife, and there she sells her handcrafted beanies, so if you need one, definitely check it out here :-). Why to build a shop yourself? Why would one build a shop from scratch...


Under the hood: How model attributes work in Laravel

Laravel model attributes are basically representing the database fields for the given model. When data is retrieved from the database, it can be accessed through the model as they were actual properties of the model instance: $model->database_column_name. However the attributes are not real properties of the instance, but they are stored in the protected $attributes property. The attributes are accessed using the __get magic method. This makes it possible to do some cool things with them like mutations and casting. As this in an under the hood post, I won’t explain what you can do with attributes, you can find...


Using Github actions and Deployer for creating CI/CD for Laravel

In one of my previous articles I’ve written about how to create an automated pipeline for continuous integration and continuous delivery using travis. Lately GitHub actions are quite popular, I am also experimenting with them. I started by reading the articles written by the guys from Spatie, you can find them here and here. While setting up the actions I encountered some issues with the mysql service, I’ll dive into it later. Let’s set up the same pipeline as last time. Run the tests for every push and deploy the application if the tests are successful. TL;DR; For those who...


Automated linux server backup with Dropbox

Introduction I was looking around for some low cost backup solution for my server when I got an idea. Let’s use dropbox for that. The free version of dropbox has 2 GB of space and it has an official linux client. The offered space is not too much, but for smaller sites it is good enough. I’ve created a collection of scripts for the backup, it creates the backup of the given input directory, from the Nginx configurations and all databases. The backup is stored in the synchronized Dropbox directory the last 30 files are kept, the older ones are...


A recap of 2019

This year is about to end, so I thought it might be interesting to write a recap of what I did this year. This is the first time I am writing this kind of blog post, hopefully there will be more of them in the future. Open source development I created my first open source project back in 2009, but after that, for a really long time I only used OSS in my projects, without giving back to the community. This year I’ve continued with the OSS development. I created two php packages: Laravel SchemaCrawler and PHP Quality Tools. Developing...