diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 94e053d031..c97e2329b6 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -24605,16 +24605,6 @@ parameters: count: 1 path: tests/bundle/Core/EventListener/OriginalRequestListenerTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\RejectExplicitFrontControllerRequestsListenerTest\\:\\:prohibitedRequestDataProvider\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\RejectExplicitFrontControllerRequestsListenerTest\\:\\:validRequestDataProvider\\(\\) return type has no value type specified in iterable type array\\.$#" - count: 1 - path: tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Bundle\\\\Core\\\\EventListener\\\\RequestEventListenerTest\\:\\:testOnKernelRequestForward\\(\\) has no return type specified\\.$#" count: 1 @@ -56670,76 +56660,11 @@ parameters: count: 1 path: tests/lib/Repository/Permission/PermissionCriterionResolverTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:getEventDispatcher\\(\\) should return PHPUnit\\\\Framework\\\\MockObject\\\\MockObject&Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface but returns Symfony\\\\Contracts\\\\EventDispatcher\\\\EventDispatcherInterface\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:getRelationProcessorMock\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:getRepository\\(\\) has parameter \\$serviceSettings with no value type specified in iterable type array\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Parameter \\#1 \\$className of method PHPUnit\\\\Framework\\\\TestCase\\:\\:getMockBuilder\\(\\) expects class\\-string\\, string given\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - message: "#^Parameter \\#2 \\$searchHandler of class Ibexa\\\\Core\\\\Repository\\\\Repository constructor expects Ibexa\\\\Contracts\\\\Core\\\\Search\\\\Handler, PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" count: 1 path: tests/lib/Repository/Service/Mock/Base.php - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$contentDomainMapperMock \\(Ibexa\\\\Core\\\\Repository\\\\Mapper\\\\ContentDomainMapper&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$contentTypeDomainMapperMock \\(Ibexa\\\\Core\\\\Repository\\\\Mapper\\\\ContentTypeDomainMapper&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$fieldTypeRegistryMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$fieldTypeServiceMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$permissionServiceMock \\(Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\PermissionService&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$persistenceMock \\(Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Handler&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$repositoryMock \\(Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Repository&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\Base\\:\\:\\$thumbnailStrategyMock \\(Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\Strategy\\\\ContentThumbnail\\\\ThumbnailStrategy&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject\\) in isset\\(\\) is not nullable\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - - - message: "#^Unable to resolve the template type T in call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:getMockBuilder\\(\\)$#" - count: 1 - path: tests/lib/Repository/Service/Mock/Base.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\BookmarkTest\\:\\:createBookmarkService\\(\\) has parameter \\$methods with no value type specified in iterable type array\\.$#" count: 1 @@ -57485,41 +57410,11 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/ContentTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfo\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoById\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoByIdAndVersionNumber\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoByIdNonPublishedVersion\\(\\) has no return type specified\\.$#" count: 1 path: tests/lib/Repository/Service/Mock/ContentTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoByIdPublishedVersion\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoByIdThrowsNotFoundException\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:testUpdateContentNonRedundantFieldSet1\\(\\) has no return type specified\\.$#" count: 1 @@ -57735,11 +57630,6 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/ContentTest.php - - - message: "#^PHPDoc tag @var for property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:\\$nameSchemaServiceMock with type PHPUnit\\\\Framework\\\\MockObject\\\\MockObject is not subtype of native type Ibexa\\\\Contracts\\\\Core\\\\Repository\\\\NameSchema\\\\NameSchemaServiceInterface\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - message: "#^Parameter \\#1 \\$languageHandlerMock of method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:loadByLanguageCodeMock\\(\\) expects PHPUnit\\\\Framework\\\\MockObject\\\\MockObject, Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Language\\\\Handler given\\.$#" count: 1 @@ -57770,11 +57660,6 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/ContentTest.php - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\ContentTest\\:\\:\\$relationProcessorMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/ContentTest.php - - message: "#^Call to an undefined method Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\User\\\\Handler\\:\\:expects\\(\\)\\.$#" count: 11 @@ -57790,21 +57675,11 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:getPermissionResolverMock\\(\\) has parameter \\$methods with no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:getPermissionSetsMock\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:getUserReferenceMock\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:providerForTestCanUserComplex\\(\\) return type has no value type specified in iterable type array\\.$#" count: 1 @@ -57985,11 +57860,6 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:testSetAndGetCurrentUserReference\\(\\) has no return type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - message: "#^PHPDoc tag @var has invalid value \\(\\$policy \\\\Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\User\\\\Policy\\)\\: Unexpected token \"\\$policy\", expected type at offset 9$#" count: 1 @@ -58020,31 +57890,11 @@ parameters: count: 3 path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:\\$permissionResolverMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:\\$repositoryMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionTest\\:\\:\\$userReferenceMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionTest.php - - message: "#^Variable \\$limitationsPass might not be defined\\.$#" count: 1 path: tests/lib/Repository/Service/Mock/PermissionTest.php - - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionsCriterionHandlerTest\\:\\:getPermissionResolverMock\\(\\) has parameter \\$methods with no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php - - message: "#^Method Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionsCriterionHandlerTest\\:\\:mockServices\\(\\) has no return type specified\\.$#" count: 1 @@ -58150,11 +58000,6 @@ parameters: count: 1 path: tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php - - - message: "#^Property Ibexa\\\\Tests\\\\Core\\\\Repository\\\\Service\\\\Mock\\\\PermissionsCriterionHandlerTest\\:\\:\\$permissionResolverMock has no type specified\\.$#" - count: 1 - path: tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php - - message: "#^Call to an undefined method Ibexa\\\\Contracts\\\\Core\\\\Persistence\\\\Content\\\\Location\\\\Handler\\:\\:expects\\(\\)\\.$#" count: 2 diff --git a/tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php b/tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php index 394da78c70..82f8a7b24e 100644 --- a/tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php +++ b/tests/bundle/Core/EventListener/RejectExplicitFrontControllerRequestsListenerTest.php @@ -9,6 +9,7 @@ namespace Ibexa\Tests\Bundle\Core\EventListener; use Ibexa\Bundle\Core\EventListener\RejectExplicitFrontControllerRequestsListener; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Event\RequestEvent; @@ -16,13 +17,11 @@ use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\KernelEvents; -class RejectExplicitFrontControllerRequestsListenerTest extends TestCase +final class RejectExplicitFrontControllerRequestsListenerTest extends TestCase { - /** @var \Ibexa\Bundle\Core\EventListener\RejectExplicitFrontControllerRequestsListener */ - private $eventListener; + private RejectExplicitFrontControllerRequestsListener $eventListener; - /** @var \Symfony\Component\HttpKernel\HttpKernelInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $httpKernel; + private HttpKernelInterface & MockObject $httpKernel; protected function setUp(): void { @@ -76,210 +75,93 @@ public function testOnKernelRequestThrowsException(Request $request): void $this->eventListener->onKernelRequest($event); } - public function validRequestDataProvider(): array + /** + * @return iterable + */ + public function validRequestDataProvider(): iterable { - return [ - [ - Request::create( - 'https://example.com', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/admin/dashboard', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/admin/dashboard', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/admin/dashboard/', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/admin/dashboard/', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/Folder/Content', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/Folder/Content', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/Folder/Content/', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/Folder/Content/', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php-foo', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/app.php-foo', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php.foo', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php.foo', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/folder/folder/app.php', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/folder/folder/app.php', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], + yield 'site root' => [ + $this->buildRequest('https://example.com', 'https://example.com/app.php'), + ]; + + yield 'site root with slash' => [ + $this->buildRequest('https://example.com/', 'https://example.com/app.php/'), + ]; + + yield 'with path' => [ + $this->buildRequest('https://example.com/admin/dashboard', 'https://example.com/app.php/admin/dashboard'), + ]; + + yield 'with path with slash' => [ + $this->buildRequest('https://example.com/admin/dashboard/', 'https://example.com/app.php/admin/dashboard/'), + ]; + + yield 'with capital leading letter path' => [ + $this->buildRequest('https://example.com/Folder/Content', 'https://example.com/app.php/Folder/Content'), + ]; + + yield 'with capital leading letter path with slash' => [ + $this->buildRequest('https://example.com/Folder/Content/', 'https://example.com/app.php/Folder/Content/'), + ]; + + yield 'with php-foo extension' => [ + $this->buildRequest('https://example.com/app.php-foo', 'https://example.com/app.php/app.php-foo'), + ]; + + yield 'with php.foo extension' => [ + $this->buildRequest('https://example.com/app.php.foo', 'https://example.com/app.php.foo'), + ]; + + yield 'with php extension' => [ + $this->buildRequest( + 'https://example.com/folder/folder/app.php', + 'https://example.com/app.php/folder/folder/app.php' + ), ]; } - public function prohibitedRequestDataProvider(): array + /** + * @return iterable + */ + public function prohibitedRequestDataProvider(): iterable { - return [ - [ - Request::create( - 'https://example.com/app.php', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php/app.php', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/app.php', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/folder/app.php', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/folder/app.php', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php/foo', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/app.php/foo', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php?foo=bar', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/app.php?foo=bar', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], - [ - Request::create( - 'https://example.com/app.php#foo', - 'GET', - [], - [], - [], - [ - 'REQUEST_URI' => 'https://example.com/app.php/app.php#foo', - 'SCRIPT_FILENAME' => 'app.php', - ] - ), - ], + yield 'with explicit front controller' => [ + $this->buildRequest('https://example.com/app.php', 'https://example.com/app.php'), + ]; + + yield 'with front controller in path' => [ + $this->buildRequest('https://example.com/app.php/app.php', 'https://example.com/app.php/app.php'), + ]; + + yield 'with an arbitrary path' => [ + $this->buildRequest('https://example.com/folder/app.php', 'https://example.com/app.php/folder/app.php'), + ]; + + yield 'with path after front controller' => [ + $this->buildRequest('https://example.com/app.php/foo', 'https://example.com/app.php/app.php/foo'), ]; + + yield 'with query parameter' => [ + $this->buildRequest('https://example.com/app.php?foo=bar', 'https://example.com/app.php/app.php?foo=bar'), + ]; + + yield 'with fragment' => [ + $this->buildRequest('https://example.com/app.php#foo', 'https://example.com/app.php/app.php#foo'), + ]; + } + + private function buildRequest(string $uri, string $requestUri): Request + { + return Request::create( + $uri, + Request::METHOD_GET, + [], + [], + [], + [ + 'REQUEST_URI' => $requestUri, + 'SCRIPT_FILENAME' => 'app.php', + ] + ); } } diff --git a/tests/lib/Repository/Service/Mock/Base.php b/tests/lib/Repository/Service/Mock/Base.php index 7ab15416c9..e09bd6e0a9 100644 --- a/tests/lib/Repository/Service/Mock/Base.php +++ b/tests/lib/Repository/Service/Mock/Base.php @@ -9,15 +9,17 @@ use Ibexa\Contracts\Core\Persistence\Filter\Content\Handler as ContentFilteringHandler; use Ibexa\Contracts\Core\Persistence\Filter\Location\Handler as LocationFilteringHandler; -use Ibexa\Contracts\Core\Persistence\Handler; +use Ibexa\Contracts\Core\Persistence\Handler as PersistenceHandler; use Ibexa\Contracts\Core\Repository\LanguageResolver; use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface; use Ibexa\Contracts\Core\Repository\PasswordHashService; +use Ibexa\Contracts\Core\Repository\PermissionResolver; use Ibexa\Contracts\Core\Repository\PermissionService; use Ibexa\Contracts\Core\Repository\Repository as APIRepository; use Ibexa\Contracts\Core\Repository\Strategy\ContentThumbnail\ThumbnailStrategy; use Ibexa\Contracts\Core\Repository\Validator\ContentValidator; use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo; +use Ibexa\Contracts\Core\Repository\Values\User\User as APIUser; use Ibexa\Contracts\Core\SiteAccess\ConfigResolverInterface; use Ibexa\Core\FieldType\FieldTypeRegistry; use Ibexa\Core\Repository\FieldTypeService; @@ -47,67 +49,57 @@ */ abstract class Base extends TestCase { - /** @var \Ibexa\Contracts\Core\Repository\Repository */ - private $repository; + private APIRepository $repository; - /** @var \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject */ - private $repositoryMock; + private MockObject & APIRepository $repositoryMock; - /** @var \Ibexa\Contracts\Core\Repository\PermissionService|\PHPUnit\Framework\MockObject\MockObject */ - private $permissionServiceMock; + private MockObject & PermissionService $permissionServiceMock; - /** @var \Ibexa\Contracts\Core\Persistence\Handler|\PHPUnit\Framework\MockObject\MockObject */ - private $persistenceMock; + private MockObject & PersistenceHandler $persistenceMock; - /** @var \Ibexa\Contracts\Core\Repository\Strategy\ContentThumbnail\ThumbnailStrategy|\PHPUnit\Framework\MockObject\MockObject */ - private $thumbnailStrategyMock; + private MockObject & ThumbnailStrategy $thumbnailStrategyMock; /** - * The Content / Location / Search ... handlers for the persistence / Search / .. handler mocks. + * The Content / Location / Search ... handlers for the persistence / Search / ... handler mocks. * - * @var \PHPUnit\Framework\MockObject\MockObject[] Key is relative to "Ibexa\Contracts\Core\" + * @var array Key is relative to "Ibexa\Contracts\Core\" * * @see getPersistenceMockHandler() */ - private $spiMockHandlers = []; + private array $spiMockHandlers = []; - /** @var \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper */ - private $contentTypeDomainMapperMock; + private MockObject & ContentTypeDomainMapper $contentTypeDomainMapperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Mapper\ContentDomainMapper */ - private $contentDomainMapperMock; + private MockObject & ContentDomainMapper $contentDomainMapperMock; - /** @var \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Permission\LimitationService */ - private $limitationServiceMock; + private MockObject & LimitationService $limitationServiceMock; - /** @var \Ibexa\Contracts\Core\Repository\LanguageResolver|\PHPUnit\Framework\MockObject\MockObject */ - private $languageResolverMock; + private MockObject & LanguageResolver $languageResolverMock; - /** @var \Ibexa\Core\Repository\Mapper\RoleDomainMapper|\PHPUnit\Framework\MockObject\MockObject */ - protected $roleDomainMapperMock; + protected MockObject & RoleDomainMapper $roleDomainMapperMock; - /** @var \Ibexa\Core\Repository\Mapper\ContentMapper|\PHPUnit\Framework\MockObject\MockObject */ - protected $contentMapperMock; + protected MockObject & ContentMapper $contentMapperMock; - /** @var \Ibexa\Contracts\Core\Repository\Validator\ContentValidator|\PHPUnit\Framework\MockObject\MockObject */ - protected $contentValidatorStrategyMock; + protected MockObject & ContentValidator $contentValidatorStrategyMock; - /** @var \Ibexa\Contracts\Core\Persistence\Filter\Content\Handler|\PHPUnit\Framework\MockObject\MockObject */ - private $contentFilteringHandlerMock; + private MockObject & ContentFilteringHandler $contentFilteringHandlerMock; - /** @var \Ibexa\Contracts\Core\Persistence\Filter\Location\Handler|\PHPUnit\Framework\MockObject\MockObject */ - private $locationFilteringHandlerMock; + private MockObject & LocationFilteringHandler $locationFilteringHandlerMock; + + protected MockObject & FieldTypeService $fieldTypeServiceMock; + + protected MockObject & FieldTypeRegistry $fieldTypeRegistryMock; + + protected MockObject & EventDispatcherInterface $eventDispatcher; /** * Get Real repository with mocked dependencies. * - * @param array $serviceSettings If set then non shared instance of Repository is returned - * - * @return \Ibexa\Contracts\Core\Repository\Repository + * @param array $serviceSettings If set then non-shared instance of Repository is returned */ - protected function getRepository(array $serviceSettings = []) + protected function getRepository(array $serviceSettings = []): APIRepository { - if ($this->repository === null || !empty($serviceSettings)) { + if (!isset($this->repository) || !empty($serviceSettings)) { $repository = new Repository( $this->getPersistenceMock(), $this->getSPIMockHandler('Search\\Handler'), @@ -143,12 +135,7 @@ protected function getRepository(array $serviceSettings = []) return $this->repository; } - protected $fieldTypeServiceMock; - - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Contracts\Core\Repository\FieldTypeService - */ - protected function getFieldTypeServiceMock() + protected function getFieldTypeServiceMock(): MockObject & FieldTypeService { if (!isset($this->fieldTypeServiceMock)) { $this->fieldTypeServiceMock = $this->createMock(FieldTypeService::class); @@ -157,12 +144,7 @@ protected function getFieldTypeServiceMock() return $this->fieldTypeServiceMock; } - protected $fieldTypeRegistryMock; - - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\FieldType\FieldTypeRegistry - */ - protected function getFieldTypeRegistryMock() + protected function getFieldTypeRegistryMock(): MockObject & FieldTypeRegistry { if (!isset($this->fieldTypeRegistryMock)) { $this->fieldTypeRegistryMock = $this->createMock(FieldTypeRegistry::class); @@ -171,12 +153,7 @@ protected function getFieldTypeRegistryMock() return $this->fieldTypeRegistryMock; } - protected EventDispatcherInterface $eventDispatcher; - - /** - * @return \Symfony\Contracts\EventDispatcher\EventDispatcherInterface&\PHPUnit\Framework\MockObject\MockObject - */ - protected function getEventDispatcher(): EventDispatcherInterface + protected function getEventDispatcher(): MockObject & EventDispatcherInterface { if (!isset($this->eventDispatcher)) { $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class); @@ -197,10 +174,7 @@ protected function getThumbnailStrategy() return $this->thumbnailStrategyMock; } - /** - * @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getRepositoryMock() + protected function getRepositoryMock(): MockObject & APIRepository { if (!isset($this->repositoryMock)) { $this->repositoryMock = $this->createMock(APIRepository::class); @@ -209,18 +183,12 @@ protected function getRepositoryMock() return $this->repositoryMock; } - /** - * @return \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getPermissionResolverMock() + protected function getPermissionResolverMock(): MockObject & PermissionResolver { return $this->getPermissionServiceMock(); } - /** - * @return \Ibexa\Contracts\Core\Repository\PermissionService|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getPermissionServiceMock(): PermissionService + protected function getPermissionServiceMock(): MockObject & PermissionService { if (!isset($this->permissionServiceMock)) { $this->permissionServiceMock = $this->createMock(PermissionService::class); @@ -229,10 +197,7 @@ protected function getPermissionServiceMock(): PermissionService return $this->permissionServiceMock; } - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Mapper\ContentDomainMapper - */ - protected function getContentDomainMapperMock(): MockObject + protected function getContentDomainMapperMock(): MockObject & ContentDomainMapper { if (!isset($this->contentDomainMapperMock)) { $this->contentDomainMapperMock = $this->createMock(ContentDomainMapper::class); @@ -241,10 +206,7 @@ protected function getContentDomainMapperMock(): MockObject return $this->contentDomainMapperMock; } - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Mapper\ContentTypeDomainMapper - */ - protected function getContentTypeDomainMapperMock() + protected function getContentTypeDomainMapperMock(): MockObject & ContentTypeDomainMapper { if (!isset($this->contentTypeDomainMapperMock)) { $this->contentTypeDomainMapperMock = $this->createMock(ContentTypeDomainMapper::class); @@ -253,65 +215,60 @@ protected function getContentTypeDomainMapperMock() return $this->contentTypeDomainMapperMock; } - /** - * Returns a persistence Handler mock. - * - * @return \Ibexa\Contracts\Core\Persistence\Handler|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getPersistenceMock() + protected function getPersistenceMock(): MockObject & PersistenceHandler { if (!isset($this->persistenceMock)) { - $this->persistenceMock = $this->createMock(Handler::class); + $this->persistenceMock = $this->createMock(PersistenceHandler::class); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('contentHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('contentTypeHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Type\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Type\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('contentLanguageHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Language\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Language\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('locationHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Location\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Location\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('objectStateHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\ObjectState\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\ObjectState\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('trashHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Location\\Trash\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Location\\Trash\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('userHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('User\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('User\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('sectionHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\Section\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\Section\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('urlAliasHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\UrlAlias\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\UrlAlias\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('urlWildcardHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('Content\\UrlWildcard\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('Content\\UrlWildcard\\Handler')); - $this->persistenceMock->expects(self::any()) + $this->persistenceMock ->method('urlWildcardHandler') - ->will(self::returnValue($this->getPersistenceMockHandler('URL\\Handler'))); + ->willReturn($this->getPersistenceMockHandler('URL\\Handler')); } return $this->persistenceMock; } - protected function getRelationProcessorMock() + protected function getRelationProcessorMock(): MockObject & RelationProcessor { return $this->createMock(RelationProcessor::class); } @@ -323,14 +280,12 @@ protected function getRelationProcessorMock() * * @return \PHPUnit\Framework\MockObject\MockObject */ - protected function getSPIMockHandler($handler) + protected function getSPIMockHandler(string $handler): MockObject { if (!isset($this->spiMockHandlers[$handler])) { - $this->spiMockHandlers[$handler] = $this->getMockBuilder("Ibexa\\Contracts\\Core\\{$handler}") - ->setMethods([]) - ->disableOriginalConstructor() - ->setConstructorArgs([]) - ->getMock(); + $interfaceFQCN = "Ibexa\\Contracts\\Core\\$handler"; + self::assertTrue(interface_exists($interfaceFQCN), "Interface $interfaceFQCN does not exist"); + $this->spiMockHandlers[$handler] = $this->createMock($interfaceFQCN); } return $this->spiMockHandlers[$handler]; @@ -343,19 +298,12 @@ protected function getSPIMockHandler($handler) * * @return \PHPUnit\Framework\MockObject\MockObject */ - protected function getPersistenceMockHandler($handler) + protected function getPersistenceMockHandler(string $handler): MockObject { - return $this->getSPIMockHandler("Persistence\\{$handler}"); + return $this->getSPIMockHandler("Persistence\\$handler"); } - /** - * Returns User stub with $id as User/Content id. - * - * @param int $id - * - * @return \Ibexa\Contracts\Core\Repository\Values\User\User - */ - protected function getStubbedUser($id) + protected function getStubbedUser(int $id): APIUser { return new User( [ @@ -373,12 +321,9 @@ protected function getStubbedUser($id) ); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Permission\LimitationService - */ - protected function getLimitationServiceMock(): MockObject + protected function getLimitationServiceMock(): MockObject & LimitationService { - if ($this->limitationServiceMock === null) { + if (!isset($this->limitationServiceMock)) { $this->limitationServiceMock = $this->createMock(LimitationService::class); } @@ -387,7 +332,7 @@ protected function getLimitationServiceMock(): MockObject protected function getLanguageResolverMock(): LanguageResolver { - if ($this->languageResolverMock === null) { + if (!isset($this->languageResolverMock)) { $this->languageResolverMock = $this->createMock(LanguageResolver::class); } @@ -396,12 +341,10 @@ protected function getLanguageResolverMock(): LanguageResolver /** * @param string[] $methods - * - * @return \Ibexa\Core\Repository\Mapper\RoleDomainMapper|\PHPUnit\Framework\MockObject\MockObject */ - protected function getRoleDomainMapperMock(array $methods = []): RoleDomainMapper + protected function getRoleDomainMapperMock(array $methods = []): MockObject & RoleDomainMapper { - if ($this->roleDomainMapperMock === null) { + if (!isset($this->roleDomainMapperMock)) { $mockBuilder = $this->getMockBuilder(RoleDomainMapper::class); if (!empty($methods)) { $mockBuilder->onlyMethods($methods); @@ -444,7 +387,7 @@ protected function getContentValidatorStrategy(): ContentValidator protected function getContentFilteringHandlerMock(): ContentFilteringHandler { - if (null === $this->contentFilteringHandlerMock) { + if (!isset($this->contentFilteringHandlerMock)) { $this->contentFilteringHandlerMock = $this->createMock(ContentFilteringHandler::class); } @@ -453,7 +396,7 @@ protected function getContentFilteringHandlerMock(): ContentFilteringHandler private function getLocationFilteringHandlerMock(): LocationFilteringHandler { - if (null === $this->locationFilteringHandlerMock) { + if (!isset($this->locationFilteringHandlerMock)) { $this->locationFilteringHandlerMock = $this->createMock(LocationFilteringHandler::class); } diff --git a/tests/lib/Repository/Service/Mock/ContentTest.php b/tests/lib/Repository/Service/Mock/ContentTest.php index 96c2defefd..1f0d6032b5 100644 --- a/tests/lib/Repository/Service/Mock/ContentTest.php +++ b/tests/lib/Repository/Service/Mock/ContentTest.php @@ -16,6 +16,7 @@ use Ibexa\Contracts\Core\Persistence\Content\ContentInfo as SPIContentInfo; use Ibexa\Contracts\Core\Persistence\Content\CreateStruct as SPIContentCreateStruct; use Ibexa\Contracts\Core\Persistence\Content\Field as SPIField; +use Ibexa\Contracts\Core\Persistence\Content\Handler as LegacyContentHandler; use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Persistence\Content\MetadataUpdateStruct as SPIMetadataUpdateStruct; use Ibexa\Contracts\Core\Persistence\Content\ObjectState as SPIObjectState; @@ -29,6 +30,7 @@ use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException; use Ibexa\Contracts\Core\Repository\LocationService as APILocationService; use Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface; +use Ibexa\Contracts\Core\Repository\PermissionResolver; use Ibexa\Contracts\Core\Repository\Repository; use Ibexa\Contracts\Core\Repository\Values\Content\Content as APIContent; use Ibexa\Contracts\Core\Repository\Values\Content\ContentCreateStruct as APIContentCreateStruct; @@ -48,6 +50,7 @@ use Ibexa\Core\FieldType\Value; use Ibexa\Core\Repository\ContentService; use Ibexa\Core\Repository\Helper\RelationProcessor; +use Ibexa\Core\Repository\Mapper\ContentDomainMapper; use Ibexa\Core\Repository\Values\Content\Content; use Ibexa\Core\Repository\Values\Content\ContentCreateStruct; use Ibexa\Core\Repository\Values\Content\ContentUpdateStruct; @@ -58,20 +61,22 @@ use Ibexa\Core\Repository\Values\ContentType\FieldDefinitionCollection; use Ibexa\Core\Repository\Values\User\UserReference; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; /** - * Mock test case for Content service. + * @covers \Ibexa\Core\Repository\ContentService */ class ContentTest extends BaseServiceMockTest { - private const EMPTY_FIELD_VALUE = 'empty'; + private const string EMPTY_FIELD_VALUE = 'empty'; + private const string NAME_SCHEMA = ''; + private const string STORE_FAILED_EXPECTED_EXCEPTION_MESSAGE = 'Store failed'; - /** - * Test for the __construct() method. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::__construct - */ - public function testConstructor(): void + protected MockObject & RelationProcessor $relationProcessorMock; + + protected MockObject & NameSchemaServiceInterface $nameSchemaServiceMock; + + public function testConstructor(): ContentService { $repositoryMock = $this->getRepositoryMock(); /** @var \Ibexa\Contracts\Core\Persistence\Handler $persistenceHandlerMock */ @@ -89,7 +94,7 @@ public function testConstructor(): void 'remove_archived_versions_on_publish' => true, ]; - new ContentService( + return new ContentService( $repositoryMock, $persistenceHandlerMock, $contentDomainMapperMock, @@ -104,12 +109,7 @@ public function testConstructor(): void ); } - /** - * Test for the loadVersionInfo() method, of published version. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfoById - */ - public function testLoadVersionInfoById() + public function testLoadVersionInfoById(): void { $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ @@ -130,14 +130,14 @@ public function testLoadVersionInfoById() ->with( self::equalTo(42), self::equalTo(null) - )->will( - self::returnValue(new SPIVersionInfo()) + )->willReturn( + new SPIVersionInfo() ); $domainMapperMock->expects(self::once()) ->method('buildVersionInfoDomainObject') ->with(new SPIVersionInfo()) - ->will(self::returnValue($versionInfoMock)); + ->willReturn($versionInfoMock); $permissionResolver->expects(self::once()) ->method('canUser') @@ -145,7 +145,7 @@ public function testLoadVersionInfoById() self::equalTo('content'), self::equalTo('read'), self::equalTo($versionInfoMock) - )->will(self::returnValue(true)); + )->willReturn(true); $result = $contentServiceMock->loadVersionInfoById(42); @@ -156,10 +156,8 @@ public function testLoadVersionInfoById() * Test for the loadVersionInfo() method, of a draft. * * @depends testLoadVersionInfoById - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfoById */ - public function testLoadVersionInfoByIdAndVersionNumber() + public function testLoadVersionInfoByIdAndVersionNumber(): void { $permissionResolver = $this->getPermissionResolverMock(); $contentServiceMock = $this->getPartlyMockedContentService(['loadContentInfo']); @@ -168,7 +166,7 @@ public function testLoadVersionInfoByIdAndVersionNumber() $domainMapperMock = $this->getContentDomainMapperMock(); $versionInfoMock = $this->createMock(APIVersionInfo::class); - $versionInfoMock->expects(self::any()) + $versionInfoMock ->method('__get') ->with('status') ->willReturn(APIVersionInfo::STATUS_DRAFT); @@ -201,12 +199,7 @@ public function testLoadVersionInfoByIdAndVersionNumber() self::assertEquals($versionInfoMock, $result); } - /** - * Test for the loadVersionInfo() method. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfoById - */ - public function testLoadVersionInfoByIdThrowsNotFoundException() + public function testLoadVersionInfoByIdThrowsNotFoundException(): void { $this->expectException(NotFoundException::class); @@ -235,89 +228,54 @@ public function testLoadVersionInfoByIdThrowsNotFoundException() } /** - * Test for the loadVersionInfo() method. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfoById + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException */ - public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion() + public function testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion(): void { $this->expectException(UnauthorizedException::class); $contentServiceMock = $this->getPartlyMockedContentService(); - /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Contracts\Core\Persistence\Content\Handler $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); $domainMapperMock = $this->getContentDomainMapperMock(); $versionInfoMock = $this->createMock(APIVersionInfo::class); $permissionResolver = $this->getPermissionResolverMock(); - $versionInfoMock->expects(self::any()) - ->method('isPublished') - ->willReturn(false); - - $contentHandler->expects(self::once()) - ->method('loadVersionInfo') - ->with( - self::equalTo(42), - self::equalTo(24) - )->will( - self::returnValue(new SPIVersionInfo()) - ); - - $domainMapperMock->expects(self::once()) - ->method('buildVersionInfoDomainObject') - ->with(new SPIVersionInfo()) - ->will(self::returnValue($versionInfoMock)); - - $permissionResolver->expects(self::once()) - ->method('canUser') - ->with( - self::equalTo('content'), - self::equalTo('versionread'), - self::equalTo($versionInfoMock) - )->will(self::returnValue(false)); + $this->configureMocksForLoadVersionInfoById( + false, + 'versionread', + false, + $versionInfoMock, + $contentHandler, + $domainMapperMock, + $permissionResolver + ); $contentServiceMock->loadVersionInfoById(42, 24); } /** - * Test for the loadVersionInfo() method. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfoById + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException + * @throws \Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException */ - public function testLoadVersionInfoByIdPublishedVersion() + public function testLoadVersionInfoByIdPublishedVersion(): void { $contentServiceMock = $this->getPartlyMockedContentService(); - /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Contracts\Core\Persistence\Content\Handler $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); $domainMapperMock = $this->getContentDomainMapperMock(); $versionInfoMock = $this->createMock(APIVersionInfo::class); $permissionResolver = $this->getPermissionResolverMock(); - $versionInfoMock->expects(self::once()) - ->method('isPublished') - ->willReturn(true); - - $contentHandler->expects(self::once()) - ->method('loadVersionInfo') - ->with( - self::equalTo(42), - self::equalTo(24) - )->will( - self::returnValue(new SPIVersionInfo()) - ); - - $domainMapperMock->expects(self::once()) - ->method('buildVersionInfoDomainObject') - ->with(new SPIVersionInfo()) - ->will(self::returnValue($versionInfoMock)); - - $permissionResolver->expects(self::once()) - ->method('canUser') - ->with( - self::equalTo('content'), - self::equalTo('read'), - self::equalTo($versionInfoMock) - )->will(self::returnValue(true)); + $this->configureMocksForLoadVersionInfoById( + true, + 'read', + true, + $versionInfoMock, + $contentHandler, + $domainMapperMock, + $permissionResolver + ); $result = $contentServiceMock->loadVersionInfoById(42, 24); @@ -332,55 +290,28 @@ public function testLoadVersionInfoByIdPublishedVersion() public function testLoadVersionInfoByIdNonPublishedVersion() { $contentServiceMock = $this->getPartlyMockedContentService(); - /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ + /** @var \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Contracts\Core\Persistence\Content\Handler $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); $domainMapperMock = $this->getContentDomainMapperMock(); $versionInfoMock = $this->createMock(APIVersionInfo::class); $permissionResolver = $this->getPermissionResolverMock(); - $versionInfoMock->expects(self::once()) - ->method('isPublished') - ->willReturn(false); - - $contentHandler->expects(self::once()) - ->method('loadVersionInfo') - ->with( - self::equalTo(42), - self::equalTo(24) - )->will( - self::returnValue(new SPIVersionInfo()) - ); - - $domainMapperMock->expects(self::once()) - ->method('buildVersionInfoDomainObject') - ->with(new SPIVersionInfo()) - ->will(self::returnValue($versionInfoMock)); - - $permissionResolver->expects(self::once()) - ->method('canUser') - ->with( - self::equalTo('content'), - self::equalTo('versionread'), - self::equalTo($versionInfoMock) - )->will(self::returnValue(true)); + $this->configureMocksForLoadVersionInfoById( + false, + 'versionread', + true, + $versionInfoMock, + $contentHandler, + $domainMapperMock, + $permissionResolver + ); $result = $contentServiceMock->loadVersionInfoById(42, 24); self::assertEquals($versionInfoMock, $result); } - /** - * Test for the loadVersionInfo() method. - * - * @covers \Ibexa\Contracts\Core\Repository\ContentService::loadVersionInfo - * - * @depends Ibexa\Tests\Core\Repository\Service\Mock\ContentTest::testLoadVersionInfoById - * @depends Ibexa\Tests\Core\Repository\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsNotFoundException - * @depends Ibexa\Tests\Core\Repository\Service\Mock\ContentTest::testLoadVersionInfoByIdThrowsUnauthorizedExceptionNonPublishedVersion - * @depends Ibexa\Tests\Core\Repository\Service\Mock\ContentTest::testLoadVersionInfoByIdPublishedVersion - * @depends Ibexa\Tests\Core\Repository\Service\Mock\ContentTest::testLoadVersionInfoByIdNonPublishedVersion - */ - public function testLoadVersionInfo() + public function testLoadVersionInfo(): void { $expectedResult = $this->createMock(VersionInfo::class); @@ -804,7 +735,7 @@ public function testDeleteContentThrowsUnauthorizedException() ] ); - $persistenceHandlerMock = $this->getPersistenceMockHandler('Handler'); + $this->getPersistenceMockHandler('Handler'); /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); @@ -871,7 +802,7 @@ public function testDeleteContent() ] ); - $persistenceHandlerMock = $this->getPersistenceMockHandler('Handler'); + $this->getPersistenceMockHandler('Handler'); /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); @@ -949,7 +880,7 @@ public function testDeleteContentWithRollback() ] ); - $persistenceHandlerMock = $this->getPersistenceMockHandler('Handler'); + $this->getPersistenceMockHandler('Handler'); /** @var \PHPUnit\Framework\MockObject\MockObject $contentHandler */ $contentHandler = $this->getPersistenceMock()->contentHandler(); @@ -2173,7 +2104,7 @@ private function provideCommonCreateContentObjects(array $fieldDefinitions, arra [ 'id' => 123, 'fieldDefinitions' => new FieldDefinitionCollection($fieldDefinitions), - 'nameSchema' => '', + 'nameSchema' => self::NAME_SCHEMA, ] ); $contentCreateStruct = new ContentCreateStruct( @@ -2191,9 +2122,9 @@ private function provideCommonCreateContentObjects(array $fieldDefinitions, arra } private function commonContentCreateMocks( - \PHPUnit\Framework\MockObject\MockObject $languageHandlerMock, - \PHPUnit\Framework\MockObject\MockObject $contentTypeServiceMock, - \PHPUnit\Framework\MockObject\MockObject $repositoryMock, + MockObject $languageHandlerMock, + MockObject $contentTypeServiceMock, + MockObject $repositoryMock, ContentType $contentType ): void { $this->loadByLanguageCodeMock($languageHandlerMock); @@ -2208,7 +2139,7 @@ private function commonContentCreateMocks( ->will(self::returnValue($contentTypeServiceMock)); } - private function loadByLanguageCodeMock(\PHPUnit\Framework\MockObject\MockObject $languageHandlerMock): void + private function loadByLanguageCodeMock(MockObject $languageHandlerMock): void { $languageHandlerMock->expects(self::any()) ->method('loadByLanguageCode') @@ -2390,7 +2321,7 @@ protected function assertForTestCreateContentThrowsContentFieldValidationExcepti [ 'id' => 123, 'fieldDefinitions' => new FieldDefinitionCollection($fieldDefinitions), - 'nameSchema' => '', + 'nameSchema' => self::NAME_SCHEMA, ] ); $contentCreateStruct = new ContentCreateStruct( @@ -2534,7 +2465,7 @@ public function testCreateContentThrowsContentFieldValidationException($mainLang } } - private function acceptFieldTypeValueMock(\PHPUnit\Framework\MockObject\MockObject $fieldTypeMock): void + private function acceptFieldTypeValueMock(MockObject $fieldTypeMock): void { $fieldTypeMock->expects(self::any()) ->method('acceptValue') @@ -2547,7 +2478,7 @@ static function ($valueString) { ); } - private function toHashFieldTypeMock(\PHPUnit\Framework\MockObject\MockObject $fieldTypeMock): void + private function toHashFieldTypeMock(MockObject $fieldTypeMock): void { $fieldTypeMock ->method('toHash') @@ -2556,14 +2487,14 @@ private function toHashFieldTypeMock(\PHPUnit\Framework\MockObject\MockObject $f }); } - private function getFieldTypeFieldTypeRegistryMock(\PHPUnit\Framework\MockObject\MockObject $fieldTypeMock): void + private function getFieldTypeFieldTypeRegistryMock(MockObject $fieldTypeMock): void { $this->getFieldTypeRegistryMock()->expects(self::any()) ->method('getFieldType') ->will(self::returnValue($fieldTypeMock)); } - private function isEmptyValueFieldTypeMock(\PHPUnit\Framework\MockObject\MockObject $fieldTypeMock): void + private function isEmptyValueFieldTypeMock(MockObject $fieldTypeMock): void { $emptyValue = new ValueStub(self::EMPTY_FIELD_VALUE); $fieldTypeMock->expects(self::any()) @@ -2578,7 +2509,7 @@ static function (ValueStub $value) use ($emptyValue) { } private function getUniqueHashDomainMapperMock( - \PHPUnit\Framework\MockObject\MockObject $domainMapperMock, + MockObject $domainMapperMock, self $that, ContentCreateStruct $contentCreateStruct ): void { @@ -2777,7 +2708,7 @@ public function testCreateContentWithLocationsDuplicateUnderParent() [ 'id' => 123, 'fieldDefinitions' => new FieldDefinitionCollection($fieldDefinitions), - 'nameSchema' => '', + 'nameSchema' => self::NAME_SCHEMA, ] ); $contentCreateStruct = new ContentCreateStruct( @@ -2936,10 +2867,6 @@ public function testCreateContentObjectStates() ] ), ]; - $objectStateGroups = [ - new SPIObjectStateGroup(['id' => 10]), - new SPIObjectStateGroup(['id' => 20]), - ]; // Set up a simple case that will pass $contentCreateStruct = $this->assertForTestCreateContentNonRedundantFieldSet( @@ -3023,7 +2950,7 @@ public function testCreateContentObjectStates() public function testCreateContentWithRollback() { $this->expectException(\Exception::class); - $this->expectExceptionMessage('Store failed'); + $this->expectExceptionMessage(self::STORE_FAILED_EXPECTED_EXCEPTION_MESSAGE); $fieldDefinitions = [ new FieldDefinition( @@ -3059,7 +2986,7 @@ public function testCreateContentWithRollback() $contentHandlerMock->expects(self::once()) ->method('create') ->with(self::anything()) - ->will(self::throwException(new \Exception('Store failed'))); + ->will(self::throwException(new \Exception(self::STORE_FAILED_EXPECTED_EXCEPTION_MESSAGE))); // Execute $this->partlyMockedContentService->createContent($contentCreateStruct, []); @@ -5024,21 +4951,14 @@ static function ($languageCode) { self::returnValue($content) ); - $permissionResolverMock->expects(self::any()) + $permissionResolverMock ->method('canUser') ->with( self::equalTo('content'), self::equalTo('edit'), self::equalTo($content), self::isType('array') - )->will(self::returnValue(true)); - - /* - $contentTypeServiceMock->expects($this->once()) - ->method('loadContentType') - ->with($this->equalTo($contentType->id)) - ->will($this->returnValue($contentType)); - */ + )->willReturn(true); $contentUpdateStruct = new ContentUpdateStruct( [ @@ -5544,7 +5464,7 @@ public function testUpdateContentThrowsContentFieldValidationException( public function testUpdateContentTransactionRollback() { $this->expectException(\Exception::class); - $this->expectExceptionMessage('Store failed'); + $this->expectExceptionMessage(self::STORE_FAILED_EXPECTED_EXCEPTION_MESSAGE); $existingFields = [ new Field( @@ -5593,7 +5513,7 @@ public function testUpdateContentTransactionRollback() self::anything(), self::anything(), self::anything() - )->will(self::throwException(new \Exception('Store failed'))); + )->will(self::throwException(new \Exception(self::STORE_FAILED_EXPECTED_EXCEPTION_MESSAGE))); // Execute $this->partlyMockedContentService->updateContent($versionInfo, $contentUpdateStruct); @@ -6222,12 +6142,7 @@ protected function mockPublishUrlAliasesForContent(APIContent $content) ->with(123, 456, ['eng-GB']); } - protected $relationProcessorMock; - - /** - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\Helper\RelationProcessor - */ - protected function getRelationProcessorMock() + protected function getRelationProcessorMock(): MockObject & RelationProcessor { if (!isset($this->relationProcessorMock)) { $this->relationProcessorMock = $this->createMock(RelationProcessor::class); @@ -6236,12 +6151,6 @@ protected function getRelationProcessorMock() return $this->relationProcessorMock; } - /** - * @var \PHPUnit\Framework\MockObject\MockObject - * &\Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface - */ - protected NameSchemaServiceInterface $nameSchemaServiceMock; - /** * @return \PHPUnit\Framework\MockObject\MockObject * &\Ibexa\Contracts\Core\Repository\NameSchema\NameSchemaServiceInterface @@ -6321,17 +6230,55 @@ protected function getPartlyMockedContentService(array $methods = null) return $this->partlyMockedContentService; } - /** - * @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getRepositoryMock(): Repository + protected function getRepositoryMock(): MockObject & Repository { $repositoryMock = parent::getRepositoryMock(); $repositoryMock - ->expects(self::any()) ->method('getPermissionResolver') ->willReturn($this->getPermissionResolverMock()); return $repositoryMock; } + + private function configureMocksForLoadVersionInfoById( + bool $isPublished, + string $contentFunction, + bool $canUser, + APIVersionInfo & MockObject $versionInfoMock, + LegacyContentHandler & MockObject $contentHandler, + ContentDomainMapper & MockObject $domainMapperMock, + PermissionResolver & MockObject $permissionResolver + ): void { + $versionInfoMock + ->method('isPublished') + ->willReturn($isPublished) + ; + + $contentHandler + ->expects(self::once()) + ->method('loadVersionInfo') + ->with(42, 24) + ->willReturn( + new SPIVersionInfo() + ) + ; + + $domainMapperMock + ->expects(self::once()) + ->method('buildVersionInfoDomainObject') + ->with(new SPIVersionInfo()) + ->willReturn($versionInfoMock) + ; + + $permissionResolver + ->expects(self::once()) + ->method('canUser') + ->with( + 'content', + $contentFunction, + $versionInfoMock + ) + ->willReturn($canUser) + ; + } } diff --git a/tests/lib/Repository/Service/Mock/PermissionTest.php b/tests/lib/Repository/Service/Mock/PermissionTest.php index 4f78cd918d..c0ba4920ab 100644 --- a/tests/lib/Repository/Service/Mock/PermissionTest.php +++ b/tests/lib/Repository/Service/Mock/PermissionTest.php @@ -11,6 +11,7 @@ use Ibexa\Contracts\Core\Persistence\User\Policy; use Ibexa\Contracts\Core\Persistence\User\Role; use Ibexa\Contracts\Core\Persistence\User\RoleAssignment; +use Ibexa\Contracts\Core\Repository\PermissionResolver as APIPermissionResolver; use Ibexa\Contracts\Core\Repository\Repository; use Ibexa\Contracts\Core\Repository\Values\User\Limitation; use Ibexa\Contracts\Core\Repository\Values\ValueObject; @@ -18,9 +19,9 @@ use Ibexa\Core\Base\Exceptions\InvalidArgumentValue; use Ibexa\Core\Base\Exceptions\NotFound\LimitationNotFoundException; use Ibexa\Core\Repository\Permission\PermissionResolver; -use Ibexa\Core\Repository\Repository as CoreRepository; use Ibexa\Core\Repository\Values\User\UserReference; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; /** * Mock test case for PermissionResolver. @@ -107,20 +108,20 @@ public function testHasAccessReturnsTrue(array $roles, array $roleAssignments) { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $mockedService = $this->getPermissionResolverMock(null); + $mockedService = $this->getPermissionResolverWithMockedMethods([]); $userHandlerMock ->expects(self::once()) ->method('loadRoleAssignmentsByGroupId') ->with(self::equalTo(10), self::equalTo(true)) - ->will(self::returnValue($roleAssignments)); + ->willReturn($roleAssignments); foreach ($roleAssignments as $at => $roleAssignment) { $userHandlerMock ->expects(self::at($at + 1)) ->method('loadRole') ->with($roleAssignment->roleId) - ->will(self::returnValue($roles[$roleAssignment->roleId])); + ->willReturn($roles[$roleAssignment->roleId]); } $result = $mockedService->hasAccess('dummy-module', 'dummy-function'); @@ -178,7 +179,7 @@ public function testHasAccessReturnsFalse(array $roles, array $roleAssignments) { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $service = $this->getPermissionResolverMock(null); + $service = $this->getPermissionResolverWithMockedMethods([]); $userHandlerMock ->expects(self::once()) @@ -206,7 +207,7 @@ public function testHasAccessReturnsFalseButSudoSoTrue() { /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); - $service = $this->getPermissionResolverMock(null); + $service = $this->getPermissionResolverWithMockedMethods([]); $repositoryMock = $this->getRepositoryMock(); $repositoryMock ->expects(self::any()) @@ -291,7 +292,7 @@ public function testHasAccessReturnsPermissionSets(array $roles, array $roleAssi /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); $roleDomainMapper = $this->getRoleDomainMapperMock(['buildDomainPolicyObject']); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -407,7 +408,7 @@ public function testHasAccessReturnsLimitationNotFoundException(array $roles, ar /** @var $userHandlerMock \PHPUnit\Framework\MockObject\MockObject */ $userHandlerMock = $this->getPersistenceMock()->userHandler(); $roleDomainMapper = $this->getRoleDomainMapperMock(); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -519,7 +520,7 @@ public function testHasAccessReturnsInvalidArgumentValueException(array $roles, { $this->expectException(InvalidArgumentValue::class); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); /** @var $role \Ibexa\Contracts\Core\Persistence\User\Role */ foreach ($roles as $role) { @@ -587,7 +588,7 @@ public function testHasAccessReturnsPermissionSetsWithRoleLimitation(array $role $limitationTypeMock = $this->createMock(Type::class); $limitationService = $this->getLimitationServiceMock(); $roleDomainMapper = $this->getRoleDomainMapperMock(); - $permissionResolverMock = $this->getPermissionResolverMock(['getCurrentUserReference']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['getCurrentUserReference']); $permissionResolverMock ->expects(self::once()) @@ -690,13 +691,13 @@ public function providerForTestCanUserSimple() */ public function testCanUserSimple($permissionSets, $result) { - $permissionResolverMock = $this->getPermissionResolverMock(['hasAccess']); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods(['hasAccess']); $permissionResolverMock ->expects(self::once()) ->method('hasAccess') ->with(self::equalTo('test-module'), self::equalTo('test-function')) - ->will(self::returnValue($permissionSets)); + ->willReturn($permissionSets); /** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */ $valueObject = $this->getMockForAbstractClass(ValueObject::class); @@ -714,7 +715,7 @@ public function testCanUserSimple($permissionSets, $result) */ public function testCanUserWithoutLimitations() { - $permissionResolverMock = $this->getPermissionResolverMock( + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods( [ 'hasAccess', 'getCurrentUserReference', @@ -891,7 +892,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli /** @var $valueObject \Ibexa\Contracts\Core\Repository\Values\ValueObject */ $valueObject = $this->createMock(ValueObject::class); $limitationServiceMock = $this->getLimitationServiceMock(); - $permissionResolverMock = $this->getPermissionResolverMock( + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods( [ 'hasAccess', 'getCurrentUserReference', @@ -903,7 +904,7 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli ->expects(self::once()) ->method('hasAccess') ->with(self::equalTo('test-module'), self::equalTo('test-function')) - ->will(self::returnValue($permissionSets)); + ->willReturn($permissionSets); $userRef = new UserReference(14); $permissionResolverMock @@ -974,9 +975,9 @@ public function testCanUserComplex(array $roleLimitationEvaluations, array $poli /** * Test for the setCurrentUserReference() and getCurrentUserReference() methods. */ - public function testSetAndGetCurrentUserReference() + public function testSetAndGetCurrentUserReference(): void { - $permissionResolverMock = $this->getPermissionResolverMock(null); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]); $userReferenceMock = $this->getUserReferenceMock(); $userReferenceMock @@ -997,19 +998,19 @@ public function testSetAndGetCurrentUserReference() */ public function testGetCurrentUserReferenceReturnsAnonymousUser() { - $permissionResolverMock = $this->getPermissionResolverMock(null); + $permissionResolverMock = $this->getPermissionResolverWithMockedMethods([]); self::assertEquals(new UserReference(10), $permissionResolverMock->getCurrentUserReference()); } - protected $permissionResolverMock; + protected MockObject & APIPermissionResolver $permissionResolverMock; /** - * @return \Ibexa\Contracts\Core\Repository\PermissionResolver|\PHPUnit\Framework\MockObject\MockObject + * @param string[] $methods */ - protected function getPermissionResolverMock($methods = []) + protected function getPermissionResolverWithMockedMethods(array $methods): MockObject & APIPermissionResolver { - if ($this->permissionResolverMock === null) { + if (!isset($this->permissionResolverMock)) { $configResolverMock = $this->createMock(ConfigResolverInterface::class); $configResolverMock ->method('getParameter') @@ -1018,7 +1019,7 @@ protected function getPermissionResolverMock($methods = []) $this->permissionResolverMock = $this ->getMockBuilder(PermissionResolver::class) - ->setMethods($methods) + ->onlyMethods($methods) ->setConstructorArgs( [ $this->getRoleDomainMapperMock(), @@ -1051,32 +1052,14 @@ protected function getPermissionResolverMock($methods = []) return $this->permissionResolverMock; } - protected $userReferenceMock; + protected MockObject & UserReference $userReferenceMock; - protected function getUserReferenceMock() + protected function getUserReferenceMock(): MockObject & UserReference { - if ($this->userReferenceMock === null) { + if (!isset($this->userReferenceMock)) { $this->userReferenceMock = $this->createMock(UserReference::class); } return $this->userReferenceMock; } - - protected $repositoryMock; - - /** - * @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getRepositoryMock(): Repository - { - if ($this->repositoryMock === null) { - $this->repositoryMock = $this - ->getMockBuilder(CoreRepository::class) - ->onlyMethods(['getPermissionResolver']) - ->disableOriginalConstructor() - ->getMock(); - } - - return $this->repositoryMock; - } } diff --git a/tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php b/tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php index 529e036ae3..28419aa6b5 100644 --- a/tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php +++ b/tests/lib/Repository/Service/Mock/PermissionsCriterionHandlerTest.php @@ -8,13 +8,13 @@ namespace Ibexa\Tests\Core\Repository\Service\Mock; use Ibexa\Contracts\Core\Limitation\Type; -use Ibexa\Contracts\Core\Repository\PermissionResolver; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\User\Limitation as APILimitation; use Ibexa\Core\Repository\PermissionsCriterionHandler; use Ibexa\Core\Repository\Values\User\Policy; use Ibexa\Core\Repository\Values\User\User; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; /** * Mock test case for PermissionCriterionHandler. @@ -280,12 +280,7 @@ protected function mockServices($criterionMock, $limitationCount, $permissionSet $userMock = $this->createMock(User::class); $limitationTypeMock = $this->createMock(Type::class); $limitationServiceMock = $this->getLimitationServiceMock(); - $permissionResolverMock = $this->getPermissionResolverMock( - [ - 'hasAccess', - 'getCurrentUserReference', - ] - ); + $permissionResolverMock = $this->getPermissionResolverMock(); $limitationTypeMock ->expects(self::any()) @@ -326,7 +321,7 @@ public function testGetPermissionsCriterion( $expectedCriterion ) { $this->mockServices($criterionMock, $limitationCount, $permissionSets); - $handler = $this->getPermissionsCriterionHandlerMock(null); + $handler = $this->getPermissionsCriterionHandlerMock(); $permissionsCriterion = $handler->getPermissionsCriterion(); @@ -348,13 +343,13 @@ public function providerForTestGetPermissionsCriterionBooleanPermissionSets() */ public function testGetPermissionsCriterionBooleanPermissionSets($permissionSets) { - $permissionResolverMock = $this->getPermissionResolverMock(['hasAccess']); + $permissionResolverMock = $this->getPermissionResolverMock(); $permissionResolverMock ->expects(self::once()) ->method('hasAccess') ->with(self::equalTo('testModule'), self::equalTo('testFunction')) - ->will(self::returnValue($permissionSets)); - $handler = $this->getPermissionsCriterionHandlerMock(null); + ->willReturn($permissionSets); + $handler = $this->getPermissionsCriterionHandlerMock(); $permissionsCriterion = $handler->getPermissionsCriterion('testModule', 'testFunction'); @@ -364,15 +359,13 @@ public function testGetPermissionsCriterionBooleanPermissionSets($permissionSets /** * Returns the PermissionsCriterionHandler to test with $methods mocked. * - * @param string[]|null $methods - * - * @return \PHPUnit\Framework\MockObject\MockObject|\Ibexa\Core\Repository\PermissionsCriterionHandler + * @param string[] $methods */ - protected function getPermissionsCriterionHandlerMock($methods = []) + protected function getPermissionsCriterionHandlerMock(array $methods = []): MockObject & PermissionsCriterionHandler { return $this ->getMockBuilder(PermissionsCriterionHandler::class) - ->setMethods($methods) + ->onlyMethods($methods) ->setConstructorArgs( [ $this->getPermissionResolverMock(), @@ -381,19 +374,4 @@ protected function getPermissionsCriterionHandlerMock($methods = []) ) ->getMock(); } - - protected $permissionResolverMock; - - protected function getPermissionResolverMock($methods = []) - { - if ($this->permissionResolverMock === null) { - $this->permissionResolverMock = $this - ->getMockBuilder(PermissionResolver::class) - ->setMethods($methods) - ->disableOriginalConstructor() - ->getMockForAbstractClass(); - } - - return $this->permissionResolverMock; - } } diff --git a/tests/lib/Repository/Service/Mock/RelationProcessorTest.php b/tests/lib/Repository/Service/Mock/RelationProcessorTest.php index c5cfa2529b..bfc31fbb9b 100644 --- a/tests/lib/Repository/Service/Mock/RelationProcessorTest.php +++ b/tests/lib/Repository/Service/Mock/RelationProcessorTest.php @@ -20,6 +20,7 @@ use Ibexa\Core\Repository\Values\Content\Relation as RelationValue; use Ibexa\Core\Repository\Values\ContentType\FieldDefinition; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; /** @@ -680,10 +681,7 @@ protected function getPartlyMockedRelationProcessor(array $methods = null) ->getMock(); } - /** - * @return \PHPUnit\Framework\MockObject\MockObject - */ - protected function getFieldTypeServiceMock() + protected function getFieldTypeServiceMock(): MockObject & FieldTypeService { return $this->createMock(FieldTypeService::class); } diff --git a/tests/lib/Repository/Service/Mock/RoleTest.php b/tests/lib/Repository/Service/Mock/RoleTest.php index 325bdceec5..4bf4b8192f 100644 --- a/tests/lib/Repository/Service/Mock/RoleTest.php +++ b/tests/lib/Repository/Service/Mock/RoleTest.php @@ -14,7 +14,7 @@ use Ibexa\Contracts\Core\Repository\Exceptions\BadStateException; use Ibexa\Contracts\Core\Repository\Exceptions\LimitationValidationException; use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException; -use Ibexa\Contracts\Core\Repository\Repository; +use Ibexa\Contracts\Core\Repository\Repository as APIRepository; use Ibexa\Contracts\Core\Repository\UserService; use Ibexa\Contracts\Core\Repository\Values\User\Limitation\RoleLimitation; use Ibexa\Contracts\Core\Repository\Values\User\PolicyCreateStruct; @@ -30,6 +30,7 @@ use Ibexa\Core\Repository\Permission\LimitationService; use Ibexa\Core\Repository\RoleService; use Ibexa\Tests\Core\Repository\Service\Mock\Base as BaseServiceMockTest; +use PHPUnit\Framework\MockObject\MockObject; /** * @covers \Ibexa\Contracts\Core\Repository\RoleService @@ -1083,14 +1084,10 @@ protected function getPartlyMockedRoleService( return $this->partlyMockedRoleService; } - /** - * @return \Ibexa\Contracts\Core\Repository\Repository|\PHPUnit\Framework\MockObject\MockObject - */ - protected function getRepositoryMock(): Repository + protected function getRepositoryMock(): MockObject & APIRepository { $repositoryMock = parent::getRepositoryMock(); $repositoryMock - ->expects(self::any()) ->method('getPermissionResolver') ->willReturn($this->getPermissionResolverMock()); diff --git a/tests/lib/Search/Legacy/Content/AbstractTestCase.php b/tests/lib/Search/Legacy/Content/AbstractTestCase.php index a53b70569a..570a62bc5c 100644 --- a/tests/lib/Search/Legacy/Content/AbstractTestCase.php +++ b/tests/lib/Search/Legacy/Content/AbstractTestCase.php @@ -7,10 +7,14 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; +use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; +use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Persistence\Content\Type\Handler as SPIContentTypeHandler; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\Converter; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry; use Ibexa\Core\Persistence\Legacy\Content\Gateway; +use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; +use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper\ResolveVirtualFieldSubscriber; use Ibexa\Core\Persistence\Legacy\Content\StorageRegistry; use Ibexa\Core\Persistence\Legacy\Content\Type\Gateway\DoctrineDatabase as ContentTypeGateway; @@ -19,6 +23,7 @@ use Ibexa\Core\Persistence\Legacy\Content\Type\StorageDispatcherInterface; use Ibexa\Core\Persistence\Legacy\Content\Type\Update\Handler as ContentTypeUpdateHandler; use Ibexa\Tests\Core\Persistence\Legacy\Content\LanguageAwareTestCase; +use PHPUnit\Framework\MockObject\MockObject; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -139,4 +144,54 @@ protected function getEventDispatcher(): EventDispatcherInterface return $eventDispatcher; } + + protected function getContentMapperMock(): ContentMapper & MockObject + { + $mapperMock = $this->createMock(ContentMapper::class); + $mapperMock + ->method('extractContentInfoFromRows') + ->with(self::isType('array')) + ->willReturnCallback( + static function (array $rows): array { + $contentInfoObjs = []; + foreach ($rows as $row) { + $contentId = (int)$row['id']; + if (!isset($contentInfoObjs[$contentId])) { + $contentInfoObjs[$contentId] = new ContentInfo(); + $contentInfoObjs[$contentId]->id = $contentId; + } + } + + return array_values($contentInfoObjs); + } + ) + ; + + return $mapperMock; + } + + protected function getLocationMapperMock(): LocationMapper & MockObject + { + $mapperMock = $this->createMock(LocationMapper::class); + + $mapperMock + ->method('createLocationsFromRows') + ->with(self::isType('array')) + ->willReturnCallback( + static function ($rows): array { + $locations = []; + foreach ($rows as $row) { + $locationId = (int)$row['node_id']; + if (!isset($locations[$locationId])) { + $locations[$locationId] = new SPILocation(); + $locations[$locationId]->id = $locationId; + } + } + + return array_values($locations); + } + ); + + return $mapperMock; + } } diff --git a/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php b/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php index be4d4cce44..35fda09dca 100644 --- a/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerContentSortTest.php @@ -7,14 +7,12 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; use Ibexa\Contracts\Core\Repository\Values\Content\Query; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; use Ibexa\Core\Persistence\Legacy\Content\FieldHandler; use Ibexa\Core\Persistence\Legacy\Content\FieldValue\ConverterRegistry; use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; -use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Location\Gateway as LocationGateway; @@ -90,47 +88,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a content mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Mapper - */ - protected function getContentMapperMock() - { - $mapperMock = $this->getMockBuilder(ContentMapper::class) - ->setConstructorArgs( - [ - $this->getFieldRegistry(), - $this->getLanguageHandler(), - $this->getContentTypeHandler(), - $this->getEventDispatcher(), - ] - ) - ->setMethods(['extractContentInfoFromRows']) - ->getMock(); - $mapperMock->expects(self::any()) - ->method('extractContentInfoFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows) { - $contentInfoObjs = []; - foreach ($rows as $row) { - $contentId = (int)$row['id']; - if (!isset($contentInfoObjs[$contentId])) { - $contentInfoObjs[$contentId] = new ContentInfo(); - $contentInfoObjs[$contentId]->id = $contentId; - } - } - - return array_values($contentInfoObjs); - } - ) - ); - - return $mapperMock; - } - /** * Returns a field registry mock object. * diff --git a/tests/lib/Search/Legacy/Content/HandlerContentTest.php b/tests/lib/Search/Legacy/Content/HandlerContentTest.php index ac184d25ae..fed2b42f98 100644 --- a/tests/lib/Search/Legacy/Content/HandlerContentTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerContentTest.php @@ -7,7 +7,6 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\ContentInfo; use Ibexa\Contracts\Core\Persistence\Content\Type; use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException; @@ -17,7 +16,6 @@ use Ibexa\Core\Persistence; use Ibexa\Core\Persistence\Legacy\Content\FieldHandler; use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; -use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Location\Gateway as LocationGateway; @@ -189,47 +187,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a content mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Mapper - */ - protected function getContentMapperMock() - { - $mapperMock = $this->getMockBuilder(ContentMapper::class) - ->setConstructorArgs( - [ - $this->getConverterRegistry(), - $this->getLanguageHandler(), - $this->getContentTypeHandler(), - $this->getEventDispatcher(), - ] - ) - ->setMethods(['extractContentInfoFromRows']) - ->getMock(); - $mapperMock->expects(self::any()) - ->method('extractContentInfoFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows) { - $contentInfoObjs = []; - foreach ($rows as $row) { - $contentId = (int)$row['id']; - if (!isset($contentInfoObjs[$contentId])) { - $contentInfoObjs[$contentId] = new ContentInfo(); - $contentInfoObjs[$contentId]->id = $contentId; - } - } - - return array_values($contentInfoObjs); - } - ) - ); - - return $mapperMock; - } - /** * Returns a content field handler mock. * diff --git a/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php b/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php index d5264353a4..55a521986c 100644 --- a/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerLocationSortTest.php @@ -7,11 +7,9 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; -use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter; @@ -105,40 +103,6 @@ protected function getContentSearchHandler() ); } - /** - * Returns a location mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Location\Mapper - */ - protected function getLocationMapperMock() - { - $mapperMock = $this->getMockBuilder(LocationMapper::class) - ->setMethods(['createLocationsFromRows']) - ->getMock(); - $mapperMock - ->expects(self::any()) - ->method('createLocationsFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows) { - $locations = []; - foreach ($rows as $row) { - $locationId = (int)$row['node_id']; - if (!isset($locations[$locationId])) { - $locations[$locationId] = new SPILocation(); - $locations[$locationId]->id = $locationId; - } - } - - return array_values($locations); - } - ) - ); - - return $mapperMock; - } - public function testNoSorting() { $handler = $this->getContentSearchHandler(); diff --git a/tests/lib/Search/Legacy/Content/HandlerLocationTest.php b/tests/lib/Search/Legacy/Content/HandlerLocationTest.php index 8761d99507..e592ba3ce8 100644 --- a/tests/lib/Search/Legacy/Content/HandlerLocationTest.php +++ b/tests/lib/Search/Legacy/Content/HandlerLocationTest.php @@ -7,13 +7,11 @@ namespace Ibexa\Tests\Core\Search\Legacy\Content; -use Ibexa\Contracts\Core\Persistence\Content\Location as SPILocation; use Ibexa\Contracts\Core\Repository\Exceptions\InvalidArgumentException; use Ibexa\Contracts\Core\Repository\Values\Content\LocationQuery; use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; use Ibexa\Contracts\Core\Repository\Values\Content\Query\SortClause; use Ibexa\Core\Persistence; -use Ibexa\Core\Persistence\Legacy\Content\Location\Mapper as LocationMapper; use Ibexa\Core\Persistence\Legacy\Content\Mapper as ContentMapper; use Ibexa\Core\Search\Legacy\Content; use Ibexa\Core\Search\Legacy\Content\Common\Gateway\CriteriaConverter; @@ -166,40 +164,6 @@ protected function getContentSearchHandler(array $fullTextSearchConfiguration = ); } - /** - * Returns a location mapper mock. - * - * @return \Ibexa\Core\Persistence\Legacy\Content\Location\Mapper - */ - protected function getLocationMapperMock() - { - $mapperMock = $this->getMockBuilder(LocationMapper::class) - ->setMethods(['createLocationsFromRows']) - ->getMock(); - $mapperMock - ->expects(self::any()) - ->method('createLocationsFromRows') - ->with(self::isType('array')) - ->will( - self::returnCallback( - static function ($rows) { - $locations = []; - foreach ($rows as $row) { - $locationId = (int)$row['node_id']; - if (!isset($locations[$locationId])) { - $locations[$locationId] = new SPILocation(); - $locations[$locationId]->id = $locationId; - } - } - - return array_values($locations); - } - ) - ); - - return $mapperMock; - } - public function testFindWithoutOffsetLimit() { $handler = $this->getContentSearchHandler();