Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Fabien Potencier:
Symfony 4: A quick Demo
May 05, 2017 @ 09:39:52

Fabien Potencier has continued his post series covering the next major release of the Symfony framework, Symfony 4. In this latest post he walks you through a quick demonstration of the creation of a new Symfony 4 application including a simple administration system.

Time to test Symfony 4... or at least let's test the experience of developing Symfony 4 projects with Symfony 3.3. Keep in mind that all the tools are in preview mode. Features might evolve over time. I'm waiting for your feedback! The first stable version of Symfony Flex will not be released before Symfony 4 at the end of November 2017. It gives the community plenty of time to discuss the changes I have described in this series of blog posts.

He then walks through the process for creating the application:

  • Using Composer's "create-project" to make a new skeleton application
  • Setting it up as a git repository
  • Defining environment variables
  • Registering the framework bundle
  • Installing the command line tools

With the basic application set up he then shows how to install the EasyAdminBundle to create the simple administrative interface. He's also created a screencast showing this same process so you can see it all in action.

tagged: symfony4 demo screencast skeleton application bundle install

Link: http://fabien.potencier.org/symfony4-demo.html

Adam Wathan:
Detecting Out of Sync Mocks in Mockery
Apr 05, 2017 @ 11:14:41

Adam Wathan has shared a new post on his site with advice on finding out-of-date mocks when using the Mockery mocking tool in your testing.

If you're not careful, it's easy to find yourself in a situation where a test double has gotten out of sync with the actual class or interface it's mocking.

In this quick screencast (taken from my Test-Driven Laravel course), I walk through how I use a little-known Mockery feature to help track down these issues and make sure I'm not mocking methods that don't exist.

The quick screencast (about 4 minutes) gives an example of locating the issue when a "Ticket" class is refactored. While the tests still pass, it can cause issues in testing and can be difficult to find. Mockery comes with a configuration option (in 1.0 alpha) to disable the mocking of methods that don't exist on the original object. He shows how to disable this feature and what the resulting error looks like when the tests are run.

tagged: mockery screencast unittest mock sync class disable configuration

Link: https://adamwathan.me/2017/04/03/detecting-out-of-sync-mocks-in-mockery/

Master Zend Framework:
How To Generate Class Factories The Easy Way with FactoryCreator
Jan 20, 2017 @ 10:07:57

The Master Zend Framework site has a new tutorial posted guiding you through the process of generating class factories the easy way with the help of the "FactoryCreator" tool in the Zend ServiceManager component.

If there’s one thing that’s always frustrated me when working with Zend Framework, it’s having to create factories for classes. Sure, it’s gotten easier as Zend ServiceManager’s continued to ever improve. And PhpStorm and Zend ServiceManager Grand Master, Gary Hockin, has given me a number of great tips and suggestions.

But it’s always been something I’ve felt frustrated by. Perhaps you feel the same. [...] But, what I’ve felt for some time is that they could also make it easier for us to follow these best practices too, such as with some tooling support. In the latest release of Zend ServiceManager, version 3.2.0, they have.

He goes on to talk about two tools that are included in this latest release: the ConfigDumper and FactoryCreator. He helps you get the FactoryCreator tool installed and provides a simple example of it in use, generating the factory for a "JournalService" class. He includes the results of the generation of the simple example before moving on to a more complicated example: a TableGateway object. The final example shows the generation of the factory for an "Actions" class, handling the controller processing for a simple MVC application. If you're a bit shorter on time, he's also created a screencast version of the tutorial you can view in-page or over on Vimeo.

tagged: zendframework generate class factory factorycreator tutorial screencast

Link: http://www.masterzendframework.com/simple-factory-generation-with-factorycreator/

SitePoint PHP Blog:
Interrupting Applications with Laravel Middleware
Nov 16, 2016 @ 12:13:05

The SitePoint PHP blog has a new tutorial posted showing you how to "interrupt applications with Laravel middleware", adding functionality to your request/response handling in a more automatic and reusable way.

Before we launch into using middleware with Laravel, what is middleware? In general, middleware is software that connects different systems to one another. In Laravel, it’s one of the most well known and robust PHP frameworks that are available for use today, middleware provides a way for you to filter out your HTTP requests and manage those requests. This allows you to dictate how those requests interact with your application.

The use of middleware in Laravel 5 can interrupt requests to your application. [...] Laravel’s middleware can interrupt those requests and then perform various actions such as controlling access and requiring authentication to enter certain areas.

They get into some of the details about middleware, how it "interrupts" your requests and then relate it back to its use in Laravel applications. The tutorial talks about using the "artisan" command line tool to make the middleware and an embedded video from their own Laravel series giving even more detail.

tagged: laravel middleware interrupt framework tutorial screencast

Link: https://www.sitepoint.com/middleware-in-laravel-5/

How to Secure a REST API With Lumen
Oct 26, 2016 @ 10:56:58

Over on the TutsPlus.com site there's a new tutorial posted for the Lumen users out there building REST APIs. The post walks you through an authentication method for the API making use of Laravel's included "guard" handling and an API token.

Lumen is Laravel's little brother: a fast, lightweight micro-framework for writing RESTful APIs. With just a little bit of code, you can use Lumen to build a secure and extremely fast RESTful API.

In this video tutorial from my course, Create a REST API With Lumen, you'll learn how to use Lumen's built-in authentication middleware to secure a REST API with Lumen.

