Testing Guzzle in Laravel/Lumen with Mock Handler Response

testing guzzle in laravel

Guzzle is a great library for making HTTP calls in PHP. Guzzle can be used in Laravel or Lumen for making HTTP calls to third-party APIs. Especially when you are working with microservices architecture using Lumen framework, Guzzle can be a very good tool for communicating with other services. But when it comes to performing unit tests, calling the actual external services can cause troubles or we just don’t want to call the external APIs when we are running tests. Guzzle has a convenient way of avoiding this by using mock handlers which can be used to mock the responses when you are testing. Combining Guzzle mock handlers with Laravel dependency injection, we can make the unit testing Guzzle clients in Laravel very easy.

For testing Guzzle client with the mock response, a mock handler stack has to be passed to the Guzzle client’s constructor. In order to make this happen only during the testing, we need to make use of Laravel IoC Container. Using this Laravel will do smart dependency injection. For this instead of creating the Guzzle client object in the controller, we have to inject the Guzzle client object through the constructor.

Let’s get into the code.

In the above controller, during normal execution Laravel will create an object of Gizzle client and will inject that into the constructor. Thanks to the intelligent dependency injection.

Now let’s get into testing  this api in Laravel / Lumen

Here we have created a new Guzzle Client object with the mock handler configuration and bound that object in Laravel IoC. Laravel’s dependency injection will do the rest by injecting this object to the controller and giving the mock response when Guzzle call is made inside the controller method.

Password Protect PDF in PHP using mPDF

Creating PDF in PHP is a very common requirement in web apps. mPDF is a great library for creating PDF from any HTML using PHP. Please check the article posted earlier to see how we can use mPDF for generating PDF from HTML. But sometimes we need to password protect the PDF file. mPDF also provides a very simple solution to achieve this.

mPDF has a method called SetProtection() which can be used to protect your PDF in several way including password protection, copy/print protection.

First argument is $permissions which is used to allow permissions like copy, print, modify etc. If null or empty array is passed to the argument user will be able to open the pdf but won’t be able to do anything like copy or print.

Second argument is the $user_password which you can set to make the PDF password protected.


Here is a working example for password protecting the PDF generated using mPDF in PHP.

Hope this will help you in making you dev process easier and make secure PDFs from PHP applications.

Setup Virtual Host on XAMPP in Windows

Why Virtual Hosts?

Virtual Hosts allows us to define our own domain name during the development process. How cool is it to have something like www.myapp.dev instead of localhost/myapp? It is always better to setup up virtual host when you are working on multiple applications. It makes the url much more cleaner during the development and also it will give us different cookie and sessions if we are working on multiple apps at the same time. Setting up a virtual host is very easy with just 2-3 steps involved.

How to Setup Virtual Host on XAMPP in Windows

With setting up virtual host you are no longer required to put your source code inside the xampp/htdocs folder. You can put your source code anywhere you want. For this example I am going to setup my app in D:\myapp.

  1. Open C:\xampp\apache\conf\extra\httpd-vhosts.conf
  2. Add the following block of code to make the default localhost working even after adding our virtual host
  3. Add the following block of code after that.
  4. Open C:\Windows\System32\drivers\etc\hosts in notepad as administrator.
  5. Add the following lines to the file.
  6. Thats All. Now you can open your browser and access your app by entering myapp.dev or www.myapp.dev

Secure WordPress using Jetpack Plugin

When it comes to securing your WordPress site, there are a lot of things can be done. It matters from the admin username to the file permissions. A lot of configurations inside WordPress can affect your security of your WordPress site. There are lots of WordPress plugin which will help you to secure WordPress site.

Here I will be explaining how to use Jetpack plugin by WordPress.com inorder to secure WordPress site.

What is Jetpack Plugin

Dashboard of Jetpack by WordPress.com

To those who are not familiar with Jetpack by WordPress.com, it a plugin developed by the creators of WordPress itself. This plugin is like a collection of plugins which most of the WordPress sites use.

Jetpack simplifies managing WordPress sites by giving you visitor stats, security services, speeding up images, and helping you get more traffic. Jetpack is a free plugin.

Yes it is free!!!

Security Services by Jetpack Plugin to Secure WordPress


