SuperSeller3000/tests/Unit/Application/Article/ArticleValidatorTest.php

67 lines
2.3 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace App\Tests\Unit\Application\Article;
use App\Application\Article\ArticleValidator;
use App\Domain\Article\Article;
use App\Domain\Article\ArticleCondition;
use App\Domain\Article\ArticleType;
use App\Domain\Article\AttributeDefinition;
use App\Domain\Article\AttributeType;
use App\Domain\Article\AttributeValue;
use PHPUnit\Framework\TestCase;
final class ArticleValidatorTest extends TestCase
{
private ArticleValidator $validator;
private ArticleType $type;
private AttributeDefinition $ramDef;
private AttributeDefinition $cpuDef;
protected function setUp(): void
{
$this->validator = new ArticleValidator();
$this->type = new ArticleType('Notebook');
$this->ramDef = new AttributeDefinition('RAM', AttributeType::String);
$this->cpuDef = new AttributeDefinition('CPU', AttributeType::String);
$this->type->addAttributeDefinition($this->ramDef);
$this->type->addAttributeDefinition($this->cpuDef);
}
public function test_valid_when_all_attributes_set(): void
{
$article = new Article($this->type, 'NB-001', 'INV-001', 1, ArticleCondition::Good);
$article->setAttributeValue(new AttributeValue($article, $this->ramDef, '16 GB'));
$article->setAttributeValue(new AttributeValue($article, $this->cpuDef, 'Intel i7'));
$missing = $this->validator->getMissingAttributes($article);
$this->assertEmpty($missing);
$this->assertTrue($this->validator->isValid($article));
}
public function test_returns_missing_attribute_names(): void
{
$article = new Article($this->type, 'NB-001', 'INV-001', 1, ArticleCondition::Good);
$article->setAttributeValue(new AttributeValue($article, $this->ramDef, '16 GB'));
// cpuDef not set
$missing = $this->validator->getMissingAttributes($article);
$this->assertCount(1, $missing);
$this->assertContains('CPU', $missing);
$this->assertFalse($this->validator->isValid($article));
}
public function test_all_missing_when_no_values_set(): void
{
$article = new Article($this->type, 'NB-001', 'INV-001', 1, ArticleCondition::Good);
$missing = $this->validator->getMissingAttributes($article);
$this->assertCount(2, $missing);
}
}