Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 39 additions & 39 deletions 01StackTest.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<?php
/**
* This class is to just give you a glimpse that how phpunit works.
* For writing any test you need to so following things
*
* 1. Write a class which ends with 'Test' and should
* extends PHPUnit_Framework_TestCase class
* 2. After that write and test functions which should start with 'test'
* 3. A test class could have multiple test functions
* 5. You can write other supporting functions also to be
* used in test functions but don't start their names with 'test'.
* Otherwise they will be considered as test functions.
*/

class StackTest extends PHPUnit_Framework_TestCase
{
/**
* This method will be used to test
* count() function in php, Its a very
* simple test and multiple assertEquals
* method are used. Fire it by
*
* $ ./bin/phpunit --colors 01StackTest
*/

public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));

array_push($stack, 'foo');

$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));

$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
<?php
/**
* This class is to just give you a glimpse that how phpunit works.
* For writing any test you need to so following things
*
* 1. Write a class which ends with 'Test' and should
* extends PHPUnit_Framework_TestCase class
* 2. After that write and test functions which should start with 'test'
* 3. A test class could have multiple test functions
* 5. You can write other supporting functions also to be
* used in test functions but don't start their names with 'test'.
* Otherwise they will be considered as test functions.
*/
class StackTest extends PHPUnit_Framework_TestCase
{
/**
* This method will be used to test
* count() function in php, Its a very
* simple test and multiple assertEquals
* method are used. Fire it by
*
* $ ./bin/phpunit --colors 01StackTest
*/
public function testPushAndPop()
{
$stack = array();
$this->assertEquals(0, count($stack));
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertEquals(1, count($stack));
$this->assertEquals('foo', array_pop($stack));
$this->assertEquals(0, count($stack));
}
}
?>
118 changes: 59 additions & 59 deletions 02DependencyTest.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
<?php
/**
* This class demonstrate the a lot of tests could be dependent on each others
* and they could use the return value of each function executed as the input of another.
*
* If any of the function failed to give the return value, if it get failed
* all the tests below will be skipped.
*
* This test is anyway going to be passed but the failure example is covered
* in 03DependencyFailureTest.php
*/

class DependencyTest extends PHPUnit_Framework_TestCase
{
/**
* This is a simple test but it returns a value also.
* Multiple functions could be dependent on this function.
*
* @return array
*/
public function testEmpty()
{
$stack = array();
$this->assertEmpty($stack);

return $stack;
}

/**
* This method is also a test method but its execution depends upon
* the value returned by testEmpty method. If testEmpty method report
* any error this test will never be fired
*
* @param array $stack This is an array return by testEmpty method
* @depends testEmpty
*/
public function testPush(array $stack)
{
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertNotEmpty($stack);

return $stack;
}

/**
* This method is also a test method but its execution depends upon
* the value returned by testEmpty method. If testEmpty method report
* any error this test will never be fired
*
* @param array $stack This is an array return by testEmpty method
* @depends testPush
*/
public function testPop(array $stack)
{
$this->assertEquals('foo', array_pop($stack));
$this->assertEmpty($stack);
}
}
<?php
/**
* This class demonstrate the a lot of tests could be dependent on each others
* and they could use the return value of each function executed as the input of another.
*
* If any of the function failed to give the return value, if it get failed
* all the tests below will be skipped.
*
* This test is anyway going to be passed but the failure example is covered
* in 03DependencyFailureTest.php
*/
class DependencyTest extends PHPUnit_Framework_TestCase
{
/**
* This is a simple test but it returns a value also.
* Multiple functions could be dependent on this function.
*
* @return array
*/
public function testEmpty()
{
$stack = array();
$this->assertEmpty($stack);
return $stack;
}
/**
* This method is also a test method but its execution depends upon
* the value returned by testEmpty method. If testEmpty method report
* any error this test will never be fired
*
* @param array $stack This is an array return by testEmpty method
* @depends testEmpty
*/
public function testPush(array $stack)
{
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertNotEmpty($stack);
return $stack;
}
/**
* This method is also a test method but its execution depends upon
* the value returned by testEmpty method. If testEmpty method report
* any error this test will never be fired
*
* @param array $stack This is an array return by testEmpty method
* @depends testPush
*/
public function testPop(array $stack)
{
$this->assertEquals('foo', array_pop($stack));
$this->assertEmpty($stack);
}
}
?>
102 changes: 51 additions & 51 deletions 03DependencyFailureTest.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
<?php
/**
* This class is an example of what happened when a function on which other
* functions are dependent get failed
*
* Please see the results by running it
* Result Expected is:
*
* FAILURES!
* Tests: 1, Assertions: 1, Failures: 1, Skipped: 2.
*/

