PsySH – REPL for PHP

PsySH is a great interactive debugger/REPL for PHP. I have used it for quick prototyping and instant debugging as shown below.

  1. Playing with classes in console:


  1. Debugging an unit test:


I have uploaded the sample code on github



Single Responsibility Principle

This is episode 1 on my SOLID design principle series. Single Responsibility Principle is the S of SOLID principles.

Uncle Bob says,

A class should have only one reason to change

In case of module or class it should concern about a single responsibility, and in case of method or function it should do a single thing.

Unix commands are best example of this principles. ls command will list directory contents.

In OOP we often put everything under a class. For example lets say we have a Bookclass that has title, author and current_page properties.

In the Book class we can see it has all sort of rendering logic in it, it seems fine at first glance but if we look carefully why a plain Book class have to know about all those rendering logic, what if we need another type of rendering tomorrow ? should we just go ahead and change this class ? it seems dual responsibility.

Instead we can extract the rendering logic into its own class, first lets define a RendererInterface that declares a render method.

Now we can implement it for text and web rendering as follows.

and the client would use it like

This is a very basic example of how we can separate responsibilities around classes, one thing to note that it would require to add more classes but it would be slim! so the takeaway is

many thin classes are better than one big fat class

Thats all I have for now!



Test doubles: Dummy, Stub, Spy, Mock

Today I am going to write about Test doubles! Its always confusing when to use stub or mock. Lets first define different kind of test doubles.

N.B. I am taking examples from Uncle Bob’s Little Mocker but translating it into PHP way.


Dummies are used in tests when we need to provide an instance as an argument to create an instance of SUT (System Under Test) but its never going to be used in the test.

Suppose we have an interface Authorizer that has a method authorize as follows:

and an Authorizer class implements the Authorizerinterface

now we want to test loginCount method of System class:

It seems System class requires an instance of AuthorizerInterface to create a System instance, so instead of using a real Authorizer we can use a DummyAuthorizer here.

so in the test method we can use it as follows:

if we look carefully into the test, we only want to test the loginCount method and it does not matter how the System instance was created as long as we are providing an instance of AuthorizerInterface.


Say we want to test a SUT method that internally make a call to DOC method but and we dont care what it does but to return a specific value, we need to create a stub.

Here is an example

other scenario would be:

  1. The test method is doing an HTTP API call but we dont actually need to any real call, we can stub it out and return a custom response.
  2. We want to test a part of a system that requires a logged in user, create a user stub and return true for that particular method.


Use Spy for spying, say you stub a method how do we know that stubbed method was actually get called in the test ?

Here is a an example

So at the end of the test we can assert if $authorized_was_called is true. We can do a lot more in Spy, we can check the arguments, how many times it get called and so on.


Now lets talk about Mock! finally! If we look carefully in the Spy we were asserting something that is not a SUT thing but a DOC thing, by accessing it outside the Spy class we are tight coupling it with the tests which makes it vulnerable, what if we move the assertion inside the Spy ? you got it, thats how we create a Mock and thats why we have Mocking frameworks like Phake or Mockery


Testing with some real objects or modules that are not going to be used in production, example SQLite db would be preferable for tests that a MySQL db. Keep in mind that Fake has real business behavior, as Uncle bob said

Mock is a kind of spy, a spy is a kind of stub, and a stub is a kind of dummy. But a fake isn’t a kind of any of them. It’s a completely different kind of test double.

Hope this will remove some of the confusion about Test doubles!

Anonymous function in PHP

Anonymous function (aka Closure, Lambda) in PHP are very useful, it allows us to do functional programming (well in a sense we can create higher order function ).  Here are some usages of anonymous function.

1. Create a Lambda function:

Anonymous function does not have any name but we can assign it to a variable.

2. Use as function argument:

We can use anonymous function as function argument, php also provides a callable type hint that we can use. Here is an example to create a decorator using anonymous function.

3. Use as function return value:

We can also use anonymous function as function return value, welcome to function currying.

4. Built-ins:

Many built-in array functions accepts callable ex. array_filterarray_map, array_reduceusort etc


Is testing part of QA or QC ?

In software engineering, testing plays a vital phase and in many occasion we use Quality Assurance (QA) and Quality Control (QC) interchangeably. and I was getting it wrong until today (while attending in a session conducted by Pankaj Rabra) to know that testing is a part of QC not QA!

QC is about the product. To check if the product meets the requirements or not. It includes reviewing the requirements, code, design.

QA is about the process through which the product is going to built. Is the team following a right process in all development life-cycle.