PHPUnit with CakePHP and Yii

18
Unit Testing In PHP Quality is never an accident; It is always the result of intelligent effort.

description

presentation for how we can integrate PHPUnit with php

Transcript of PHPUnit with CakePHP and Yii

Page 1: PHPUnit with CakePHP and Yii

Unit Testing In PHPQuality is never an accident; It is always the result of intelligent effort.

Page 2: PHPUnit with CakePHP and Yii

About Speakers

Hrishikesh Raverkar● A Geek● A haPHPy’er● Full Stack Engineer

hrishi4u hrishithegreat

Madhavi Ghadge-Kumbhar● A Technology Enthusiastic● A haPHPy’er● Love to work in cross platform

madhavi.ghadge madhavighadge

Page 3: PHPUnit with CakePHP and Yii

Why Unit testing

● It's a great idea!

● Makes you confident about impact

● Helps other developers understand

code onboarding easy

● Speeds up future development

process.

● It will force you to think things such

as "how am I going to test this?"

● Lets you sleep peacefully at night ;)

Lots of methodologies have come and gone, paradigms have changed but the requirements are always the same; Make it good and make it fast.

Page 4: PHPUnit with CakePHP and Yii

Unit Testing Tools

● Php Unit: PHPUnit is a package of xUnit family(JUnit, NUnit, CppUnit, PyUnit). ● Amock : Amock is a mock object library written in PHP 5, inspired by

EasyMock.

● SimpleTest : Unit testing, web testing and mock objects framework. Community support is not available any more

● Snap Test : SnapTest is a powerful unit testing framework. Simplify the unit test process without sacrificing the agility tests provide.

Page 5: PHPUnit with CakePHP and Yii

Why PHPUnit

● It is the standard.

● PHPUnit is integrated in every PHP IDE and gives required reports.

● Now coming with new releases of all frameworks.

● Works fine with every continuous integration

● Actively maintained, stable and works great for every codebase, every

scenario and every way you want to write your tests.

● Provides better code coverage reports.

One Framework with all facilities to get unit testing done

Page 6: PHPUnit with CakePHP and Yii

Installation

Traditional way - PEAR ● Pear channel-discover pear.phpunit.de● Pear install phpunit/package_name

The New Way - PHAR or Composer http://phpunit.de/manual/current/en/installation.html

One Framework with all facilities to get unit testing done

Page 7: PHPUnit with CakePHP and Yii

Ways to do Unit testing using PHPUnit

● Assertion● Data Provider● Exceptions● Fixtures(setUp() and tearDown())● Database testing

Page 8: PHPUnit with CakePHP and Yii

Assertion

A predicate (a true–false statement) placed in a program to indicate that thedeveloper thinks that the predicate is always true at that place.

We asserted that true will assert to true ( if (true == true) ). There is no magichere, what you see is what you get with assertions.

If we assert that false is true ( if (false == true) ), we would get a failing test

Page 9: PHPUnit with CakePHP and Yii

<?php class AssertsTest extends PHPUnit_Framework_TestCase {

public function testTrueIsTrue(){ $foo = true;

$this->assertTrue($foo);}

}?>

Page 10: PHPUnit with CakePHP and Yii

Data Provider

A test method can accept arbitrary arguments. These arguments are to be provided by a data provider method

Page 11: PHPUnit with CakePHP and Yii

<?php class DataTest extends PHPUnit_Framework_TestCase {

/** @dataProvider additionProvider*/ public function testAdd($a, $b, $expected) { $this->assertEquals($expected, $a + $b); } public function additionProvider(){ return array( array(0, 0, 0), array(0, 1, 1), array(1, 1, 3)); } }

?>

Page 12: PHPUnit with CakePHP and Yii

Exceptions

By default, PHPUnit converts PHP errors, warnings, and notices that are triggered during the execution of a test to an exception. Using these exceptions, for an instance, you can expect a test to trigger a PHP error

Page 13: PHPUnit with CakePHP and Yii

<?phpclass ExceptionTest extends PHPUnit_Framework_TestCase { /** * @expectedException InvalidArgumentException */ public function testException(){ $this->setExpectedException('InvalidArgumentException'); }}?>

Page 14: PHPUnit with CakePHP and Yii

Fixtures A test fixture is all the things that must be in place in order to run a test and

expect a particular outcome.

Four phases of a test:Set up -- Setting up the test fixture.Exercise -- Interact with the system under test.Verify -- Determine whether the expected outcome has been obtained.Tear down -- Tear down the test fixture to return to the original state.

Page 15: PHPUnit with CakePHP and Yii

<?php class StackTest extends PHPUnit_Framework_TestCase{ protected $stack; protected function setUp() { $this->stack = array(); } public function testEmpty(){ $this->assertTrue(empty($this->stack)); } public function tearDown(){ print "\nMySuite::tearDown()"; }}?>

Page 16: PHPUnit with CakePHP and Yii

Database Testing

The four stages of a database test● Clean-Up Database● Set up fixture● Run Test, Verify outcome ● Teardown

Page 17: PHPUnit with CakePHP and Yii

Test Driven Development with...

Page 18: PHPUnit with CakePHP and Yii

Thank You!

@webonise weboniselab