Protect is a cloud-powered brute force attack prevention tool. We leverage the millions of WordPress sites to identify and block malicious IPs. Protect tracks failed login attempts across all Jetpack-connected sites using the Protect module. If any single IP has too many failed attempts in a short period of time, they are blocked from logging in to any site with this plugin installed.

Single Sign On

With Single Sign On, your users will be able to log in to or register for your WordPress site with the same credentials they use on WordPress.com. It’s safe and secure.

Once enabled, a “Log in with WordPress.com” option will be added to your existing log in form.


Enabling Manage allows you to update your self-hosted WordPress sites along with any WordPress.com sites you have, all in one simple dashboard.

You can update plugins and themes of any of your WordPress sites from the WordPress.com dashboard. Keeping your plugins and themes up-to date is one of the top priority tasks to secure WordPress site.



Use Has Many Relations with Yii2 GridView

In Yii2 it is very easy to display has many relations for an ActiveRecord with the GridView. This tutorial is aimed to explain how easy it is to display your has many relations with Yii2 GridView in your Yii2 application.

For this tutorial I uses two model classes, User and Post. Here is the table structure for my models.

Then I created the model class using the yii2 gii code generator. In the User model class I had defined relation to Post model as follows

This relation defines that User is having a has many relation to Post based on the userID attribute of Post and id attribute of User.

Now we have the relation defined in the model and we are good to use it in the GridView. To show the posts for a given User I have created a new action named ‘posts’ in the UserController with an argument ‘$userID‘.

First we are loading the ActiveRecord for the user with the given $userID. Then as usual we are passing that ActiveRecord to the view file ‘posts’.

Now add the following lines to the posts view file.

Here we can see we are generating an ActiveDataProvider with its ‘query’ argument populated with the relation. This is all what we have to do to get all the posts for the given User and generate the ActiveDataProvider to populate the GridView with the relation.

Here is the resulting GridView

Let me know if you are having any queries regarding the tutorial. Happy coding.

Install & Using Google API in Yii2 using Composer

Many of us are still going in the process of learning Yii2 and how to use composer with Yii2 to include packages. Today I am explaining about how to install and use Google API in Yii2 using Composer.

Using Composer may seems to be confusing in the beginning, but in reality its not. Composer really makes the library management and dependency checking not at all a headache for the developer.

Lets start with the Tutorial on installing Google API using Composer and using it in your Yii2 Application. First of all we have to install the Yii2 Application using composer. For those who are new you can see the tutorial on installing Yii2 using composer here.

Once your Yii2 Application is installed, you can install the Google API library for PHP using composer by editing the composer.json file in the root folder of your Yii2 application.

You have to modify the composer.json file and add “google/apiclient”: “1.0.*@beta” in the require block. Once you have saved the file you have to open your composer console and run the command composer update. This command will update all the exisiting libraries and also install the latest Google API library in your Yii2 project. You can verify by going into the vendor folder in your root folder and you will see a folder ‘google’ which consists all the library files for Google API.

Now you have the library files within your project and we can start using it in your Yii2 Project. All the libraries installed via Composer is automatically loaded in your Yii2 Application. So there is no need to include new library in your action.

You can simply create objects of your library classes as usual. Since Yii2 is using namespaces you have to added ‘’ before the class name. That is the only change in Yii2 while using a thrid party library.

On a special note I noticed most of the class names given in the Google API documentation is using old class names. Google havn’t updated the official documentation to use the new class name. So do not get panic if you see Class not found error while you use code from the Google API documentation. You can check the new class names in the source file inside vendorgoogleapiclientsrcGoogle folder. The new class names looks similar to the old name. but each words are now separated by underscore.

is now

Hope this article helps you in inegrating Google API in your Yii2 Application. If you are having any queries just let me know via comments.

Install Yii2 Application Tutorial

Install Yii2 Application

In my previous article I explain about some of the major changes in Yii2 Framework. In this article I will guide you to install and setup the configuration web application using Yii2 framework.

One of the new feature in Yii2 is the introduction of Composer.

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

We can install Yii2 framework and application using composer. Using composer is very easy and I insist those who havn’t used composer should start using it. Though if you are not intending to use composer you can download Yii2 and install it the same way as in Yii1. I will first explain how to install Yii2 using composer and then the normal download.

Install Yii2 using composer

You need to install Composer inorder to user composer. If you are on Linux or Mac you can install it by executing the following code

In Windows you can download the executable installer here. Download and install it. When you install you can choose to add menu item to the right click menu.

