Inspect the response in Laravel PHPUnit tests

The Problem Sometimes, when running integration tests using Laravel and PHPUnit, it’s useful to inspect the response when you can’t figure out why the test is failing. In Laravel 5 and later versions, testing has been highly improved with an API that allows nice and fluent interaction with your application. Inspect the Response Object In Laravel, each Test extends the default abstract TestCase class defined in Illuminate\Foundation\Testing. This class uses the CrawlerTrait to provide iteraction with the application, therefore it contains the $response property, which is an instance of \Illuminate\Http\Response and contains the last response returned by the application. For this reason we can use the getContent() method on the response object to check what the application actually returned. In the following example we are first creating a new user in the database, then we use the chainable API to interact with the application. Finally we use the dd function...

Read More
Basic ProcessWire website workflow - Part One

Introduction For those of you who don’t know what ProcessWire is (shame on you!), here the introduction statement of the official website: “ProcessWire is a free PHP content management system and framework (open source CMS/CMF) built to save you time and work the way you do. ProcessWire provides simpler and stronger control over your pages, fields, templates and markup at any scale.” ProcessWire Logo In my opinion, ProcessWire is one of the most simple and effective tools for building websites, both small and large. It has all the features that you demand from a CMS/CMF. I strongly suggest you to check it out, if you are new to it. But don’t take my word for it, go on the official website and check out what differentiates ProcessWire from the other CMSs. Why is ProcessWire (PW from now on) the right tool for the job? These are the main reasons: It’s...

Read More
First steps with GraphQL in Laravel Framework - Part One

Introduction GraphQL1 is one of the recent major technologies introduced by Facebook in the React ecosystem. Essentially it is a declarative approach for querying data from a backend. “React is changing the way we build complex client-side UI applications. Flux rethinks the flow of data through applications. GraphQL is changing the way we fetch our data from the server.” In a nutshell, GraphQL is challenging REST. The key concept is that the client is the only component of an application that knows best what data is needed for rendering the UI. This means that each part of the UI can fetch declaratively from the server the exact data that it needs, without affecting other parts of the application. I’m sure that this is a common scenario: you set up a REST endpoint for fetching the users of your application in the administrator section. Later you want to use the same...

Read More
JavaScript Quiz Answers Explained

Introduction JavaScript is notoriously a simple programming language, but let’s be honest: it sucks! Don’t get me wrong! I love programming in JavaScript, it gives extraordinary freedom in building things that runs everywhere, but at the cost that is so easy to write bad code. The reason falls back to its winding history, starting from its creation in 10 days by Brendan Eich. Wikipedia has an exhaustive entry on the subject, so I suggest you to check it. Nevertheless after 20 years, a language with loads of imperfections, is going through its sixth standardization and is currently implemented on PCs, TVs, watches, cars, circuits and is, from time to time, adopted in place of Java in server environments. I remember back in the days when I was using JS to create random snowflakes on a webpage, now enterprise JS applications consists in thousands of hundreds lines of code and handle...

Read More
Laravel 5 Cron Jobs on shared hosting

Introduction The Laravel documentation covers all the information you need to configure Cron Jobs on your hosting machine. Some years ago we were required to define an entry on the server for each task we needed to schedule. Now with Laravel we can simply define a single entry that interacts with the Schedule object, which will take care to run the tasks that are due. The only Cron entry needed on the server, is the following * * * * * php /path/to/artisan schedule:run >> /dev/null 2>&1 Basically it means that every minute (* * * * *) php will run the artisan command schedule:run. The notation >> /dev/null 2>&1 basically means that the standard output (STDOUT) is redirected to /dev/null and so it is ignored, while 2>&1 means that STDERR is redirected to STDOUT1. Define Jobs In the schedule method of the App\Console\Kernel class you can define the tasks...

Read More
Five Programming Problems

Introduction Recently I stumbled upon this blog post on ShiftedUp. The writer briefly explains that he encountered many cases of job applications for Software Engineer positions, made by people who actually have no idea of what programming means. Then he proposes 5 coding problems to solve in less than one hour, using whatever programming language you are comfortable with. So I decided to take the challenge! The language used is JavaScript, but the examples can be written easily in any other language. Disclaimer: I’m aware that in most of these solutions are not optimal (e.g. don’t check wrong input, etc.), I’m just sticking to the examples provided by the author. Problem 1 “Write three functions that compute the sum of the numbers in a given list using a for-loop, a while-loop, and recursion.” function forSum(list) { var i, result = 0; for(i = 0; i < list.length; i++) { result...

Read More