<?php
namespace Ampersand\DevTest\BasicOop;
interface NumberInterface
{
/**
* Adds n to this number
*
* @param int $n The amount to add to this number
* @return int The new integer value of this number
*/
public function add($n);
/**
* Subtracts n from this number
*
* @param int $n The amount to subtract from this number
* @return int The new integer value of this number
*/
public function subtract($n);
/**
* Multiplies this number by n
*
* @param int $n The amount to multiply this number by
* @return int The new integer value of this number
*/
public function multiply($n);
/**
* Divides this number by n
*
* @param int $n The amount to divide this number by
* @return int The new integer value of this number
*/
public function divide($n);
/**
* Returns the integer value of this number
*
* @return int The current integer value of this number
*/
public function toInt();
}
class NumberTest
{
public function testConstruct()
{
$n = new Number;
$this->assertEquals(0, $n->toInt());
$n = new Number(0);
$this->assertEquals(0, $n->toInt());
$n = new Number(3);
$this->assertEquals(3, $n->toInt());
}
public function testAdd()
{
$n = new Number;
$n->add(5);
$this->assertEquals(5, $n->toInt());
$n->add(-7);
$this->assertEquals(-2, $n->toInt());
}
public function testSubtract()
{
$n = new Number;
$n->subtract(8);
$this->assertEquals(-8, $n->toInt());
$n->subtract(-10);
$this->assertEquals(2, $n->toInt());
}
public function testMultiply()
{
$n = new Number(1);
$n->multiply(10);
$this->assertEquals(10, $n->toInt());
$n->multiply(6);
$this->assertEquals(60, $n->toInt());
}
public function testDivide()
{
$n = new Number(100);
$n->divide(5);
$this->assertEquals(20, $n->toInt());
$n->divide(5);
$this->assertEquals(4, $n->toInt());
$hasThrownEx = false;
try {
$n->divide(0);
} catch (\InvalidArgumentException $e) {
$hasThrownEx = true;
}
$this->assertEquals(true, $hasThrownEx);
}
private function assertEquals($expected, $actual)
{
if ($expect !== $actual) {
echo "\n";
var_dump($actual);
echo ' does not match expected ';
var_dump($expected);
echo "\n";
}
}
}
class Number implements NumberInterface
{
private $value;
public function __construct($value = 0)
{
$this->value = $value;
}
public function add($value)
{
$this->value += $value;
return $this->value;
}
public function subtract($value)
{
$this->value -= $value;
return $this->value;
}
public function multiply($value)
{
$this->value *= $value;
return $this->value;
}
public function divide($value)
{
if (0 === $value) {
throw new \InvalidArgumentException;
}
$this->value /= $value;
return $this->value;
}
public function toInt()
{
return $this->value;
}
}
$test = new NumberTest;
$test->testConstruct();
$test->testAdd();
$test->testSubtract();
$test->testMultiply();
$test->testDivide();
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(0)
does not match expected int(0)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(0)
does not match expected int(0)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(3)
does not match expected int(3)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(5)
does not match expected int(5)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(-2)
does not match expected int(-2)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(-8)
does not match expected int(-8)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(2)
does not match expected int(2)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(10)
does not match expected int(10)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(60)
does not match expected int(60)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(20)
does not match expected int(20)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
int(4)
does not match expected int(4)
Warning: Undefined variable $expect in /in/ccKr7 on line 115
bool(true)
does not match expected bool(true)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(0)
does not match expected int(0)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(0)
does not match expected int(0)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(3)
does not match expected int(3)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(5)
does not match expected int(5)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(-2)
does not match expected int(-2)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(-8)
does not match expected int(-8)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(2)
does not match expected int(2)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(10)
does not match expected int(10)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(60)
does not match expected int(60)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(20)
does not match expected int(20)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
int(4)
does not match expected int(4)
Notice: Undefined variable: expect in /in/ccKr7 on line 115
bool(true)
does not match expected bool(true)
Output for 7.3.32 - 7.3.33
int(0)
does not match expected int(0)
int(0)
does not match expected int(0)
int(3)
does not match expected int(3)
int(5)
does not match expected int(5)
int(-2)
does not match expected int(-2)
int(-8)
does not match expected int(-8)
int(2)
does not match expected int(2)
int(10)
does not match expected int(10)
int(60)
does not match expected int(60)
int(20)
does not match expected int(20)
int(4)
does not match expected int(4)
bool(true)
does not match expected bool(true)