Dieses Blog durchsuchen

Donnerstag, 4. August 2016

phpunit simple tests with selenium server on ubuntu

Today we want to add selenium to our phpunit testcases.

for that we need:

1) composer installed (ubuntu)
http://magento2-tuts.blogspot.de/2016/08/ubuntu-install-composer.html

2) phpunit (ubuntu)
http://magento2-tuts.blogspot.de/2016/08/ubuntu-install-phpunit.html 

3) selenium installed (ubuntu)
http://magento2-tuts.blogspot.de/2016/08/install-selenium-rc-server-2-3-on.html


It is possible to use chrome or firefox in your testcases. IE is excluded for now


lets start:

create following structure:
myproject
|__app
    |__test
   

add a file "phpunit.xml" under "myproject"
 
this tells phpunit to use the composer autoloader, to do the loading

add file "composer.json" under "myproject" with folling content
 
This will tell composer, wich dependencies to use.

build dependency from a terminal
$ composer install



Add a testclass named GitHubTest.php under "app/test" with following code.


To explain the code above.

At first we are using the facebook testlibrary namespaces to get all the facebook webdriver stuff

setUp()
In the method "setUp()" we are configuring our selenium webdriver and tell the test to use firefox on seleniumhost on port 4443. The port is defined in your selenium startscript under /etc/init.d/selenium

tearDown()
Here we shutdown the current selenium driver instance.

testGitHubHome()
This is the method where we are doing the testing. We are simlly opening "http://github.com" and check the windowtitle for occurence of the word "Github".

Thats it.







selenium: fix problems with firefox 47 on ubuntu

Till webdriver version 0.9.0 its not possible to get a connection to firefox in your testcases.

The solution is to update to gecko.driver version 0.10.0
https://github.com/mozilla/geckodriver/releases

simply replace your old driver and restart selenium.

If that does not help update selenium rc to version 3.x. (Thats my current version)



phpunit: sample configfile phpunit.xml to autoload dependencies

If you are using phpunit,  its good not to require phpunit or your own libraries in your testcases everytime.

You can use the composer autoloading to do that.

All you need is phpunit in the "vendor" folder of your application and composer installed.

If you do not have composer installed, follow this tutorial
http://magento2-tuts.blogspot.de/2016/08/ubuntu-install-composer.html


If you do not have phpunit installed you can follow this totorial
http://magento2-tuts.blogspot.de/2016/08/use-composer-to-install-phpunit.html


now you can simply add a file "phpunit.xml" in your projectroot with following content:
This will tell phpunit, to use the autoloading from "vendor/autoload" where all external libraries are included.


Now you can switch to your terminal and go tho the projectroot and type:

phpunit


Thats it. All your dependencies will be required automaticly and your testsuites in the folder app/test will be executed




use composer to install phpunit

if you have composer already installed an adde to your project you can simply add phpunit via terminal.

open a terminal and swich to your projectroot wher composer.json is stored:
type:
composer global require "phpunit/phpunit=5.4.*"
sudo ln -s  ~/.composer/vendor/phpunit/phpunit/phpunit   /usr/bin/ 


Composer will add phpunit into your vendor folder

ubuntu: create a simple php autoloader with composer

Here is a simple example how to setup a simple autoloader with composer classmap.

1) install composer:
http://magento2-tuts.blogspot.de/2016/08/ubuntu-install-composer.html

2) create 2 classes in 2 folders.
- create a folder named "myproject"
- in this folder create a folder "app" and a folder "library"
- so that you have this structure:
--myproject
  |__app
  |__library
      |__Calculator.php

In the app/library folder create a php Class "Calculator.php"

create autoloader
- in the "myproject" folder create a file "composer.json" with follwing content


Switch to your terminal to the folder, where your composer.json is stored. type:
php composer install

After that a "vendor" folder is generated with the autoloading files.
Every Classes which are stored in the classmap ("app/library") will be automaticly added to the autoloading classmap.


Create a file "index.php" in folder "myproject" and add folling content,:

You now can use the calculator class without require or include like that:

Switch to terminal and type:
php index.php

this will output the result of 2+2 in yout terminal. You see the class Calculator gets automaticly loaded.


Now lets add a new folder "models" to the classmap
- create a folder named "models" in the folder "myproject"
- create a php class "User.php" in that folder

Now add the new folder "models" to the composer.json classmap
- open composer.json and add "app/models" to the classmap, so that it looks like that:

- switch back to your terminal in the folder where composer.json is stored and type:
php composer dump-autoload


If you open vendor/autoload_classmap.php you will see the new addition of the Usermodel.


Thats it.

In one of the next tutorials, PSR 0 and PSR 4 will be explained.



Mittwoch, 3. August 2016

ubuntu: install phpunit

This i an easy task:

open up terminal:

$ wget https://phar.phpunit.de/phpunit.phar

$ chmod +x phpunit.phar

$ mv phpunit.phar /usr/local/bin/phpunit
 
 
After that phpunit is available on your terminal. 

composer: install and add it permanently to console on ubuntu 16.04

We want to download and install composer, so that it is available on your terminal permanently

If you do not have curl installed
apt-get install curl
 
Copy folling commands to your terminal for the current user
This installs composer  and adds it to the users bashrc

thats it!

Now composer works everywhere on your console
- open console and type:
composer