With Composer installed, you can install Yii by running the following command under a Web-accessible folder:

The above command installs Yii in a directory named basic.

Tip: If you want to install the latest development version of Yii, you may use the following command, which adds a stability option:

Note that the development version of Yii should not be used for production as it may break your running code.

Installing from an Archive File

Installing Yii from an archive file involves two steps:

  1. Download the archive file from Yii 2 with basic application template or Yii 2 with advanced application template.
  2. Unpack the downloaded file to a Web-accessible folder.
  3. Modify the config/web.php file by entering a secret key for the cookieValidationKey configuration item (this is done automatically if you are installing Yii using Composer):


Verifying the Installation

After installation, you can use your browser to access the installed Yii application with the following URL:

Install Yii2 Application
Install Yii2 Application


You can check if the minimum requirements are met using one of the following approaches:

  • Use a browser to access the URL http://localhost/basic/requirements.php


Introduction to Yii2 Framework

What is Yii?

Yii is a high performance, component-based PHP framework for rapidly developing modern Web applications. The name Yii (pronounced Yee or [ji:]) means “simple and evolutionary” in Chinese. It can also be thought of as an acronym for Yes It Is! – Yii PHP Framework 

What is Yii2?

Yii currently has two major versions available: 1.1 and 2.0. Version 1.1 is the old generation and is now in maintenance mode. Version 2.0 is a complete rewrite of Yii, adopting the latest technologies and protocols, including Composer, PSR, namespaces, traits, and so forth.

Hoping you read the above questions and answers let say Welcome to Yii2 Framework. Yii2 was under development for more than a year. As it is mentioned in official documentation, Yii2 is a complete rewrite of Yii1.1. That’s a big change from the developer point of view.

What are the major changes in Yii2?

PHP Version

One of the basic and most important changes in Yii2 is Yii 2.0 requires PHP 5.4.0 or above. Yii2 is built using most of latest features provided in PHP release version 5.4.0.  Make sure your server is running PHP version 5.4.0 or above before choosing Yii2.

Introduction of Namespaces

From PHP version 5.3.0, Namespaces were introduced in PHP programming. Due to the large number of libraries for PHP, the chances of name collision in different libraries became a real problem. Namespaces will help us to remove such problems. Namespaces are just like a folder which groups the files for a library.

Yii2 and Namespaces

Yii2 is built completely using namespaces. So we have to be familiar with using namespace in PHP programming inorder work with Yii2. Here is good tutorial for how to use namespaces in PHP. Once you are ready to work with namespaces you are almost good to start development in Yii2 framework.

Integration with Bootstrap

Like the close integration with jQuery in Yii 1.1, Yii 2 is having a close integration with Bootstrap HTML Framework. Infact all the widgets are modified so that we can customize the html properties of those widgets inorder to match the bootstrap structure. This will be very useful for modern web application development because most of the designs are now based on bootstrap html framework.

Yii2 Basic and Yii2 Advance

Yii2 framework is available in two templates, first one is basic and the other is advanced. Usually most of the projects will require only the basic template unless you really want to use advanced template. Advanced template provide multiple tires of application with individual application files sharing some common configuration. Basically the advanced template will help to create multiple application with sharing some common config such as db configuration. This helps us to create and application for frontend and a separate application for the backend. This is not required for most of the projects.

Structure of Yii 2 Framwork Fles

Unlike with Yii 1, standard installations of Yii 2 results in both the framework and an application skeleton being downloaded and installed. The core framework files are stored in the vendors folder.

Thats all for now. I will be writing more article on how to install amd write hello world application in Yii2 framework soon. So stay followed via Facebook, Twitter and Google

PHP Exponent operator (**) introduced in PHP 5.6

PHP has released its new version 5.6 couple of days before. In the new version a new exponent  operator has been introduced for the exponentiation operation. Till now exponentiation was carried out using a built-in function pow in PHP. From PHP 5.6 this can be carried using double airstrike (**) to execute exponentiation. The new exponent operator in php is a very handy way to write code than calling a function as required now.

Before using Exponent Operator (**) in PHP

If the new exponent operator (**) is used in PHP versions below 5.6, it will throw a parse error Parse error: syntax error, unexpected ‘*’. So make sure your PHP version is 5.6 before you start using  the new operator for exponentiation in PHP.