-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
For more details see https://issues.ibexa.co/browse/IBX-4918 or #196
- Loading branch information
1 parent
4a9ec46
commit 8d555ce
Showing
10 changed files
with
283 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Specification; | ||
|
||
abstract class AbstractSpecification implements SpecificationInterface | ||
{ | ||
abstract public function isSatisfiedBy($item): bool; | ||
|
||
public function and(SpecificationInterface $other): SpecificationInterface | ||
{ | ||
return new AndSpecification($this, $other); | ||
} | ||
|
||
public function or(SpecificationInterface $other): SpecificationInterface | ||
{ | ||
return new OrSpecification($this, $other); | ||
} | ||
|
||
public function not(): SpecificationInterface | ||
{ | ||
return new NotSpecification($this); | ||
} | ||
} |
This file contains 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,27 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Specification; | ||
|
||
final class AndSpecification extends AbstractSpecification | ||
{ | ||
private SpecificationInterface $one; | ||
|
||
private SpecificationInterface $two; | ||
|
||
public function __construct(SpecificationInterface $one, SpecificationInterface $two) | ||
{ | ||
$this->one = $one; | ||
$this->two = $two; | ||
} | ||
|
||
public function isSatisfiedBy($item): bool | ||
{ | ||
return $this->one->isSatisfiedBy($item) && $this->two->isSatisfiedBy($item); | ||
} | ||
} |
This file contains 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,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Specification; | ||
|
||
final class NotSpecification extends AbstractSpecification | ||
{ | ||
private SpecificationInterface $specification; | ||
|
||
public function __construct(SpecificationInterface $specification) | ||
{ | ||
$this->specification = $specification; | ||
} | ||
|
||
public function isSatisfiedBy($item): bool | ||
{ | ||
return !$this->specification->isSatisfiedBy($item); | ||
} | ||
} |
This file contains 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,27 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Specification; | ||
|
||
final class OrSpecification extends AbstractSpecification | ||
{ | ||
private SpecificationInterface $one; | ||
|
||
private SpecificationInterface $two; | ||
|
||
public function __construct(SpecificationInterface $one, SpecificationInterface $two) | ||
{ | ||
$this->one = $one; | ||
$this->two = $two; | ||
} | ||
|
||
public function isSatisfiedBy($item): bool | ||
{ | ||
return $this->one->isSatisfiedBy($item) || $this->two->isSatisfiedBy($item); | ||
} | ||
} |
This file contains 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,23 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\Core\Specification; | ||
|
||
interface SpecificationInterface | ||
{ | ||
/** | ||
* @param mixed $item | ||
*/ | ||
public function isSatisfiedBy($item): bool; | ||
|
||
public function and(SpecificationInterface $other): SpecificationInterface; | ||
|
||
public function or(SpecificationInterface $other): SpecificationInterface; | ||
|
||
public function not(): SpecificationInterface; | ||
} |
This file contains 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,49 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Core\Specification; | ||
|
||
class AbstractSpecificationTest extends BaseSpecificationTest | ||
{ | ||
public function testSpecification(): void | ||
{ | ||
$isStringSpecification = $this->getIsStringSpecification(); | ||
$isTestStringSpecification = $this->getIsTestStringSpecification(); | ||
|
||
self::assertTrue($isStringSpecification->isSatisfiedBy('test_string')); | ||
self::assertFalse($isStringSpecification->isSatisfiedBy(1234)); | ||
} | ||
|
||
public function testSpecificationAnd(): void | ||
{ | ||
$isStringSpecification = $this->getIsStringSpecification(); | ||
$isTestStringSpecification = $this->getIsTestStringSpecification(); | ||
|
||
self::assertTrue($isStringSpecification->and($isTestStringSpecification)->isSatisfiedBy('test')); | ||
self::assertFalse($isStringSpecification->and($isTestStringSpecification)->isSatisfiedBy('test_string')); | ||
self::assertFalse($isStringSpecification->and($isTestStringSpecification)->isSatisfiedBy(1234)); | ||
} | ||
|
||
public function testSpecificationOr(): void | ||
{ | ||
$isStringSpecification = $this->getIsStringSpecification(); | ||
$isTestStringSpecification = $this->getIsTestStringSpecification(); | ||
|
||
self::assertTrue($isStringSpecification->or($isTestStringSpecification)->isSatisfiedBy('test')); | ||
self::assertTrue($isStringSpecification->or($isTestStringSpecification)->isSatisfiedBy('test_string')); | ||
self::assertFalse($isStringSpecification->or($isTestStringSpecification)->isSatisfiedBy(1234)); | ||
} | ||
|
||
public function testSpecificationNot(): void | ||
{ | ||
$isStringSpecification = $this->getIsStringSpecification(); | ||
|
||
self::assertFalse($isStringSpecification->not()->isSatisfiedBy('test_string')); | ||
self::assertTrue($isStringSpecification->not()->isSatisfiedBy(1234)); | ||
} | ||
} |
This file contains 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,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Tests\Core\Specification; | ||
|
||
use Ibexa\Contracts\Core\Specification\AndSpecification; | ||
|
||
class AndSpecificationTest extends BaseSpecificationTest | ||
{ | ||
public function testAndSpecification(): void | ||
{ | ||
$andSpecification = new AndSpecification( | ||
$this->getIsStringSpecification(), | ||
$this->getIsTestStringSpecification() | ||
); | ||
|
||
self::assertTrue($andSpecification->isSatisfiedBy('test')); | ||
self::assertFalse($andSpecification->isSatisfiedBy('test_string')); | ||
self::assertFalse($andSpecification->isSatisfiedBy(1234)); | ||
} | ||
} |
This file contains 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,34 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Tests\Core\Specification; | ||
|
||
use Ibexa\Contracts\Core\Specification\AbstractSpecification; | ||
use Ibexa\Contracts\Core\Specification\SpecificationInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class BaseSpecificationTest extends TestCase | ||
{ | ||
protected function getIsStringSpecification(): SpecificationInterface | ||
{ | ||
return new class() extends AbstractSpecification { | ||
public function isSatisfiedBy($item): bool | ||
{ | ||
return is_string($item); | ||
} | ||
}; | ||
} | ||
|
||
protected function getIsTestStringSpecification(): SpecificationInterface | ||
{ | ||
return new class() extends AbstractSpecification { | ||
public function isSatisfiedBy($item): bool | ||
{ | ||
return $item === 'test'; | ||
} | ||
}; | ||
} | ||
} |
This file contains 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,22 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Tests\Core\Specification; | ||
|
||
use Ibexa\Contracts\Core\Specification\NotSpecification; | ||
|
||
class NotSpecificationTest extends BaseSpecificationTest | ||
{ | ||
public function testNotSpecification(): void | ||
{ | ||
$andSpecification = new NotSpecification( | ||
$this->getIsStringSpecification() | ||
); | ||
|
||
self::assertFalse($andSpecification->isSatisfiedBy('test')); | ||
self::assertTrue($andSpecification->isSatisfiedBy(1234)); | ||
} | ||
} |
This file contains 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,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
namespace Ibexa\Tests\Core\Specification; | ||
|
||
use Ibexa\Contracts\Core\Specification\OrSpecification; | ||
|
||
class OrSpecificationTest extends BaseSpecificationTest | ||
{ | ||
public function testOrSpecification(): void | ||
{ | ||
$andSpecification = new OrSpecification( | ||
$this->getIsStringSpecification(), | ||
$this->getIsTestStringSpecification() | ||
); | ||
|
||
self::assertTrue($andSpecification->isSatisfiedBy('test')); | ||
self::assertTrue($andSpecification->isSatisfiedBy('test_string')); | ||
self::assertFalse($andSpecification->isSatisfiedBy(1234)); | ||
} | ||
} |