Mockery or PHPUnit

Test doubles are not new thing for php developers. When it comes to mocking there are clear 3 choices at the moment, assuming that phpUnit is used for testing.

1. PhpUnit Mock (Default)
2. Mockery
3. Phrophesy

I have not got much experience with phrophesy. But it’s more a behavioural testing tool for bdd style of development than a testing tool.

Everzet, the man himself, provides amazing little blog about it here. Please read.

Among remaining two, I use Mockery for few reasons.

1. Easy to mock

Mockery::mock('My\Amazing\Class');

or

Mockery::mock('My\Amazing\Interface');

2. Mocking with Chain and Fluent Interface

I will take an example from Mockery doc.

Code: $object->foo()->bar()->selfDestruct();
MockeryMock: $mock->shouldReceive('foo->bar->selfDestruct')->andReturn('Ten!');

A real life example would be:

if $this->securityContext->getToken()->isAuthenticated(){}

How do you do this with phpUnit?

i) You mock bar.

ii) Mock foo

I assume it uses DI in both levels

iii) Mock $object

3. Calling of a method more than once with different parameters

Lets take an example of code below.

$object->addSomething('thing 1')
->addSomething('thing2);

With Mockery:
$mock->shouldReceive('addSomething')
->with('thing1)
->andReturn($mock);
$mock->shouldReceive('addSomething')
->with('thing2)
->andReturn($object);

WithPhpUnit:
You get error if you stub a method twice. Umm…..

4. Mocking Final Methods Proxy

Read here.

Swahaa!! for now!!!

 

Posted in BDD and TDD | Leave a comment

Docker setup using docker-compse with CentOS6.8 Nginx PHP7.0

Why NOT CentOs7.0?

At this time of writing this, there is an issue with Docker base repo available for CentOs 7.0 has some issue with  PHP7.0. Hopefully in the near future, that will be fixed too.

Docker Compose:

If you want to know what docker-compose is, please read here.

What Happens here?

  • We use Version 2 of docker-compose
  • We have two services called web and php-fpm
  • The port 80 of Web is exposed as 8080 for host os.
  • Nginx configuration is copied to /etc/nginx/conf.d
  • web container has link to php-fpm container. With docker-compose 2, you can use network option too do so…
  • php-fpm has 9000 port exposed.
  • Volums, all the files/directories that are mounted to the container.

What’s in ./container/centos-docker/nginx/centos68/Dockerfile ?

FROM centos:centos6

RUN yum -y update; yum clean all
RUN yum -y install epel-release; yum clean all
RUN yum -y install nginx; yum clean all
RUN echo “daemon off;” >> /etc/nginx/nginx.conf
RUN echo “nginx on CentOS 6 inside Docker” > /usr/share/nginx/html/index.html

EXPOSE 80

CMD [ "/usr/sbin/nginx" ]

What’s in ./container/eght/php7/Dockerfile ?

[sourcecode language="csharp"]

FROM centos:centos6.8

RUN yum install -y epel-release
RUN rpm -Uvh http://rpms.famillecollet.com/enterprise/remi-release-6.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el6/latest.rpm
RUN rpm -Uvh http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm

RUN yum install -y php70w \
php70w-xml \
php70w-mbstring \
php70w-json \
php70w-gd \
php70w-mcrypt \
php70w-fpm \
php70w-pdo

EXPOSE 9000

CMD ["php-fpm", "-F"]

[/sourcecode]

What’s on site.conf ?

[sourcecode language="csharp"]
server {
listen 80;
index index.html index.php;
server_name test.local;
error_log  /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html/backend;

location ~ \.php$ {
#try_files $uri =404;
#fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}

[/sourcecode]

What’s on php-fpm.conf?

[sourcecode language="csharp"]

[www]
user = apache
group = apache
;;;This is the main change that is done from original file, so it’s not restricted to certain ip..
;;; Best to take the original file and to make those changes..
listen = 0.0.0.0:9000

;;And the line below was commented out
;listen.allowed_clients = 0.0.0.0

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35

catch_workers_output = yes

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path]    = /var/lib/php/session
php_value[soap.wsdl_cache_dir]  = /var/lib/php/wsdlcache

[/sourcecode]

Thats it..

Now check the browser test.local:8080 and see if index.php file is executed…

Note: Tested in Ubuntu 16.04 as host..

Posted in Docker | Leave a comment

Docker Commands

Once Updated the config:

docker-compose stop && docker-compose rm -vf && docker-compose up -d –build

List Processes:

docker ps -a

Bash Login to a container:

docker exec -it NameOrIdOfTheContainer bash

Inspect the Network:

docker network inspect nameofnetwork

Note: Name of network is found out by running “network ls” command

View the Network:

docker network ls

Run any command on the container:

Example: docker exec -it 88 mysql -uadmin -p

View any logs:

docker logs NameOrIdOfContainer

 

Posted in Zend Framework | Leave a comment