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

TJ Miller:
Separate Interactive Test Suites
Mar 26, 2018 @ 17:56:24

In a post to his Medium.com site TJ Miller has a quick post for the PHPUnit users out there showing how to isolate your tests and prevent them from interacting by splitting them into different test suites.

On a recent Full Stack Radio episode Adam Wathan and Taylor Otwell were talking about testing Laravel applications. During the episode, they spoke about isolating interactive integration tests from your normal testing e.g. payment gateways, third-party integrations.

TJ shares an example of a project he's working on where this is useful: avoiding interactions with an HTTP API every time the tests run. He then shows how, with a single PHPUnit configuration, you can split up the tests by name and directory to prevent them all from executing every time. Then the "testsuite" option can be used to isolate the execution from the command-line. An example of the XML configuration is also included in the post.

tagged: tutorial phpunit separate test suite isolation configuration

Link: https://medium.com/@sixlive/separate-interactive-test-suites-f6fd59316ec2

Anna Filina:
Testing Legacy PHP Scripts
Jan 30, 2018 @ 17:56:23

Anna Filina has a quick post to her site with some recommendations around testing legacy PHP scripts giving an example of a challenge to test a controller in isolation from the rest of the application.

I gave myself a challenge: to test a legacy "controller" in isolation, yet with minimal impact on the original code.

She starts with the example code she'll be testing and then works through the steps to effectively test it:

  • isolating it from the other functionality in the application
  • mocking a statically called method
  • requiring necessary files
  • executing the controller under test

The post ends with the test class she created showing how to evaluate the result of a call with one invoice in the billing system. She makes one comment at the end to answer the question "why not just refactor" but points out that, especially in larger legacy applications, that's just not always an option.

tagged: testing legacy script tutorial isolation mock unittest phpunit

Link: https://afilina.com/testing-legacy-php-scripts

Scott Mattocks:
I is for Isolation
Jan 16, 2013 @ 18:02:23

Scott Mattocks has posted the next "letter" in his LUCID development series (more here) - the I is for Isolation:

Applications are a collection of individual components; they pull together the contributions of individuals to create something which is greater than the sum of its parts. If one piece breaks down, and the system is not prepared to put it in quarantine, the entire application can come crashing down. [...] The best way to stave off this infection is to prevent it at the source. Keep the application happy by making it more resistant to implosion.

He talks about the "code jerks" of your application that hog resources or functionality and cause general problems for the application as a whole. He notes that sometimes it's the result of a third party, but it can also be internal (and more difficult to find). He gives a more specific example of a database wrapper class and some suggestions of things to do as failover (ex. used cached data or read from a secondary).

tagged: lucid development principles isolation series

Link:

Brian Swan's Blog:
SQL Server Driver for PHP Connection Options: Snapshot Isolation
Sep 15, 2010 @ 17:21:54

Following up on his previous post about transaction-level isolation with the SQL Server driver, Brian Swan has this new look at an additional option - snapshot isolation.

I’ll cover the snapshot isolation level in this post, and (as in my earlier post) I'll look at what happens at the database level when using a particular isolation level (rather than concentrating on PHP data access code).

He introduces snapshots for those not familiar with them - the idea of making a set of data available to other users without having to lock the database in a certain state (also called row versioning). He shows how to turn on the isolation functionality and set it to work with a transaction. With the snapshot created, he shows the SQL needed to fetch the snapshot of data and, of course, how to read it in PHP with the help of the SQL Server driver.

tagged: sqlserver driver isolation snapshot tutorial

Link:

Brian Swan's Blog:
SQL Server Driver for PHP Connection Options: Transaction Isolation Levels
Sep 09, 2010 @ 18:53:32

On his blog today Brian Swan has posted the results of some of his research on transaction isolation levels in SQL Server PHP applications.

Last week I had the good fortune of presenting at the the Seattle PHP Meet Up on a topic I knew only a little about: transaction isolation levels. It was fun doing the homework to learn more and I'll share what I learned in this post. This post is only somewhat PHP-specific. I'll focus largely on the concepts behind database transactions and isolation levels. Most of the concepts I'll look at are database agnostic (although I will use SQL Server as the vehicle for explaining the concepts) and are supported by most relational database management systems out there (e.g. SQL Server, MySQL, Oracle, DB2, etc.).