The post includes the screencast of the tutorial but it also includes all of the content below that in more developer-friendly text form. Screenshots of the code in various states are also included as well as descriptions of what's happening in the auth process along the way.

tagged: lumen security rest api screencast tutorial

Link: https://code.tutsplus.com/tutorials/how-to-secure-a-rest-api-with-lumen--cms-27442

Master Zend Framework:
How To Simplify Zend Expressive Configuration with ConfigProviders
Aug 15, 2016 @ 12:42:19

The Master Zend Framework site has a new tutorial posted helping you simplify the configuration on your Zend Expressive application with the help of ConfigProviders, a handy feature that lets you split up the configuration into logical "chunks" as PHP classes.

Given Zend Framework’s design (and accompanying flexible nature), this [configuration complexity] can easily be the case if we’re not careful. [...] Specifically, we’ll likely end up with a config/autoload directory polluted with a plethora of configuration, including for dependencies, routing, and middleware.

[...] As it turns out, this was something which was already identified by other developers, including the Zend Framework contributors. [...] In there, he mentioned ConfigProvider classes as a simple way of enabling ZendForm ViewHelpers, which aren’t enabled by default in Zend Expressive. As I looked at the composition of the file, I realized that this was the answer I needed to solve the configuration issue I created for myself.

A screencast is included in the post showing off the solution but the code an explanation are below that as well for those more interested in reading than watching a video. He walks you through the creation of the configuration provider including setting up the dependency configuration, updating the route handling and, finally, actually using the provider in your global configuration.

tagged: zendexpressive configuration provider configprovider tutorial screencast example

Link: http://www.masterzendframework.com/configproviders-classes/

Testing in Docker (Using Different PHP Versions)
Aug 12, 2016 @ 12:23:58

On the ShippingDocker.com site there's a video (and matching tutorial) posted showing you how to use Docker to test with multiple PHP versions with relatively little difficulty. In this case they're not testing the frontend of the application, they're running its unit tests.

[This is a] quick video on running PHP unit tests against different versions of PHP using Docker. [We'll] cover unit testing with PHP.

They start with an example of using the pywatch tool to do the testing without Docker, automatically executing the tests when something changes. This has the limitation of only being able to use your current, local version of PHP. They then shift over to the Docker side of things and show how to run the same pywatch command inside a container of your choosing, tagging it with the PHP version and making it easy to switch between them in the future.

tagged: docker version unittest different example video screencast tutorial

Link: https://shippingdocker.com/blog/docker-testing/

Adam Wathan:
Stubbing Eloquent Relations for Faster Tests
Aug 08, 2016 @ 11:52:53

Adam Wathan has a recent post to his site showing you how to stub out your Eloquent relations in a Laravel application for use in your testing (rather than hitting the database directly).

When you’re trying to test methods on an Eloquent model, you often need to hit the database to really test your code.

But sometimes the functionality you’re testing doesn’t really depend on database features. Is there any way to test that stuff without hitting the database?

He starts with a look at the more traditional method, using the models normally and testing with the database. He includes a simple test and class showing a basic "song duration" integer response. He gets into a bit more detail on how the Eloquent code grabs the data it needs when a relation is accessed (hint: not a separate query) and how to update the test to mimic the eager loading of the duration information. He ends the post by pointing out that "nothing is free" however as, if the underlying database implementation changes, the test would start to fail regardless of it not using the database.

tagged: tutorial screencast example relation eloquent unittest stub

Link: https://adamwathan.me/2016/08/04/stubbing-eloquent-relations-for-faster-tests/

Adam Wathan:
Customizing Keys When Mapping Collections
Jul 19, 2016 @ 10:52:29

Adam Wathan has a new post to his site talking about mapping with collections and customizing the keys when injecting new data into your Laravel collections.

People often ask me, “how do I specify keys when I’m mapping a collection?”

It actually ends up being a pretty interesting topic, so I decided to cover it in a short screencast, as well as in written format below.

He shows how to translate a simple set of data into a much more slimmed down version. He points out that the "map" function could be used but it doesn't allow for setting keys. Instead he talks briefly about how the problem could be solved in Javascript (returning an object instead of an array) and how to use the "reduce" method to filter and reset the data as it goes through the array. He finishes out the post talking about learning from other languages, the "toAssoc" macro on Laravel collections and mapping the data back to an array with a custom macro.

tagged: customize key mapping collection laravel object javascript example screencast

Link: https://adamwathan.me/2016/07/14/customizing-keys-when-mapping-collections/

Adam Wathan:
Writing Your Own Test Doubles
May 11, 2016 @ 10:19:58

In this recent post to his site Adam Wathan about writing your own custom test doubles (fakes) to help make your tests cleaner and improve their overall readability/maintainability.

Once in a while I run into a situation where trying to use a mocking library hurts the readability of my test. For example, say I’m building out a basic user registration flow where someone signs up and receives a welcome email. [...] To test that an account is created correctly, I can make a request to the endpoint and verify that the new account exists in a test database. [...] This covers creating the account itself, but what’s the best way to test the welcome email?

He goes through a few of the options that could be used to test this including using Mockery to replace the mailer class with a spy or actually sending emails. There's downfalls to both of these methods and he suggests using a custom "fake" where the mailer class is swapped out with an "in-memory" option with the same kind of interface. He does point out a few issues with this method, however, and offers a few tips to remember when using them.

tagged: custom test unittest doubles email example video screencast

Link: http://adamwathan.me/2016/01/25/writing-your-own-test-doubles/