class DependencyFailureTest extends PHPUnit_Framework_TestCase
{

/**
* This method is written to fail. testTwo() and
* testThree() functions are dependent on this function.
* If this function get failed all the dependent function
* should fail.
*
*/
public function testOne()
{
# This should fail :)
$this->assertTrue(FALSE);
return array();
}

/**
* This test will never get executed because the function
* it depends will not get passed
*
* @depends testOne
*/
public function testTwo($arr)
{
}

/**
* This test will also never get executed because the function
* it depends will not get passed
*
* @depends testOne
*/
public function testThree($arr)
{

}
}
<?php
/**
* This class is an example of what happened when a function on which other
* functions are dependent get failed
*
* Please see the results by running it
* Result Expected is:
*
* FAILURES!
* Tests: 1, Assertions: 1, Failures: 1, Skipped: 2.
*/
class DependencyFailureTest extends PHPUnit_Framework_TestCase
{
/**
* This method is written to fail. testTwo() and
* testThree() functions are dependent on this function.
* If this function get failed all the dependent function
* should fail.
*
*/
public function testOne()
{
# This should fail :)
$this->assertTrue(FALSE);
return array();
}
/**
* This test will never get executed because the function
* it depends will not get passed
*
* @depends testOne
*/
public function testTwo($arr)
{
}
/**
* This test will also never get executed because the function
* it depends will not get passed
*
* @depends testOne
*/
public function testThree($arr)
{
}
}
?>
90 changes: 45 additions & 45 deletions 04DataProvider.php
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
<?php
/**
* This example show that how we can write functions which will act as
* a data providor for another test.
*
* The providor can send data in a array if we have to send multiple variables
* and it may send multidimentional array to feed multiple values to assert.
*/

class DataTest extends PHPUnit_Framework_TestCase
{
/**
* This function will get data from its data providor function 'providor'.
* The function name should be mentioned in the tag 'dataProvider' preceeded
* by its function name
*
* @dataProvider provider
*/
public function testAdd($a, $b, $c)
{
$this->assertEquals($c, $a + $b);
}

public function provider()
{
$return1 = array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);

$return2 = array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 2)
);

$return = $return1;
//$return = $return2;
//Uncommenting this line will pass this test
return $return;
}
}
<?php
/**
* This example show that how we can write functions which will act as
* a data providor for another test.
*
* The providor can send data in a array if we have to send multiple variables
* and it may send multidimentional array to feed multiple values to assert.
*/
class DataTest extends PHPUnit_Framework_TestCase
{
/**
* This function will get data from its data providor function 'providor'.
* The function name should be mentioned in the tag 'dataProvider' preceeded
* by its function name
*
* @dataProvider provider
*/
public function testAdd($a, $b, $c)
{
$this->assertEquals($c, $a + $b);
}
public function provider()
{
$return1 = array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 3)
);
$return2 = array(
array(0, 0, 0),
array(0, 1, 1),
array(1, 0, 1),
array(1, 1, 2)
);
$return = $return1;
//$return = $return2;
//Uncommenting this line will pass this test
return $return;
}
}
?>
Loading