Skip to content

Commit

Permalink
EZEE-3085: Added specification ContentTypeSpecification (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
michal-myszka authored Apr 21, 2020
1 parent 3ef7a6f commit 492324b
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\Specification\Content;

use eZ\Publish\API\Repository\Values\Content\Content;

final class ContentContainerSpecification implements ContentSpecification
{
public function isSatisfiedBy(Content $content): bool
{
return $content->getContentType()->isContainer;
}
}
16 changes: 16 additions & 0 deletions eZ/Publish/SPI/Specification/Content/ContentSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\Specification\Content;

use eZ\Publish\API\Repository\Values\Content\Content;

interface ContentSpecification
{
public function isSatisfiedBy(Content $content): bool;
}
29 changes: 29 additions & 0 deletions eZ/Publish/SPI/Specification/Content/ContentTypeSpecification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\Specification\Content;

use eZ\Publish\API\Repository\Values\Content\Content;

final class ContentTypeSpecification implements ContentSpecification
{
/**
* @var string
*/
private $expectedType;

public function __construct(string $expectedType)
{
$this->expectedType = $expectedType;
}

public function isSatisfiedBy(Content $content): bool
{
return $content->getContentType()->identifier === $this->expectedType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\Specification\Tests\Content;

use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\SPI\Specification\Content\ContentContainerSpecification;
use eZ\Publish\SPI\Specification\Content\ContentSpecification;
use PHPUnit\Framework\TestCase;

/**
* @covers \eZ\Publish\SPI\Specification\Content\ContentContainerSpecification
*/
final class ContentContainerSpecificationTest extends TestCase
{
public function testConstructor(): void
{
$contentTypeSpecification = new ContentContainerSpecification();

$this->assertInstanceOf(ContentSpecification::class, $contentTypeSpecification);
}

/**
* @covers \eZ\Publish\SPI\Specification\Content\ContentContainerSpecification::isSatisfiedBy
* @dataProvider providerForIsSatisfiedBy
*/
public function testIsSatisfiedBy(
bool $isContainer,
bool $shouldBeSatisfied
): void {
$contentContainerSpecification = new ContentContainerSpecification();

$contentTypeMock = $this->getMockBuilder(ContentType::class)
->setConstructorArgs(
[['isContainer' => $isContainer]]
)
->getMockForAbstractClass();

$contentMock = $this->createMock(Content::class);
$contentMock->expects($this->once())
->method('getContentType')
->willReturn($contentTypeMock);

$this->assertEquals(
$contentContainerSpecification->isSatisfiedBy($contentMock),
$shouldBeSatisfied
);
}

public function providerForIsSatisfiedBy(): array
{
return [
[true, true],
[false, false],
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace eZ\Publish\SPI\Specification\Tests\Content;

use eZ\Publish\API\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\ContentType\ContentType;
use eZ\Publish\SPI\Specification\Content\ContentSpecification;
use eZ\Publish\SPI\Specification\Content\ContentTypeSpecification;
use PHPUnit\Framework\TestCase;

final class ContentTypeSpecificationTest extends TestCase
{
private const EXISTING_CONTENT_TYPE_IDENTIFIER = 'article';
private const NOT_EXISTING_CONTENT_TYPE_IDENTIFIER = 'Some-Not-Existing-CT-Identifier';

public function testConstructorWithExistingContentTypeIdentifier(): void
{
$contentTypeSpecification = new ContentTypeSpecification(
self::EXISTING_CONTENT_TYPE_IDENTIFIER
);

$this->assertInstanceOf(ContentSpecification::class, $contentTypeSpecification);
}

public function testConstructorWithNotExistingContentTypeIdentifier(): void
{
$contentTypeSpecification = new ContentTypeSpecification(
self::NOT_EXISTING_CONTENT_TYPE_IDENTIFIER
);

$this->assertInstanceOf(ContentSpecification::class, $contentTypeSpecification);
}

/**
* @covers \eZ\Publish\SPI\Specification\Content\ContentTypeSpecification::isSatisfiedBy
* @dataProvider providerForIsSatisfiedBy
*/
public function testIsSatisfiedBy(
string $contentTypeSpecificationIdentifier,
string $contentTypeIdentifier,
bool $shouldBeSatisfied
): void {
$contentTypeSpecification = new ContentTypeSpecification(
$contentTypeSpecificationIdentifier
);

$contentTypeMock = $this->getMockBuilder(ContentType::class)
->setConstructorArgs(
[['identifier' => $contentTypeIdentifier]]
)
->getMockForAbstractClass();

$contentMock = $this->createMock(Content::class);
$contentMock->expects($this->once())
->method('getContentType')
->willReturn($contentTypeMock);

$this->assertEquals(
$contentTypeSpecification->isSatisfiedBy($contentMock),
$shouldBeSatisfied
);
}

public function providerForIsSatisfiedBy(): array
{
return [
[self::EXISTING_CONTENT_TYPE_IDENTIFIER, self::EXISTING_CONTENT_TYPE_IDENTIFIER, true],
[self::NOT_EXISTING_CONTENT_TYPE_IDENTIFIER, self::EXISTING_CONTENT_TYPE_IDENTIFIER, false],
];
}
}

0 comments on commit 492324b

Please sign in to comment.