Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions Tests/Functional/Domain/Model/TeaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace TTN\Tea\Tests\Functional\Domain\Model;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TTN\Tea\Domain\Model\Tea;
use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

#[CoversClass(Tea::class)]
final class TeaTest extends FunctionalTestCase
{
protected array $testExtensionsToLoad = ['ttn/tea'];

private Tea $subject;
private ConjunctionValidator $validator;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please autoformat - there should be an empty line between the properties.


protected function setUp(): void
{
parent::setUp();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, we should have a blank line after parent::setUp();.

$this->subject = new Tea();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, creating the subject should be the last line in setUp() (if possible).

$validatorResolver = $this->getContainer()->get(ValidatorResolver::class);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll most probably be able to skip getContainer() here:

Suggested change
$validatorResolver = $this->getContainer()->get(ValidatorResolver::class);
$validatorResolver = $this->get(ValidatorResolver::class);

$this->validator = $validatorResolver->getBaseValidatorConjunction(Tea::class);
}

#[Test]
public function titleWithMaximumLengthPassesValidation(): void
{
$this->subject->setTitle(str_repeat('p', 255));
Comment thread
oliverklee marked this conversation as resolved.
$result = $this->validator->validate($this->subject);
self::assertFalse($result->forProperty('title')->hasErrors());
}

#[Test]
public function titleLongerThanMaximumLengthDoesNotPassValidation(): void
{
$this->subject->setTitle(str_repeat('p', 256));
$result = $this->validator->validate($this->subject);
self::assertTrue($result->forProperty('title')->hasErrors());
}

#[Test]
public function emptyTitleDoesNotPassValidation(): void
{
$this->subject->setTitle('');
$result = $this->validator->validate($this->subject);
self::assertTrue($result->forProperty('title')->hasErrors());
}

#[Test]
public function descriptionWithMaximumLengthPassesValidation(): void
{
$this->subject->setDescription(str_repeat('d', 2000));
$result = $this->validator->validate($this->subject);
self::assertFalse($result->forProperty('description')->hasErrors());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all tests, please add blank lines between the different test phases:

  • test-specific setup (set the data)
  • execution (validate call)
  • validation (assert* call)

I've visualized this on a slide in one of my presentations on testing.

}

#[Test]
public function descriptionLongerThanMaximumLengthDoesNotPassValidation(): void
{
$this->subject->setDescription(str_repeat('d', 2001));
$result = $this->validator->validate($this->subject);
self::assertTrue($result->forProperty('description')->hasErrors());
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this blank line.

}