reflection = $this->getMockBuilder(Reflection::class)->disableOriginalConstructor()->getMock(); $this->parameterBag = $this->getMockBuilder(ParameterBagInterface::class)->disableOriginalConstructor()->getMock(); } public function getMockForMethod(string $methodName): MockObject|TestableModule { return $this->getMockBuilder(TestableModule::class) ->setConstructorArgs([$this->reflection, $this->parameterBag]) ->setMethodsExcept([$methodName]) ->getMock(); } /** * @see Module::getOrganizationModules() */ public function testGetOrganizationModules(): void { $module = $this->getMockForMethod('getOrganizationModules'); $module->expects(self::once())->method('getModuleBySettings')->willReturn(['Sms']); $module->expects(self::once())->method('getModulesByConditions')->willReturn(['CotisationCall']); $settings = $this->getMockBuilder(Settings::class)->disableOriginalConstructor()->getMock(); $settings->expects(self::once())->method('getProduct')->willReturn(SettingsProductEnum::SCHOOL); $organization = $this->getMockBuilder(Organization::class)->disableOriginalConstructor()->getMock(); $organization->expects(self::once())->method('getSettings')->willReturn($settings); $module->expects(self::once())->method('getModulesByProductConfiguration')->willReturn(['Notification']); $this->assertEqualsCanonicalizing( ['Sms', 'CotisationCall', 'Notification'], $module->getOrganizationModules($organization) ); } /** * @see Module::getModuleBySettings() */ public function testGetModuleBySettings(): void { $module = $this->getMockForMethod('getModuleBySettings'); $settings = $this->getMockBuilder(Settings::class)->getMock(); $settings ->expects($this->once()) ->method('getModules') ->willReturn(['Sms' => true]); $organization = $this->getMockBuilder(Organization::class)->getMock(); $organization ->expects($this->once()) ->method('getSettings') ->willReturn($settings); $this->assertContains('Sms', $module->getModuleBySettings($organization)); } /** * @see Module::getModulesByConditions() */ public function testGetModulesByConditions(): void { $module = $this->getMockForMethod('getModulesByConditions'); $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn( ['CotisationCall' => [ 'roles' => ['ROLE_COTISATION'], 'conditions' => [ 'service' => [ 'name' => CotisationUtils::class, 'function' => 'isLastParentAndCMF', ], ], ]] ); $organization = $this->getMockBuilder(Organization::class)->getMock(); $this->reflection ->method('dynamicInvokeServiceWithArgsAndMethod') ->with(CotisationUtils::class, 'isLastParentAndCMF', [$organization]) ->willReturn(true); $this->assertContains('CotisationCall', $module->getModulesByConditions($organization)); } /** * @see Module::getModulesByConditions() */ public function testGetModulesByConditionsLogicError(): void { $module = $this->getMockForMethod('getModulesByConditions'); $this->parameterBag->method('get')->with('opentalent.modulesbyconditions')->willReturn( [ 'CotisationCall' => [ 'roles' => ['ROLE_COTISATION'], 'conditions' => [ 'service' => [ 'name' => 'App\Service\Organization\Utils', 'function' => 'isLastParentAndCMF', ], ], ], ] ); $organization = $this->getMockBuilder(Organization::class)->getMock(); $this->reflection ->method('dynamicInvokeServiceWithArgsAndMethod') ->with(CotisationUtils::class, 'isLastParentAndCMF', [$organization]) ->willThrowException(new \LogicException()); $this->expectException(\LogicException::class); $module->getModulesByConditions($organization); } /** * @see Module::getModulesByProductConfiguration() */ public function testGetModulesByProductConfiguration(): void { $module = $this->getMockForMethod('getModulesByProductConfiguration'); $this->parameterBag->method('get')->with('opentalent.products')->willReturn( ['artist' => ['modules' => ['foo']]] ); $this->assertEquals(['foo'], $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST)); } /** * @see Module::getModulesByProductConfiguration() */ public function testGetModulesByProductConfigurationExtend(): void { $module = $this->getMockForMethod('getModulesByProductConfiguration'); $this->parameterBag->method('get')->with('opentalent.products')->willReturn( [ 'artist' => ['modules' => ['foo']], 'artist-premium' => ['extend' => 'artist', 'modules' => ['bar']], ] ); $this->assertEqualsCanonicalizing( ['foo', 'bar'], $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM) ); } /** * @see Module::getModulesByProductConfiguration() */ public function testGetModulesByProductConfigurationAccessDenied(): void { $module = $this->getMockBuilder(TestableModule::class); $module = $this->getMockForMethod('getModulesByProductConfiguration'); $this->parameterBag->method('get')->with('opentalent.products')->willReturn( ['artist' => ['modules' => ['foo']]] ); $this->expectException(AccessDeniedHttpException::class); $this->expectExceptionMessage('The product artist-premium does not exist !'); $module->getModulesByProductConfiguration(SettingsProductEnum::ARTIST_PREMIUM); } /** * @see Module::getModulesByResourceName() */ public function testGetModuleByResourceName(): void { $module = $this->getMockForMethod('getModulesByResourceName'); $this->parameterBag->method('get')->with('opentalent.modules')->willReturn( ['Core' => ['resources' => ['foo', 'bar']]] ); $this->assertEquals(['Core'], $module->getModulesByResourceName('foo')); } /** * @see Module::getModulesByResourceName() */ public function testGetModulesByResourceNameNotFound(): void { $module = $this->getMockForMethod('getModulesByResourceName'); $this->parameterBag->method('get')->with('opentalent.modules')->willReturn( ['Core' => ['resources' => ['bar']]] ); $this->assertEmpty($module->getModulesByResourceName('foo')); } }