He introduces database transactions for those not familiar (a key to understanding the rest of the post) and some PHP code that connects to a SQL Server and begins a transaction to update some banking information. He uses this example to show how to set up isolation levels like "READ UNCOMMITTED", "READ COMMITTED" and "SERIALIZABLE".

tagged: transaction isolation level sqlserver driver tutorial

Link:

Jens Schauder's Blog:
One Database for Every Developer
Aug 24, 2010 @ 18:08:26

Jens Schauder has an interesting suggestion about the development environment for your projects - one database per developer.

How many databases run in your teams development environment? One for the complete team? I have seen many places like that, but please tell me: Why? You aren’t working on a shared files system aren’t you? How is a developer supposed to change the structure of that database without interrupting the work of the other developers? And just in case you haven’t noticed: Pretty much every RDBMS vendor offers free versions of their database which can run on a developer machine.

He suggests that every developer needs their own isolated database instance where they can run rampant through the data and only cause any real issues (or correct major ones) without having to worry about the impact on others. Keeping them in sync is a whole different story, but can be helped with something like fixture data.

tagged: database developer opinion isolation instance

Link:

Matthew Turland's Blog:
Process Isolation in PHPUnit
Aug 20, 2010 @ 14:26:41

Matthew Turland has a new post today about isolating tests in PHPUnit testing to try to avoid overlap in the resources each needs.

One requirement of the test suite was that some test methods had to be run in a separate process since class declarations reside in the global scope and persist until the process terminates. So, I slapped a @runInSeparateProcess annotation in the docblock of a test method with that requirement, ran the test suite… and watched that test method fail because the class was still being declared.

After come close examination of his code and the PHPUnit source, he found the issue - a misunderstanding of how the preserveGlobalState property should be set. For it to work as he wanted, it needed to be set before the run() method was called for test execution. To correct the issue in his code, he overrides the run() method to set the value and call the parent.

tagged: process isolation phpunit unittest preserveglobalstate

Link:

Sebastian Bergmann's Blog:
The Cost of Test Isolation - Follow-Up
Jan 20, 2009 @ 17:11:44

Adding on a bit more to a previous post of his look at test isolation (ex. global variables from one test do not effect any others') with an update he's made to the PHPUnit code concerning the isolation.

Since the previous posting, I have added a backup/restore mechanism for static attributes of classes to PHPUnit. This is yet another feature of PHPUnit that makes the testing of code that uses global state (which includes, but is not limited to, global and superglobal variables as well as static attributes of classes) easier.

Two graphs illustrate the difference - one showing a normal run and another with this new feature in use and showing off the performance increase it can give.

tagged: test isolation phpunit xdebug backup restore static attribute

Link:

Sebastian Bergmann's Blog:
The Cost of Test Isolation (and other PHPUnit Features)
Nov 27, 2008 @ 19:04:30

Sebastian Bergmann as a new post talking about a few of the features of the PHPUnit unit testing framework, especially test isolation.

Some of PHPUnit features come with the cost of a performance penality. This posting explores the effect of the --no-syntax-check, $backupGlobals = FALSE;, and --coverage-html options.

He compares benchmarks for a test with few different settings - $GLOBALS, syntax check and code coverage (including graphical output of the execution times on the various parts of the tests).

tagged: test isolation phpunit feature setting output graphic

Link:

PHP in Action Blog:
Testing Smarty templates
Jan 29, 2008 @ 14:47:00

On the PHP in Action blog today, there's a quick new post that talks about not only using the Smarty templating system but also a method for testing it to check for any kind of possible failure.

As I mentioned in my blog post on Paparrazzi testing, Uncle Bob (Robert C. Martin) has discussed how to test web templates or server pages. Since I'm currently working with Smarty templates, I wanted a simple way to run tests on them without needing to deal with a web server and the page navigation in a full web application.

Thankfully, he's found a nice, simple method for running tests against the Smarty engine (and includes that in the post - an interface to Smarty and the example test case to run against it).

tagged: smarty unittest simpletest template fail isolation interface

Link:


Trending Topics: