diff --git a/app/libraries/Calculator.php b/app/libraries/Calculator.php index 3fd2190..26a184e 100644 --- a/app/libraries/Calculator.php +++ b/app/libraries/Calculator.php @@ -8,4 +8,29 @@ public function add($x, $y) } return $x + $y; } + + public function sub($x, $y) + { + if ( !is_numeric($x) || !is_numeric($y)) { + throw new \InvalidArgumentException; + } + return $x - $y; + } + + public function mul($x, $y) + { + if ( !is_numeric($x) || !is_numeric($y)) { + throw new \InvalidArgumentException; + } + return $x * $y; + } + + public function div($x, $y) + { + if ( !is_numeric($x) || !is_numeric($y)) { + throw new \InvalidArgumentException; + } + return $x / $y; + } + } \ No newline at end of file diff --git a/test/CalculatorTest.php b/test/CalculatorTest.php index 6e5dff9..47b787c 100644 --- a/test/CalculatorTest.php +++ b/test/CalculatorTest.php @@ -9,7 +9,7 @@ public function setUp() $this->calculator = new Calculator; } - public function inputNumbers() + public function inputNumbersForSum() { return [ [2, 2, 4], @@ -17,8 +17,32 @@ public function inputNumbers() ]; } + public function inputNumbersForSubstract() + { + return [ + [2, 2, 0], + [-20, -5, -15] + ]; + } + + public function inputNumbersForMultiply() + { + return [ + [2, 4, 8], + [10, 20, 200] + ]; + } + + public function inputNumbersForDivide() + { + return [ + [12, 6, 2], + [10, 5, 2] + ]; + } + /** - * @dataProvider inputNumbers + * @dataProvider inputNumbersForSum */ public function testCanAddNumbers($x, $y, $sum) { @@ -32,5 +56,32 @@ public function testThrowsExceptionIfNonNumberIsPassed() { $calc = new Calculator; $calc->add('a', 'b'); + $calc->sub('2', 'b'); + $calc->mul('3', 'c'); + $calc->div('20', 'a'); + } + + /** + * @dataProvider inputNumbersForSubstract + */ + public function testCanSubstractNumber($x, $y, $sub) + { + $this->assertEquals($sub, $this->calculator->sub($x, $y)); + } + + /** + * @dataProvider inputNumbersForMultiply + */ + public function testCanMultiplyNumbers($x, $y, $mul) + { + $this->assertEquals($mul, $this->calculator->mul($x, $y)); + } + + /** + * @dataProvider inputNumbersForDivide + */ + public function testCanDivideNumbers($x, $y, $div) + { + $this->assertEquals($div, $this->calculator->div($x, $y)); } } \ No newline at end of file diff --git a/vendor/autoload.php b/vendor/autoload.php index d6e96dd..5c33b76 100644 --- a/vendor/autoload.php +++ b/vendor/autoload.php @@ -1,7 +1,7 @@ prefixes; + return call_user_func_array('array_merge', $this->prefixes); } public function getFallbackDirs() @@ -75,28 +75,63 @@ public function addClassMap(array $classMap) } /** - * Registers a set of classes + * Registers a set of classes, merging with any others previously set. * - * @param string $prefix The classes prefix - * @param array|string $paths The location(s) of the classes + * @param string $prefix The classes prefix + * @param array|string $paths The location(s) of the classes + * @param bool $prepend Prepend the location(s) */ - public function add($prefix, $paths) + public function add($prefix, $paths, $prepend = false) { if (!$prefix) { - foreach ((array) $paths as $path) { - $this->fallbackDirs[] = $path; + if ($prepend) { + $this->fallbackDirs = array_merge( + (array) $paths, + $this->fallbackDirs + ); + } else { + $this->fallbackDirs = array_merge( + $this->fallbackDirs, + (array) $paths + ); } return; } - if (isset($this->prefixes[$prefix])) { - $this->prefixes[$prefix] = array_merge( - $this->prefixes[$prefix], - (array) $paths + + $first = $prefix[0]; + if (!isset($this->prefixes[$first][$prefix])) { + $this->prefixes[$first][$prefix] = (array) $paths; + + return; + } + if ($prepend) { + $this->prefixes[$first][$prefix] = array_merge( + (array) $paths, + $this->prefixes[$first][$prefix] ); } else { - $this->prefixes[$prefix] = (array) $paths; + $this->prefixes[$first][$prefix] = array_merge( + $this->prefixes[$first][$prefix], + (array) $paths + ); + } + } + + /** + * Registers a set of classes, replacing any others previously set. + * + * @param string $prefix The classes prefix + * @param array|string $paths The location(s) of the classes + */ + public function set($prefix, $paths) + { + if (!$prefix) { + $this->fallbackDirs = (array) $paths; + + return; } + $this->prefixes[substr($prefix, 0, 1)][$prefix] = (array) $paths; } /** @@ -142,7 +177,7 @@ public function unregister() * Loads the given class or interface. * * @param string $class The name of the class - * @return bool|null True, if loaded + * @return bool|null True if loaded, null otherwise */ public function loadClass($class) { @@ -158,10 +193,11 @@ public function loadClass($class) * * @param string $class The name of the class * - * @return string|null The path, if found + * @return string|false The path if found, false otherwise */ public function findFile($class) { + // work around for PHP 5.3.0 - 5.3.2 https://bugs.php.net/50731 if ('\\' == $class[0]) { $class = substr($class, 1); } @@ -172,7 +208,7 @@ public function findFile($class) if (false !== $pos = strrpos($class, '\\')) { // namespaced class name - $classPath = str_replace('\\', DIRECTORY_SEPARATOR, substr($class, 0, $pos)) . DIRECTORY_SEPARATOR; + $classPath = strtr(substr($class, 0, $pos), '\\', DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; $className = substr($class, $pos + 1); } else { // PEAR-like class name @@ -180,13 +216,16 @@ public function findFile($class) $className = $class; } - $classPath .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; + $classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php'; - foreach ($this->prefixes as $prefix => $dirs) { - if (0 === strpos($class, $prefix)) { - foreach ($dirs as $dir) { - if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { - return $dir . DIRECTORY_SEPARATOR . $classPath; + $first = $class[0]; + if (isset($this->prefixes[$first])) { + foreach ($this->prefixes[$first] as $prefix => $dirs) { + if (0 === strpos($class, $prefix)) { + foreach ($dirs as $dir) { + if (file_exists($dir . DIRECTORY_SEPARATOR . $classPath)) { + return $dir . DIRECTORY_SEPARATOR . $classPath; + } } } } diff --git a/vendor/composer/autoload_classmap.php b/vendor/composer/autoload_classmap.php index 4e01093..f4a2678 100644 --- a/vendor/composer/autoload_classmap.php +++ b/vendor/composer/autoload_classmap.php @@ -1,8 +1,8 @@ $path) { - $loader->add($namespace, $path); + $loader->set($namespace, $path); } $classMap = require __DIR__ . '/autoload_classmap.php'; @@ -36,7 +36,7 @@ public static function getLoader() $loader->addClassMap($classMap); } - $loader->register(); + $loader->register(true); return $loader; }