Setup integration testing environment



Setup PhpUnit and Composer to be able to test php classes and objects.

 
# 1 > Download PhpUnit

 Create a new folder to download stuff (can be in your home folder), open terminal (in phpStorm: View->Tool Windows->Terminal), create a new folder to download stuff and navigate to your new directory

Now get phpunit with command: curl "https://phar.phpunit.de/phpunit-3.7.38.phar" -O


# 2 > Install PhpUnit

Add permission to execute the .phar folder: chmod +x phpunit-3.7.38.phar

Move to bin directory and rename: mv phpunit-3.7.38.phar /usr/local/bin/phpunit.phar


# 3 > Edit composer.json file

In PhpStorm:

open composer.json and remove the line “ext-crypt: “*”

add “phpunit/phpunit": “3.7.*” in “require-dev” brackets :
"require-dev": {
"phpunit/phpunit": "3.7.*",
"cakephp/debug_kit" : "2.2.*"
}

Be careful about the commas: between brackets, each line needs a comma before the next one. The last line doesn't need commas.


# 4 > Install composer

Go back to your download folder

Run command in terminal:
curl -sS https://getcomposer.org/installer | php

copy to bin directory: mv composer.phar /usr/local/bin/composer


# 5 > Update database

In app->Config->database, Add 2 database configs var $test and var $import.
import connects  to the normal db (this brings in the current tables) and test connects  to db: test_automation
(Note: db test_automation should be created in phpmyadmin)

//for our automation we need to swap db's when locally or online
public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'localTest',
'password' => 'automation',
'database' => 'test_automation',
'prefix' => '',
'encoding' => 'utf8',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);

public $import = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => '127.0.0.1',
//'host' => 'localhost',
'login' => 'local',
'password' => 'undologic',
'database' => 'test_projectBrowser',
'encoding' => 'utf8',
'unix_socket' => '/Applications/MAMP/tmp/mysql/mysql.sock'
);

 


# 6 > Create test_automation db

In phpmyadmin, create db "test_automation"


# 7 > Update Docker

In Docker->sql, create file "import.sql"

-> in import.sql file add the following (This will ensure the next time you start up docker that db will get created )

create database test_automation;
use test_automation; 


# 8 > Add fixture

In app->Test, create folder "Fixture"
-> In Fixture folder, create all fixture files related to all the database tables.

Example: For table name "companies",
-> create a file  "CompanyFixture.php"
-> In CompanyFixture file add the following. ( Note: In $records array, add data related to companies table)
-> Follow the same pattern for all the tables

<?php class CompanyFixture extends CakeTestFixture
{
public $import = array('table' => 'companies', 'connection' => 'import');
var $records = array(
array('id' => '9','created' => '2013-05-28 23:09:26','modified' => '2016-09-14 15:05:21',
'is_active' => '123','user_type_id' => '222','group_id' => '1','rep_id' => '6200',
'credit' => 'Due on receipt','rate' => 'XX.00','name' => 'xxxxxxxxxxxx',
'company' => 'xxxxxxxxxxxx','email' => 'xxxxxxxxxxxx.ca','alternate_email' => 'xxxxxxxxxxxx@xxxxxxxxxxxx.ca',
'password' => 'b039a9','address' => 'xxxxxxxxxxxx','suite' => '',
'city' => 'xxxxxxxxxxxx','province' => 'QC','postalCode' => 'H3X 3A7','country' => 'CA','fax' => '',
'phone' => 'xxxxxxxxxxxx','extension' => '','cell' => '','comments' => '','notes' => '',
'emails_activity' => '','emails_activity_date' => '0000-00-00','emails_process' => '0',
'imap_host' => '','imap_user' => '','imap_pass' => '','imap_encryption' => '')
);

}

 

 

 


# 9 > Setup the unit test folders in phpstorm

cd to project root folder, the project root folder is the base folder of the project, contains all project files and folders. NOTE: in this case, we will consider the “back-end” folder to be the actual root folder

command: “composer install”, it will create a “vendor” folder with a bunch of stuff

move everything from “vendor” to “vendors”

delete the now empty “vendor” directory


# 10 > Add a test in PhpStorm:

Go to app->Test->case->folderOfChoice

Create a new class file in the folder (right-click->new->php class->just enter a name that finishes with "TEST")

The filename must finish with “Test.php”, see picture for reference
It must contain the setUp() function
Each test function name must begin with "test"

App::uses('Controller', 'Controller');
class GenericFileTest extends ControllerTestCase {

public $fixtures = array(

        'app.Company',

        'app.Quote',
       .....add all the other classes as above.
);
//Setup function (copy and paste)
public function setup() {
parent::setup();
}

//Setup generic test function (copy and paste):
public function testSometing() {
$this->assertEquals(true, true, "has to be true");
}
}


# 11 > Run tests in browser

enter www.yourSite.com/test.php

On sites like updateCase, it's updateCase.com/back-end/test.php

Click on a function to run the test


# 12 > Successful test appearance
This green line shows a successful test. A red line means a failure.

Other Instructions

Below are many other instructions that show you how to use your UpdateCase application