Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit e2e1269b4adc728fbb1500f7c1de20ef2689f553 @Kami committed Dec 27, 2009
@@ -0,0 +1,56 @@
+<?php
+
+require_once 'PHPUnit\Framework\TestCase.php';
+require_once('Factorial.php');
+
+/**
+ * Chapter 1 TDD exercises
+ *
+ * 1.1 Suppose you have to code a class that calculates the factorial of an
+ * integer N, which is the product of all integers from 1 to N. Write a failing
+ * test for it (do not code the class for now! Only the test. Suppose that the
+ * class and its methods already exists just like you want them to be.)
+ *
+ * 1.2 Add more test methods, which try different input numbers: 1, 4, 20
+ * Verify that different factorials are calculated correctly (again, just the test
+ * and no production code in this phase).
+ *
+ * 1.3 Write the production code needed to make all tests pass.
+ */
+class Chapter1Test extends PHPUnit_Framework_TestCase
+{
+
+ public function testFactorialConstructorNoNumberGivenRaiseException()
+ {
+ try
+ {
+ $factorial = new Factorial();
+ $this->fail();
+ }
+ catch (InvalidArgumentException $e)
+ {
+
+ }
+ }
+
+ public function testFactorialOfNumberOne()
+ {
+ $factorial = new Factorial(1);
+
+ $this->assertEquals(1, $factorial->getResult());
+ }
+
+ public function testFactorialOfNumberFour()
+ {
+ $factorial = new Factorial(4);
+
+ $this->assertEquals(24, $factorial->getResult());
+ }
+
+ public function testFactorialOfNumberTwenty()
+ {
+ $factorial = new Factorial(20);
+
+ $this->assertEquals(2432902008176640000, $factorial->getResult());
+ }
+}
@@ -0,0 +1,28 @@
+<?php
+
+class Factorial
+{
+ protected $_number;
+
+ public function __construct($number)
+ {
+ if (!is_int($number))
+ {
+ throw new InvalidArgumentException('Missing argument or not a number');
+ }
+
+ $this->_number = $number;
+ }
+
+ public function getResult()
+ {
+ $factorial = 1;
+
+ for ($i = 1; $i <= $this->_number; $i++)
+ {
+ $factorial *= $i;
+ }
+
+ return $factorial;
+ }
+}
@@ -0,0 +1,35 @@
+<?php
+
+class Calculator
+{
+ public static function sqrt($number = NULL)
+ {
+ if ($number === NULL)
+ {
+ throw new InvalidArgumentException('Missing argument');
+ }
+
+ if (!is_numeric($number))
+ {
+ throw new InvalidArgumentException('Argument is not a number');
+ }
+
+ $i = 0;
+ while (TRUE)
+ {
+ $result = pow(++$i, 2);
+
+ if ($result == $number)
+ {
+ break;
+ }
+ else if ($result > $number)
+ {
+ $i = (abs($number - pow($i - 1, 2)) < abs($number - $result)) ? $i - 1: $i;
+ break;
+ }
+ }
+
+ return $i;
+ }
+}
@@ -0,0 +1,93 @@
+<?php
+
+require_once 'PHPUnit\Framework\TestCase.php';
+require_once 'Calculator.php';
+
+/**
+ * 11.1 TDD a Calculator class, which has only the method Calculator::sqrt().
+ * Implement the class one test at the time.
+ * Remember that:
+ * - you should add a *failing* test and then improving production code
+ * to handle the new test case.
+ * - you can add a test only if the tests are green
+ * - you can add production code only if the tests are red, and if I
+ * removed part of your addition the test should return red (ensuring you are
+ * not writing unused code)
+ * - you cannot use the sqrt() function in this exercise
+ * - you can use a guess-and-check method: to find the square root of
+ * 36, try 1, try 2, try 3... until you get to 6. Round the result to the nearest
+ * integer.
+ */
+class Chapter11Test extends PHPUnit_Framework_TestCase
+{
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testCalculatorThrowsExceptionOnNoArgument()
+ {
+ Calculator::sqrt();
+ }
+
+ /**
+ * @expectedException InvalidArgumentException
+ */
+ public function testCalculatorThrowsExceptionOnInvalidArgumentType()
+ {
+ Calculator::sqrt('string');
+ }
+
+ public function testCalculateSquareRootOfNumber36()
+ {
+ $this->assertEquals(6, Calculator::sqrt(36));
+ }
+
+ public static function squareRootValues()
+ {
+ return array
+ (
+ array(1, 1),
+ array(4, 2),
+ array(9, 3),
+ array(16, 4),
+ array(25, 5),
+ array(36, 6),
+ array(49, 7),
+ array(64, 8),
+ array(81, 9),
+ array(100, 10)
+ );
+ }
+
+ /**
+ * @dataProvider squareRootValues
+ */
+ public function testSquareRootIsCalculactedCorrectly($number, $squareRoot)
+ {
+ $this->assertEquals($squareRoot, Calculator::sqrt($number));
+ }
+
+ public static function squareRootRoundedValues()
+ {
+ return array
+ (
+ array(26, 5),
+ array(27, 5),
+ array(28, 5),
+ array(29, 5),
+ array(30, 5),
+ array(31, 6),
+ array(32, 6),
+ array(33, 6),
+ array(34, 6),
+ array(35, 6)
+ );
+ }
+
+ /**
+ * @dataProvider squareRootRoundedValues
+ */
+ public function testSquareRootIsRoundedCorrectly($number, $squareRoot)
+ {
+ $this->assertEquals($squareRoot, Calculator::sqrt($number));
+ }
+}
@@ -0,0 +1,59 @@
+<?php
+
+require_once 'PHPUnit\Framework\TestCase.php';
+require_once('Factorial.php');
+
+/**
+ * Chapter 2 TDD exercises
+ *
+ * 18 Chapter 2: write clever tests
+ * the factorial of 0? (it is assumed by definition equal to 1.) Add a failing test
+ * case.
+ *
+ * 2.2 Modify the production class to make all the tests pass2.1 What does happen to your class from 1.1 when you try to calculate
+ */
+class Chapter2Test extends PHPUnit_Framework_TestCase
+{
+
+ public function testFactorialConstructorNoNumberGivenRaiseException()
+ {
+ try
+ {
+ $factorial = new Factorial();
+ $this->fail();
+ }
+ catch (InvalidArgumentException $e)
+ {
+
+ }
+ }
+
+ /* Already taken care in the class implementation for Chapter 1, but in any case */
+ public function testFactorialOfNumberZero()
+ {
+ $factorial = new Factorial(0);
+
+ $this->assertEquals(1, $factorial->getResult());
+ }
+
+ public function testFactorialOfNumberOne()
+ {
+ $factorial = new Factorial(1);
+
+ $this->assertEquals(1, $factorial->getResult());
+ }
+
+ public function testFactorialOfNumberFour()
+ {
+ $factorial = new Factorial(4);
+
+ $this->assertEquals(24, $factorial->getResult());
+ }
+
+ public function testFactorialOfNumberTwenty()
+ {
+ $factorial = new Factorial(20);
+
+ $this->assertEquals(2432902008176640000, $factorial->getResult());
+ }
+}
@@ -0,0 +1,28 @@
+<?php
+
+class Factorial
+{
+ protected $_number;
+
+ public function __construct($number)
+ {
+ if (!is_int($number))
+ {
+ throw new InvalidArgumentException('Missing argument or not a number');
+ }
+
+ $this->_number = $number;
+ }
+
+ public function getResult()
+ {
+ $factorial = 1;
+
+ for ($i = 1; $i <= $this->_number; $i++)
+ {
+ $factorial *= $i;
+ }
+
+ return $factorial;
+ }
+}
@@ -0,0 +1,48 @@
+<?php
+
+require_once 'PHPUnit\Framework\TestCase.php';
+require_once 'SomeClass.php';
+
+/**
+ * Chapter 3 TDD exercises
+ *
+ * 3.1 Write tests for a class which takes a single argument in the
+ * constructor and gives it back when a getter is called (assertSame() or
+ * assertEquals()?), then write the production class.
+ *
+ * 3.2 Write tests for the sort() php function, for simplicity with integer
+ * arrays as data.
+ */
+class Chapter3Test extends PHPUnit_Framework_TestCase
+{
+ public function testClassReturnsValidValue()
+ {
+ $class = new SomeClass('random value');
+
+ $this->assertEquals('random value', $class->getValue());
+ }
+
+ public function testIntegersAreSortedCorrectly()
+ {
+ $numbers = array (8, 9, 5, 4, 7, 1, 2);
+ $result = array(1, 2, 4, 5, 7, 8, 9);
+ sort($numbers);
+
+ $this->assertSame($result, $numbers);
+ }
+
+ public function testSortWasSuccessfull()
+ {
+ $numbers = array (8, 9, 5, 4, 7, 1, 2);
+
+ $this->assertTrue(sort($numbers));
+ }
+
+ public function testSortPreservesInputVariableType()
+ {
+ $numbers = array (8, 9, 5, 4, 7, 1, 2);
+ sort($numbers);
+
+ $this->assertType('array', $numbers);
+ }
+}
@@ -0,0 +1,16 @@
+<?php
+
+class SomeClass
+{
+ protected $_someVariable;
+
+ public function __construct($someVariable)
+ {
+ $this->_someVariable = $someVariable;
+ }
+
+ public function getValue()
+ {
+ return $this->_someVariable;
+ }
+}
Oops, something went wrong.

0 comments on commit e2e1269

Please sign in to comment.