-
Notifications
You must be signed in to change notification settings - Fork 144
[TASK] Add trait providing standard implementation of Commentable
#1206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fa0e11c
[TASK] Add trait providing standard implementation of `Commentable`
JakeQZ de45497
With PHPStan, require trait users to implement `Commentable`
JakeQZ 94a17b4
Change test subject type to `ConcreteCommentContainer`
JakeQZ 65bb1e7
Rename test methods to focus on the main method under test
JakeQZ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sabberworm\CSS\Comment; | ||
|
||
/** | ||
* Provides a standard reusable implementation of `Commentable`. | ||
* | ||
* @internal | ||
* | ||
* @phpstan-require-implements Commentable | ||
*/ | ||
trait CommentContainer | ||
oliverklee marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @var list<Comment> | ||
*/ | ||
protected $comments = []; | ||
|
||
/** | ||
* @param list<Comment> $comments | ||
*/ | ||
public function addComments(array $comments): void | ||
{ | ||
$this->comments = \array_merge($this->comments, $comments); | ||
} | ||
|
||
/** | ||
* @return list<Comment> | ||
*/ | ||
public function getComments(): array | ||
{ | ||
return $this->comments; | ||
} | ||
|
||
/** | ||
* @param list<Comment> $comments | ||
*/ | ||
public function setComments(array $comments): void | ||
{ | ||
$this->comments = $comments; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,232 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sabberworm\CSS\Tests\Unit\Comment; | ||
|
||
use PHPUnit\Framework\Constraint\LogicalAnd; | ||
use PHPUnit\Framework\Constraint\TraversableContains; | ||
use PHPUnit\Framework\TestCase; | ||
use Sabberworm\CSS\Comment\Comment; | ||
use Sabberworm\CSS\Tests\Unit\Comment\Fixtures\ConcreteCommentContainer; | ||
use TRegx\DataProvider\DataProviders; | ||
|
||
/** | ||
* @covers \Sabberworm\CSS\Comment\CommentContainer | ||
*/ | ||
final class CommentContainerTest extends TestCase | ||
{ | ||
/** | ||
* @var ConcreteCommentContainer | ||
*/ | ||
private $subject; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->subject = new ConcreteCommentContainer(); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function getCommentsInitiallyReturnsEmptyArray(): void | ||
{ | ||
self::assertSame([], $this->subject->getComments()); | ||
} | ||
|
||
/** | ||
* @return array<non-empty-string, array{0: list<Comment>}> | ||
*/ | ||
public function provideCommentArray(): array | ||
{ | ||
return [ | ||
'no comment' => [[]], | ||
'one comment' => [[new Comment('Is this really a spoon?')]], | ||
'two comments' => [[ | ||
new Comment('I’m a teapot.'), | ||
new Comment('I’m a cafetière.'), | ||
JakeQZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]], | ||
]; | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $comments | ||
* | ||
* @dataProvider provideCommentArray | ||
*/ | ||
public function addCommentsOnVirginContainerAddsCommentsProvided(array $comments): void | ||
{ | ||
$this->subject->addComments($comments); | ||
|
||
self::assertSame($comments, $this->subject->getComments()); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $comments | ||
* | ||
* @dataProvider provideCommentArray | ||
*/ | ||
public function addCommentsWithEmptyArrayKeepsOriginalCommentsUnchanged(array $comments): void | ||
{ | ||
$this->subject->setComments($comments); | ||
|
||
$this->subject->addComments([]); | ||
|
||
self::assertSame($comments, $this->subject->getComments()); | ||
} | ||
|
||
/** | ||
* @return array<non-empty-string, array{0: list<Comment>}> | ||
*/ | ||
public function provideAlternativeCommentArray(): array | ||
{ | ||
return [ | ||
'no comment' => [[]], | ||
'one comment' => [[new Comment('Can I eat it with my hands?')]], | ||
'two comments' => [[ | ||
new Comment('I’m a beer barrel.'), | ||
new Comment('I’m a vineyard.'), | ||
]], | ||
]; | ||
} | ||
|
||
/** | ||
* @return array<non-empty-string, array{0: non-empty-list<Comment>}> | ||
*/ | ||
public function provideAlternativeNonemptyCommentArray(): array | ||
{ | ||
$data = $this->provideAlternativeCommentArray(); | ||
|
||
unset($data['no comment']); | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* This provider crosses two comment arrays (0, 1 or 2 comments) with different comments, | ||
* so that all combinations can be tested. | ||
* | ||
* @return array<non-empty-string, array{0: list<Comment>, 1: list<Comment>}> | ||
*/ | ||
public function provideTwoDistinctCommentArrays(): array | ||
{ | ||
return DataProviders::cross($this->provideCommentArray(), $this->provideAlternativeCommentArray()); | ||
} | ||
|
||
/** | ||
* @return array<non-empty-string, array{0: list<Comment>, 1: non-empty-list<Comment>}> | ||
*/ | ||
public function provideTwoDistinctCommentArraysWithSecondNonempty(): array | ||
{ | ||
return DataProviders::cross($this->provideCommentArray(), $this->provideAlternativeNonemptyCommentArray()); | ||
} | ||
|
||
private static function createContainsContstraint(Comment $comment): TraversableContains | ||
{ | ||
return new TraversableContains($comment); | ||
} | ||
|
||
/** | ||
* @param non-empty-list<Comment> $comments | ||
* | ||
* @return non-empty-list<TraversableContains> | ||
*/ | ||
private static function createContainsContstraints(array $comments): array | ||
{ | ||
return \array_map([self::class, 'createContainsContstraint'], $comments); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $commentsToAdd | ||
* @param non-empty-list<Comment> $originalComments | ||
* | ||
* @dataProvider provideTwoDistinctCommentArraysWithSecondNonempty | ||
*/ | ||
public function addCommentsKeepsOriginalComments(array $commentsToAdd, array $originalComments): void | ||
{ | ||
$this->subject->setComments($originalComments); | ||
|
||
$this->subject->addComments($commentsToAdd); | ||
|
||
self::assertThat( | ||
$this->subject->getComments(), | ||
LogicalAnd::fromConstraints(...self::createContainsContstraints($originalComments)) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $originalComments | ||
* @param non-empty-list<Comment> $commentsToAdd | ||
* | ||
* @dataProvider provideTwoDistinctCommentArraysWithSecondNonempty | ||
*/ | ||
public function addCommentsAfterCommentsSetAddsCommentsProvided(array $originalComments, array $commentsToAdd): void | ||
{ | ||
$this->subject->setComments($originalComments); | ||
|
||
$this->subject->addComments($commentsToAdd); | ||
|
||
self::assertThat( | ||
$this->subject->getComments(), | ||
LogicalAnd::fromConstraints(...self::createContainsContstraints($commentsToAdd)) | ||
); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param non-empty-list<Comment> $comments | ||
* | ||
* @dataProvider provideAlternativeNonemptyCommentArray | ||
*/ | ||
public function addCommentsAppends(array $comments): void | ||
{ | ||
$firstComment = new Comment('I must be first!'); | ||
$this->subject->setComments([$firstComment]); | ||
|
||
$this->subject->addComments($comments); | ||
|
||
$result = $this->subject->getComments(); | ||
self::assertNotEmpty($result); | ||
self::assertSame($firstComment, $result[0]); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $comments | ||
* | ||
* @dataProvider provideCommentArray | ||
*/ | ||
public function setCommentsOnVirginContainerSetsCommentsProvided(array $comments): void | ||
{ | ||
$this->subject->setComments($comments); | ||
|
||
self::assertSame($comments, $this->subject->getComments()); | ||
} | ||
|
||
/** | ||
* @test | ||
* | ||
* @param list<Comment> $originalComments | ||
* @param list<Comment> $commentsToSet | ||
* | ||
* @dataProvider provideTwoDistinctCommentArrays | ||
*/ | ||
public function setCommentsReplacesWithCommentsProvided(array $originalComments, array $commentsToSet): void | ||
{ | ||
$this->subject->setComments($originalComments); | ||
|
||
$this->subject->setComments($commentsToSet); | ||
|
||
self::assertSame($commentsToSet, $this->subject->getComments()); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sabberworm\CSS\Tests\Unit\Comment\Fixtures; | ||
|
||
use Sabberworm\CSS\Comment\Commentable; | ||
use Sabberworm\CSS\Comment\CommentContainer; | ||
|
||
final class ConcreteCommentContainer implements Commentable | ||
{ | ||
use CommentContainer; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.