mirror of
https://github.com/kevinveenbirkenbach/infinito.git
synced 2025-01-09 22:17:26 +01:00
Implemented tests for AndOperation and optimized Operations
This commit is contained in:
parent
6540e1dd95
commit
5848bec9ce
@ -8,6 +8,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
use App\Entity\Source\AbstractSource;
|
use App\Entity\Source\AbstractSource;
|
||||||
use App\Entity\Source\Operation\Attribut\OperandsAttribut;
|
use App\Entity\Source\Operation\Attribut\OperandsAttribut;
|
||||||
|
use App\Exception\NotProcessedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -41,7 +42,10 @@ abstract class AbstractOperation extends AbstractSource implements OperandInterf
|
|||||||
|
|
||||||
public function getResult(): ResultInterface
|
public function getResult(): ResultInterface
|
||||||
{
|
{
|
||||||
return $this->result;
|
if ($this->result) {
|
||||||
|
return $this->result;
|
||||||
|
}
|
||||||
|
throw new NotProcessedException('No result was generated!');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setOperators(ArrayCollection $operands): void
|
public function setOperators(ArrayCollection $operands): void
|
||||||
|
@ -5,6 +5,7 @@ namespace App\Entity\Source\Operation;
|
|||||||
use App\Logic\Operation\OperandInterface;
|
use App\Logic\Operation\OperandInterface;
|
||||||
use App\Logic\Result\Result;
|
use App\Logic\Result\Result;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
use App\Exception\NotDefinedException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author kevinfrantz
|
* @author kevinfrantz
|
||||||
@ -16,7 +17,7 @@ final class AndOperation extends AbstractOperation
|
|||||||
public function process(): void
|
public function process(): void
|
||||||
{
|
{
|
||||||
if ($this->operands->isEmpty()) {
|
if ($this->operands->isEmpty()) {
|
||||||
throw new \Exception('Operands must be defined!');
|
throw new NotDefinedException('Operands must be defined!');
|
||||||
}
|
}
|
||||||
$this->result = new Result();
|
$this->result = new Result();
|
||||||
/*
|
/*
|
||||||
|
11
application/src/Exception/NotDefinedException.php
Normal file
11
application/src/Exception/NotDefinedException.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exception;
|
||||||
|
|
||||||
|
class NotDefinedException extends \Exception
|
||||||
|
{
|
||||||
|
public function __construct($message = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
11
application/src/Exception/NotProcessedException.php
Normal file
11
application/src/Exception/NotProcessedException.php
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exception;
|
||||||
|
|
||||||
|
class NotProcessedException extends \Exception
|
||||||
|
{
|
||||||
|
public function __construct($message = null)
|
||||||
|
{
|
||||||
|
parent::__construct($message);
|
||||||
|
}
|
||||||
|
}
|
@ -10,6 +10,7 @@ use Doctrine\Common\Collections\ArrayCollection;
|
|||||||
use App\Logic\Operation\OperandInterface;
|
use App\Logic\Operation\OperandInterface;
|
||||||
use App\Logic\Result\ResultInterface;
|
use App\Logic\Result\ResultInterface;
|
||||||
use App\Logic\Result\Result;
|
use App\Logic\Result\Result;
|
||||||
|
use App\Exception\NotProcessedException;
|
||||||
|
|
||||||
class AbstractOperationTest extends TestCase
|
class AbstractOperationTest extends TestCase
|
||||||
{
|
{
|
||||||
@ -21,14 +22,9 @@ class AbstractOperationTest extends TestCase
|
|||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->operation = new class() extends AbstractOperation {
|
$this->operation = new class() extends AbstractOperation {
|
||||||
public function getResult(): ResultInterface
|
|
||||||
{
|
|
||||||
return new Result();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function process(): void
|
public function process(): void
|
||||||
{
|
{
|
||||||
return;
|
$this->result = new Result();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -49,11 +45,20 @@ class AbstractOperationTest extends TestCase
|
|||||||
$operands = new ArrayCollection();
|
$operands = new ArrayCollection();
|
||||||
$operands->add($operand);
|
$operands->add($operand);
|
||||||
$this->operation->setOperands($operands);
|
$this->operation->setOperands($operands);
|
||||||
$this->assertEquals($operand, $this->operation->getOperands()->get(0));
|
$this->assertEquals($operand, $this->operation->getOperands()
|
||||||
|
->get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testResult()
|
public function testNotProcessedException(): void
|
||||||
{
|
{
|
||||||
|
$this->expectException(NotProcessedException::class);
|
||||||
|
$this->operation->getResult();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testResult(): void
|
||||||
|
{
|
||||||
|
$this->setUp();
|
||||||
|
$this->operation->process();
|
||||||
$this->assertInstanceOf(ResultInterface::class, $this->operation->getResult());
|
$this->assertInstanceOf(ResultInterface::class, $this->operation->getResult());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Entity\Source\Operation;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use App\Exception\NotDefinedException;
|
||||||
|
use App\Logic\Result\Result;
|
||||||
|
use App\Logic\Operation\OperandInterface;
|
||||||
|
use App\Logic\Result\ResultInterface;
|
||||||
|
use Doctrine\Common\Collections\ArrayCollection;
|
||||||
|
|
||||||
|
class AndOperationTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var OperationInterface
|
||||||
|
*/
|
||||||
|
protected $operation;
|
||||||
|
|
||||||
|
public function setUp(): void
|
||||||
|
{
|
||||||
|
$this->operation = new AndOperation();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstructor(): void
|
||||||
|
{
|
||||||
|
$this->expectException(NotDefinedException::class);
|
||||||
|
$this->operation->process();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testProcess(): void
|
||||||
|
{
|
||||||
|
//Test True
|
||||||
|
$operand1 = new class() implements OperandInterface {
|
||||||
|
public function getResult(): ResultInterface
|
||||||
|
{
|
||||||
|
$result = new Result();
|
||||||
|
$result->setAll(true);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
$operand2 = new class() implements OperandInterface {
|
||||||
|
public function getResult(): ResultInterface
|
||||||
|
{
|
||||||
|
$result = new Result();
|
||||||
|
$result->setAll(true);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$operands = new ArrayCollection([$operand1, $operand2]);
|
||||||
|
$this->operation->setOperands($operands);
|
||||||
|
$this->operation->process();
|
||||||
|
$this->assertEquals(true, $this->operation->getResult()->getBool());
|
||||||
|
|
||||||
|
//Test False
|
||||||
|
$operand3 = new class() implements OperandInterface {
|
||||||
|
public function getResult(): ResultInterface
|
||||||
|
{
|
||||||
|
$result = new Result();
|
||||||
|
$result->setAll(false);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
$this->operation->getOperands()->add($operand3);
|
||||||
|
$this->operation->process();
|
||||||
|
$this->assertEquals(false, $this->operation->getResult()->getBool());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user