Task 1: Calculator init
This commit is contained in:
@@ -0,0 +1 @@
|
||||
{"version":1,"defects":[],"times":{"Tests\\CalculatorTest::testAdd":0.007,"Tests\\CalculatorTest::testSubtract":0,"Tests\\CalculatorTest::testMultiply":0,"Tests\\CalculatorTest::testDivide":0,"Tests\\CalculatorTest::testDivideByZero":0.001}}
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name": "oskar/task_01",
|
||||
"type": "project",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"App\\": "src/"
|
||||
}
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^10.5"
|
||||
}
|
||||
}
|
||||
Generated
+1643
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use App\Calculator;
|
||||
use App\Operation;
|
||||
|
||||
try {
|
||||
$calculator = new Calculator();
|
||||
|
||||
$result = $calculator->calculate(10, 5, Operation::ADD);
|
||||
echo "Wynik dodawania: $result\n";
|
||||
|
||||
$result = $calculator->calculate(10, 5, Operation::SUBTRACT);
|
||||
echo "Wynik odejmowania: $result\n";
|
||||
|
||||
$result = $calculator->calculate(10, 5, Operation::MULTIPLY);
|
||||
echo "Wynik mnożenia: $result\n";
|
||||
|
||||
$result = $calculator->calculate(10, 5, Operation::DIVIDE);
|
||||
echo "Wynik dzielenia: $result\n";
|
||||
} catch (InvalidArgumentException $e) {
|
||||
echo 'Błąd: ' . $e->getMessage();
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="vendor/autoload.php">
|
||||
<testsuites>
|
||||
<testsuite name="My Test Suite">
|
||||
<directory>./tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class Calculator implements CalculatorInterface
|
||||
{
|
||||
public function calculate(float $a, float $b, Operation $operation): float
|
||||
{
|
||||
return match ($operation) {
|
||||
Operation::ADD => $this->add($a, $b),
|
||||
Operation::SUBTRACT => $this->subtract($a, $b),
|
||||
Operation::MULTIPLY => $this->multiply($a, $b),
|
||||
Operation::DIVIDE => $this->divide($a, $b),
|
||||
};
|
||||
}
|
||||
|
||||
private function add(float $a, float $b): float
|
||||
{
|
||||
return $a + $b;
|
||||
}
|
||||
|
||||
private function subtract(float $a, float $b): float
|
||||
{
|
||||
return $a - $b;
|
||||
}
|
||||
|
||||
private function multiply(float $a, float $b): float
|
||||
{
|
||||
return $a * $b;
|
||||
}
|
||||
|
||||
private function divide(float $a, float $b): float
|
||||
{
|
||||
if ($b === 0.0) {
|
||||
throw new InvalidArgumentException('Division by zero is not allowed.');
|
||||
}
|
||||
return $a / $b;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
interface CalculatorInterface
|
||||
{
|
||||
public function calculate(float $a, float $b, Operation $operation): float;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App;
|
||||
|
||||
enum Operation: string {
|
||||
case ADD = 'add';
|
||||
case SUBTRACT = 'subtract';
|
||||
case MULTIPLY = 'multiply';
|
||||
case DIVIDE = 'divide';
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use App\Calculator;
|
||||
use App\Operation;
|
||||
use InvalidArgumentException;
|
||||
|
||||
class CalculatorTest extends TestCase
|
||||
{
|
||||
private Calculator $calculator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->calculator = new Calculator();
|
||||
}
|
||||
|
||||
public function testAdd(): void
|
||||
{
|
||||
$result = $this->calculator->calculate(10, 5, Operation::ADD);
|
||||
$this->assertEquals(15, $result);
|
||||
}
|
||||
|
||||
public function testSubtract(): void
|
||||
{
|
||||
$result = $this->calculator->calculate(10, 5, Operation::SUBTRACT);
|
||||
$this->assertEquals(5, $result);
|
||||
}
|
||||
|
||||
public function testMultiply(): void
|
||||
{
|
||||
$result = $this->calculator->calculate(10, 5, Operation::MULTIPLY);
|
||||
$this->assertEquals(50, $result);
|
||||
}
|
||||
|
||||
public function testDivide(): void
|
||||
{
|
||||
$result = $this->calculator->calculate(10, 5, Operation::DIVIDE);
|
||||
$this->assertEquals(2, $result);
|
||||
}
|
||||
|
||||
public function testDivideByZero(): void
|
||||
{
|
||||
$this->expectException(InvalidArgumentException::class);
|
||||
$this->calculator->calculate(10, 0, Operation::DIVIDE);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user