diff --git a/application/Espo/Core/Record/HookManager.php b/application/Espo/Core/Record/HookManager.php index c1bcee107d..9cb59a4160 100644 --- a/application/Espo/Core/Record/HookManager.php +++ b/application/Espo/Core/Record/HookManager.php @@ -31,11 +31,6 @@ namespace Espo\Core\Record; use Espo\ORM\Entity; -use Espo\Core\Record\CreateParams; -use Espo\Core\Record\ReadParams; -use Espo\Core\Record\UpdateParams; -use Espo\Core\Record\DeleteParams; - use Espo\Core\Record\Hook\{ Provider, Type, diff --git a/tests/unit/Espo/Core/Record/HookManagerTest.php b/tests/unit/Espo/Core/Record/HookManagerTest.php new file mode 100644 index 0000000000..11eff077e4 --- /dev/null +++ b/tests/unit/Espo/Core/Record/HookManagerTest.php @@ -0,0 +1,225 @@ +injectableFactory = $this->createMock(InjectableFactory::class); + $this->metadata = $this->createMock(Metadata::class); + + $this->provider = new Provider($this->metadata, $this->injectableFactory); + + $this->manager = new HookManager($this->provider); + + $this->entity = $this->createEntity($this->entityType); + } + + public function testBeforeRead(): void + { + $hook = $this->createMock(BeforeReadHook::class); + + $this->initHooks(Type::BEFORE_READ, [BeforeReadHook::class], [$hook]); + + $params = ReadParams::create(); + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $params); + + $this->manager->processBeforeRead($this->entity, $params); + } + + public function testBeforeCreate(): void + { + $hook = $this->createMock(BeforeCreateHook::class); + + $this->initHooks(Type::BEFORE_CREATE, [BeforeCreateHook::class], [$hook]); + + $params = CreateParams::create(); + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $params); + + $this->manager->processBeforeCreate($this->entity, $params); + } + + public function testBeforeUpdate(): void + { + $hook = $this->createMock(BeforeUpdateHook::class); + + $this->initHooks(Type::BEFORE_UPDATE, [BeforeUpdateHook::class], [$hook]); + + $params = UpdateParams::create(); + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $params); + + $this->manager->processBeforeUpdate($this->entity, $params); + } + + public function testBeforeDelete(): void + { + $hook = $this->createMock(BeforeDeleteHook::class); + + $this->initHooks(Type::BEFORE_DELETE, [BeforeDeleteHook::class], [$hook]); + + $params = DeleteParams::create(); + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $params); + + $this->manager->processBeforeDelete($this->entity, $params); + } + + public function testBeforeLink(): void + { + $hook = $this->createMock(BeforeLinkHook::class); + + $this->initHooks(Type::BEFORE_LINK, [BeforeLinkHook::class], [$hook]); + + $link = 'test'; + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $link, $this->entity); + + $this->manager->processBeforeLink($this->entity, $link, $this->entity); + } + + public function testBeforeUnlink(): void + { + $hook = $this->createMock(BeforeUnlinkHook::class); + + $this->initHooks(Type::BEFORE_UNLINK, [BeforeUnlinkHook::class], [$hook]); + + $link = 'test'; + + $hook + ->expects($this->once()) + ->method('process') + ->with($this->entity, $link, $this->entity); + + $this->manager->processBeforeUnlink($this->entity, $link, $this->entity); + } + + private function createEntity(string $entityType): Entity + { + $entity = $this->createMock(Entity::class); + + $entity + ->expects($this->any()) + ->method('getEntityType') + ->willReturn($entityType); + + return $entity; + } + + private function initHooks(string $type, array $hookClassNameList, array $hookList): void + { + $this->metadata + ->expects($this->any()) + ->method('get') + ->with(['recordDefs', $this->entityType, $type . 'HookClassNameList']) + ->willReturn($hookClassNameList); + + foreach ($hookClassNameList as $i => $className) { + $this->injectableFactory + ->expects($this->any()) + ->method('create') + ->with($className) + ->willReturn($hookList[$i]); + } + } +} diff --git a/tests/unit/testClasses/Core/Record/Hooks/BeforeCreateHook.php b/tests/unit/testClasses/Core/Record/Hooks/BeforeCreateHook.php new file mode 100644 index 0000000000..0f0e56b9d1 --- /dev/null +++ b/tests/unit/testClasses/Core/Record/Hooks/BeforeCreateHook.php @@ -0,0 +1,41 @@ +