diff --git a/src/Entity/InvoiceTemplate.php b/src/Entity/InvoiceTemplate.php index 78d3a36b..4b97f636 100644 --- a/src/Entity/InvoiceTemplate.php +++ b/src/Entity/InvoiceTemplate.php @@ -306,7 +306,7 @@ class InvoiceTemplate * @param string $paymentTerms * @return InvoiceTemplate */ - public function setPaymentTerms(string $paymentTerms) + public function setPaymentTerms(?string $paymentTerms) { $this->paymentTerms = $paymentTerms; diff --git a/tests/Entity/AbstractEntityTest.php b/tests/Entity/AbstractEntityTest.php new file mode 100644 index 00000000..74b60b09 --- /dev/null +++ b/tests/Entity/AbstractEntityTest.php @@ -0,0 +1,57 @@ +enableAnnotationMapping()->getValidator(); + $validations = $validator->validate($value); + + if (!is_array($fieldNames)) { + $fieldNames = [$fieldNames]; + } + + $violatedFields = []; + /** @var ConstraintViolationInterface $validation */ + foreach ($validations as $validation) { + $violatedFields[] = $validation->getPropertyPath(); + } + + foreach ($fieldNames as $id => $propertyPath) { + $foundField = false; + if (in_array($propertyPath, $violatedFields)) { + $foundField = true; + unset($violatedFields[$id]); + } + + $this->assertTrue($foundField, 'Failed finding violation for field: ' . $propertyPath); + } + + $this->assertEmpty($violatedFields, sprintf('Unexpected violations found: %s', implode(', ', $violatedFields))); + + $expected = count($fieldNames); + $actual = $validations->count(); + + $this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual)); + } +} diff --git a/tests/Entity/InvoiceTemplateTest.php b/tests/Entity/InvoiceTemplateTest.php new file mode 100644 index 00000000..94be52cf --- /dev/null +++ b/tests/Entity/InvoiceTemplateTest.php @@ -0,0 +1,51 @@ +assertInstanceOf(InvoiceTemplate::class, $actual); + } + + protected function getEntity() + { + $entity = new InvoiceTemplate(); + + return $entity; + } + + public function testSetGetPaymentTerms() + { + $sut = $this->getEntity(); + + $this->assertNull($sut->getPaymentTerms()); + $this->assertIsFluent($sut->setPaymentTerms(null)); + $this->assertIsFluent($sut->setPaymentTerms('')); + $this->assertIsFluent($sut->setPaymentTerms('foo bar')); + $this->assertEquals('foo bar', $sut->getPaymentTerms()); + } + + public function testToString() + { + $sut = $this->getEntity(); + + $this->assertNull($sut->__toString()); + $this->assertIsFluent($sut->setName('a template name')); + $this->assertEquals('a template name', $sut->__toString()); + $this->assertEquals('a template name', (string) $sut); + } +} diff --git a/tests/Entity/TimesheetTest.php b/tests/Entity/TimesheetTest.php index 3c9a751e..21b4bb30 100644 --- a/tests/Entity/TimesheetTest.php +++ b/tests/Entity/TimesheetTest.php @@ -7,21 +7,18 @@ * file that was distributed with this source code. */ -namespace App\Tests\Twig; +namespace App\Tests\Entity; use App\Entity\Activity; use App\Entity\Customer; use App\Entity\Project; use App\Entity\Timesheet; use App\Entity\User; -use PHPUnit\Framework\TestCase; -use Symfony\Component\Validator\ConstraintViolationInterface; -use Symfony\Component\Validator\Validation; /** * @covers \App\Entity\Timesheet */ -class TimesheetTest extends TestCase +class TimesheetTest extends AbstractEntityTest { protected function getEntity() { @@ -85,41 +82,4 @@ class TimesheetTest extends TestCase $this->assertHasViolationForField($entity, []); } - - /** - * @param $value - * @param array|string $fieldNames - */ - protected function assertHasViolationForField($value, $fieldNames) - { - $validator = Validation::createValidatorBuilder()->enableAnnotationMapping()->getValidator(); - $validations = $validator->validate($value); - - if (!is_array($fieldNames)) { - $fieldNames = [$fieldNames]; - } - - $violatedFields = []; - /** @var ConstraintViolationInterface $validation */ - foreach ($validations as $validation) { - $violatedFields[] = $validation->getPropertyPath(); - } - - foreach ($fieldNames as $id => $propertyPath) { - $foundField = false; - if (in_array($propertyPath, $violatedFields)) { - $foundField = true; - unset($violatedFields[$id]); - } - - $this->assertTrue($foundField, 'Failed finding violation for field: ' . $propertyPath); - } - - $this->assertEmpty($violatedFields, sprintf('Unexpected violations found: %s', implode(', ', $violatedFields))); - - $expected = count($fieldNames); - $actual = $validations->count(); - - $this->assertEquals($expected, $actual, sprintf('Expected %s violations, found %s.', $expected, $actual)); - } }