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

Laravel News:
Building and Maintaining a Laravel Translation Package
Oct 22, 2018 @ 11:28:08

The Laravel News site has kicked off a series of posts sharing some of the experiences from Joe Dixon about the creation and maintenance of a Laravel translation package.

In this multi-part series, we’ll be documenting the process of building and maintaining an open-source package for Laravel. We will cover everything from bootstrapping the package to dealing with your first issues and pull requests and as much as we possibly can in between.

In this first part of the series he goes over some of the basics:

  • why they're building the tool
  • what they're planning on creating
  • how they're expecting it to be used

There's a bit of background about the tools they're planning on using and pseudo-code of how it will work.

tagged: laravel translation package series part1

Link: https://laravel-news.com/building-laravel-translation-package

Matt Glaman:
Running Drupal's PHPUnit test suites on DDEV
Oct 15, 2018 @ 09:36:29

Matt Glaman has a new post to his site where he walks you through the setup and execution of Drupal's unit tests in the DDEV platform (a Docker-based project that makes it easy to get an environment up quickly).

DDEV is a local development stack built on top of Docker. It gives you all of your environment needs without messy configured on your host machine, without needing to know Docker or configure your own containers. Which is great, and makes life easier. Instead of just using DDEV to develop your site or application locally, why not also run your tests within it?

I have had quite a few people ask me how I configure my setup for testing with Drupal’s PHPUnit test suites. [...] All of these are the same reasons for using a virtual machine or containerized local development stack. So, it is fitting we run our tests within these local stacks as well!

