|
| 1 | +<?php declare( strict_types=1 ); |
| 2 | + |
| 3 | +namespace Wikibase\Repo\Tests\Domains\Reuse\Architecture; |
| 4 | + |
| 5 | +use PHPat\Selector\Selector; |
| 6 | +use PHPat\Test\Builder\Rule; |
| 7 | +use PHPat\Test\PHPat; |
| 8 | +use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookup; |
| 9 | +use Wikibase\DataModel\Services\Lookup\PropertyDataTypeLookupException; |
| 10 | +use Wikibase\DataModel\Services\Statement\StatementGuidParser; |
| 11 | + |
| 12 | +/** |
| 13 | + * @coversNothing |
| 14 | + * |
| 15 | + * @license GPL-2.0-or-later |
| 16 | + */ |
| 17 | +class ArchitectureTest { |
| 18 | + |
| 19 | + private const REUSE_DOMAIN = 'Wikibase\Repo\Domains\Reuse'; |
| 20 | + private const DOMAIN_MODEL = self::REUSE_DOMAIN . '\Domain\Model'; |
| 21 | + private const DOMAIN_SERVICES = self::REUSE_DOMAIN . '\Domain\Services'; |
| 22 | + private const USE_CASES = self::REUSE_DOMAIN . '\Application\UseCases'; |
| 23 | + |
| 24 | + public function testDomainModel(): Rule { |
| 25 | + return PHPat::rule() |
| 26 | + ->classes( Selector::inNamespace( self::DOMAIN_MODEL ) ) |
| 27 | + ->canOnlyDependOn() |
| 28 | + ->classes( ...$this->allowedDomainModelDependencies() ); |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * Domain models may depend on: |
| 33 | + * - DataModel namespaces containing entities and their parts |
| 34 | + * - other classes from their own namespace |
| 35 | + */ |
| 36 | + private function allowedDomainModelDependencies(): array { |
| 37 | + return [ |
| 38 | + ...$this->dataModelNamespaces(), |
| 39 | + Selector::inNamespace( self::DOMAIN_MODEL ), |
| 40 | + ]; |
| 41 | + } |
| 42 | + |
| 43 | + public function testDomainServices(): Rule { |
| 44 | + return PHPat::rule() |
| 45 | + ->classes( Selector::inNamespace( self::DOMAIN_SERVICES ) ) |
| 46 | + ->canOnlyDependOn() |
| 47 | + ->classes( ...$this->allowedDomainServicesDependencies() ); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Domain services may depend on: |
| 52 | + * - the domain models namespace and everything it depends on |
| 53 | + * - some hand-picked DataModel services |
| 54 | + * - other classes from their own namespace |
| 55 | + */ |
| 56 | + private function allowedDomainServicesDependencies(): array { |
| 57 | + return [ |
| 58 | + ...$this->allowedDomainModelDependencies(), |
| 59 | + ...$this->allowedDataModelServices(), |
| 60 | + Selector::inNamespace( self::DOMAIN_SERVICES ), |
| 61 | + ]; |
| 62 | + } |
| 63 | + |
| 64 | + public function testUseCases(): Rule { |
| 65 | + return PHPat::rule() |
| 66 | + ->classes( Selector::inNamespace( self::USE_CASES ) ) |
| 67 | + ->canOnlyDependOn() |
| 68 | + ->classes( ...$this->allowedUseCasesDependencies() ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Use cases may depend on: |
| 73 | + * - the domain services namespace and everything it depends on |
| 74 | + * - other classes from their own namespace |
| 75 | + */ |
| 76 | + private function allowedUseCasesDependencies(): array { |
| 77 | + return [ |
| 78 | + ...$this->allowedDomainServicesDependencies(), |
| 79 | + Selector::inNamespace( self::USE_CASES ), |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + private function allowedDataModelServices(): array { |
| 84 | + return [ |
| 85 | + Selector::classname( PropertyDataTypeLookup::class ), |
| 86 | + Selector::classname( PropertyDataTypeLookupException::class ), |
| 87 | + Selector::classname( StatementGuidParser::class ), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + private function dataModelNamespaces(): array { |
| 92 | + return [ |
| 93 | + // These are listed in such a complicated way so that only DataModel entities and their parts are allowed without the |
| 94 | + // namespaces nested within DataModel like e.g. Wikibase\DataModel\Serializers. |
| 95 | + ...array_map( |
| 96 | + fn( string $escapedNamespace ) => Selector::classname( |
| 97 | + '/^' . preg_quote( $escapedNamespace ) . '\\\\\w+$/', |
| 98 | + true |
| 99 | + ), |
| 100 | + [ |
| 101 | + 'Wikibase\DataModel', |
| 102 | + 'Wikibase\DataModel\Entity', |
| 103 | + 'Wikibase\DataModel\Exception', |
| 104 | + 'Wikibase\DataModel\Snak', |
| 105 | + 'Wikibase\DataModel\Statement', |
| 106 | + 'Wikibase\DataModel\Term', |
| 107 | + ] |
| 108 | + ), |
| 109 | + Selector::inNamespace( 'DataValues' ), |
| 110 | + ]; |
| 111 | + } |
| 112 | + |
| 113 | +} |
0 commit comments