In this article, part one of three, he assumes you already have a DDEV environment up and running with a Drupal application running inside (there's a guide here). With that in place, he shows how to configure PHPUnit via the phpunit.xml file, changing the "SIMPLETEST_*" values for the localhost and local DB connections. He shows how to run the tests by SSHing into the web Docker container and pointing PHPUnit at the configuration file. The end result should look something like this in a terminal.

tagged: tutorial series part1 drupal test unittest ddev docker testsuite

Link: https://glamanate.com/blog/running-drupals-phpunit-test-suites-ddev

Alex Makdessi:
Diving into Symfony's DependencyInjection - Part 1: First steps with the container
Oct 10, 2018 @ 11:53:34

Alex Makdessi has a post on his Medium.com site kicking off a series of posts taking a deeper look into the Symfony DependencyInjection component. In this first part of the series he takes some of the "first steps" with the container.

this article, we’re going to dive into Symfony’s dependency injection component. We will use it from scratch, out of the Symfony framework, with the minimum of external dependencies. You can code along with me while reading this article, each steps will be detailed so you can easily follow.

He starts off the tutorial by creating a new project and including the DependencyInjection (DI) component via Composer (as well as symfony/var-dumper for debugging). He then starts in on the code showing how to make a ContainerBuilder instance to hold the object instance for reuse. With the container created, we now need something to use it. He creates a basic piece of functionality that works similar to the "voters" included with Symfony's security component to determine if a user has a certain role. He first creates this as a stand-alone tool but then refactors it so that the access manager and post voter objects are pulled from the dependency injection container rather than made manually.

tagged: symfony depdendencyinjection part1 series firststeps voter tutorial

Link: https://medium.com/manomano-tech/diving-into-symfonys-dependencyinjection-part-1-first-steps-with-the-container-2fad0593c052

Delicious Brains:
WordPress Deployment Part 4: Automated Deployments
Oct 10, 2018 @ 10:22:29

On the Delicious Brains site they've continued their series covering the deployment of WordPress applications with part four of the series. In this latest tutorial they walk you through the process to set up automated deployments of your WordPress site. In this case, they focus on the use of either the Buddy or DeployBot tools to help automate the process (rather than a roll-your-own solution).

In my last article, we looked at different methods of deploying a WordPress site using Git. We also looked at using an external CI service (such as Travis) to automatically trigger a deployment on your remote git repo.

In this article, we’re going to build on what we have looked at previously and consider some alternative hosted services that can help us with automated deployments for our WordPress site. There are many hosted services that we could use to deploy our WordPress site but we’re just going to look at a few of the bigger ones: Buddy and DeployBot. Unlike Travis, these services are dedicated to automated deployments and offer a lot more functionality and flexibility.

They start off by listing out some of the prerequisites you'll need for your environment including a WordPress site in a Git repository and a working install of Composer. They then walk you through the process to set up the automation in a similar way on each platform:

  • Signing up for Buddy and adding a new project
  • Setting up a new pipeline
  • Configuring the tasks to run
  • Transferring files
  • Connecting it to the production environment

Once all of that it set up, you're ready to run the pipeline and test it out. Screenshots are included for each step of the way to make sure you're in the right place and are getting the settings correct.

tagged: automated deployment wordpress series part4 deploybot buddy

Link: https://deliciousbrains.com/wordpress-workflow-automated-deployments/

Matthias Noback:
Test-driving repository classes - Part 2: Storing and retrieving entities
Oct 08, 2018 @ 14:44:04

Matthias Noback has continued his series of tutorials covering various uses of the Repository design pattern. This is the second part of the series and picks up where part one left of, showing the handling of entities in the repository, performing the usual CRUD operations.

In part 1 of this short series (it's going to end with this article) we covered how you can test-drive the queries in a repository class. Returning query results is only part of the job of a repository though. The other part is to store objects (entities), retrieve them using something like a save() and a getById() method, and possibly delete them. Some people will implement these two jobs in one repository class, some like to use two or even many repositories for this. When you have a separate write and read model (CQRS), the read model repositories will have the querying functionality (e.g. find me all the active products), the write model repositories will have the store/retrieve/delete functionality.

In particular if you write your own mapping code (like I've been doing a lot recently), you need to write some extra tests to verify that the persistence-related activities of your repository function correctly.

He starts with the test cases for the functionality (following the test-drive design mentality) and talks about the expected behavior of the various entity and repository methods. He includes the code for these tests covering state changes, handling child entities, deleting entities, and working with ports/adapters.

tagged: entities repository class tutorial series part2 test

Link: https://matthiasnoback.nl/2018/10/test-driving-repository-classes-part-2-storing-and-retrieving-entities/

Sameer Nyaupane:
PHP Test Driven Development Part 4: Enter The Mock
Sep 27, 2018 @ 12:29:29

On his HackerNoon site Sameer Nyaupane has posted part four of his series covering test-driven development in PHP. In this latest post he covers the use of mocking.

Hey there, welcome to part 4! Today we’ll learn how to mock. Mocking is a process where you create a fake instance of a real class, and test against it. This is so that, you do not have to worry about the real functionality of external dependencies inside a class. This makes unit testing a lot easier and reliable.

[...] Although PHPUnit does have mocking capabilities, it is not as full fledged as that of Mockery’s. We’ll be using Mockery for all our mocking needs.

He starts with some sample code, a simple Math class that calculates the area of a square (but doesn't implement it fully). This includes the need for an instance of a Calculate class that doesn't exist yet. He then works up a test for the Math class, mocking the Calculate class and calling the getArea method to evaluate the result. He walks you through each line of the code, sharing what's happening during test execution.

tagged: unittest mocking tutorial series part4 mockery introduction testdrivendevelopment

Link: https://hackernoon.com/php-test-driven-development-part-4-enter-the-mock-106b4fdedd00

Paragon Initiative:
Slice Of PIE #00
Sep 27, 2018 @ 10:17:01

The Paragon Initiative has a recent post to their blog that's kicking off a new series of posts called "Slice of PIE". In these posts they hope to provide answers in a Q&A style, answering questions commonly asked of them via Twitter or other means.

Astute readers have noticed that our blog posts have decreased in frequency this year. We put a heavy emphasis on quality, not so much on quantity.

At the same time, we field a lot of questions on social media, where our answers (and, sometimes, the questions themselves) are difficult to locate, especially when people close or lock their accounts.

With both of these thoughts in mind, I asked my Twitter followers if they'd be interested in a Q&A-style blog series. I expected maybe a 55:45 split on yes/no responses, but the final tally was overwhelmingly "Yes".

So with that in mind, I'd like to introduce the pilot for our new series, Slice of PIE.

In this inaugural edition they start by sharing how readers can get their questions answered and what kinds of questions will be answered. To kick things off they answer a question about the use of asymmetric encryption and comparing it to hybrid encryption.

tagged: sliceofpie part0 series question answer asymmetric encryption

Link: https://paragonie.com/blog/2018/09/slice-pie-00

TutsPlus.com:
Render Text and Shapes on Images in PHP
Sep 25, 2018 @ 12:36:25

TutsPlus.com has continued their series of posts covering the use of image manipulation functionality in PHP using the GD library. In this latest tutorial they cover drawing shapes in images and rendering fonts.

Besides using GD for manipulating regular images, we can also create our own from scratch. Different functions in the library can be used to draw basic shapes like ellipses, circles, rectangles, polygons, and simple lines. With some maths, these shapes can create nice patterns. There are also functions available to draw text on the rendered image, which opens up a lot of possibilities.

This tutorial will teach you how to draw basic shapes in PHP and how to render text using your favorite font.

The tutorial starts by explaining what functions to use to draw lines, circles, arcs, rectangles, and polygons. Example code is provided showing all of these in action together to create a basic line drawing of a house and sun. They then enhance this with changes of line thickness, color fills and the use of a "brush" created from another image to make a new house picture. The tutorial finishes with a look at how to integrate fonts into the image and insert whatever text you might want (using a true-type font).

tagged: tutorial series image manipulation part3 text shapes

Link: https://code.tutsplus.com/tutorials/rendering-text-and-basic-shapes-using-gd--cms-31767

Matthias Noback:
Test-driving repository classes - Part 1: Queries
Sep 25, 2018 @ 10:28:31

Matthias Noback has kicked off a new series of posts on his site covering the use of the repository design pattern in different situations. In this first post he focuses on "test driving" classes for handing database queries and their results.

A test for a repository can't be a unit test; that wouldn't make sense. You'd leave a lot of assumptions untested. So, no mocking is allowed.

[...] But how do you test everything that is going on in a repository? Well, I found out a nice way of doing so, one that even allows you to use some kind of test-driven approach. In this article I'll cover one of the two main use cases for repositories: querying the database, and returning some objects for it. The other use case - storing and loading objects - will be discussed in another article.

He starts by getting everyone on the same page with a definition of a "query" and how it relates back to a repository class. He then walks through the process of how to test the class, first as a general "get all" query then with a check on the "active" state. Once the test goes green (successful), he adds more variations to both the tests and fixtures. There's not a lot of code examples in this post but it does show some good concepts to get you headed down the right path.

tagged: tutorial repository designpattern query database part1 series

Link: https://matthiasnoback.nl/2018/09/test-driving-repository-classes-part-1-queries/

php[architect]:
Pro Parsing Techniques With PHP, Part Three Using Regular Expressions
Aug 29, 2018 @ 10:55:15

On the php[architect] site they've posted the latest part of their series of magazine excerpts sharing methods for parsing text with PHP. In this third (and last) part the author focuses on the use of regular expressions. This article was originally printed in the magazine's August 2018 edition.

This is the final installment of a set of three articles offering strategies for parsing text with PHP. The first article described the basics of parsing, followed by an article on developing fault tolerant parsing strategies. This article is dedicated to regular expressions.

Regular expressions, or sometimes simply called regex, represent a powerful set of tools which allow developers to split strings, perform character substitutions, and extract text based on matched patterns. The patterns, used in regular expressions, are an actual language that describe combinations of type castings and values that match the text you want to split, substitute, or extract. Regular expressions are an enormously powerful tool for the developer who understands them.

The tutorial starts by walking through some of the basics of what regular expressions are and the functions provided by PHP (using preg_*) to work with them (including basic code examples). It then briefly talks about pattern matching with regular expressions and gives a more specific example showing phone number extraction.

tagged: phparchitect august18 parsing text series part3 regularexpression tutorial

Link: https://www.phparch.com/2018/08/pro-parsing-techniques-with-php-part-three-using-regular-expressions/