mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
hook tests
This commit is contained in:
@@ -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,
|
||||
|
||||
225
tests/unit/Espo/Core/Record/HookManagerTest.php
Normal file
225
tests/unit/Espo/Core/Record/HookManagerTest.php
Normal file
@@ -0,0 +1,225 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\Espo\Core\Record;
|
||||
|
||||
use Espo\Core\{
|
||||
Record\HookManager,
|
||||
Record\Hook\Provider,
|
||||
Record\Hook\ReadHook,
|
||||
Record\Hook\CreateHook,
|
||||
Record\Hook\UpdateHook,
|
||||
Record\Hook\DeleteHook,
|
||||
Record\Hook\LinkHook,
|
||||
Record\Hook\Type,
|
||||
Record\CreateParams,
|
||||
Record\ReadParams,
|
||||
Record\UpdateParams,
|
||||
Record\DeleteParams,
|
||||
InjectableFactory,
|
||||
Utils\Metadata,
|
||||
};
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use tests\unit\testClasses\Core\Record\Hooks\{
|
||||
BeforeReadHook,
|
||||
BeforeCreateHook,
|
||||
BeforeUpdateHook,
|
||||
BeforeDeleteHook,
|
||||
BeforeLinkHook,
|
||||
BeforeUnlinkHook,
|
||||
};
|
||||
|
||||
class HookManagerTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var InjectableFactory
|
||||
*/
|
||||
private $injectableFactory;
|
||||
|
||||
/**
|
||||
* @var Metadata
|
||||
*/
|
||||
private $metadata;
|
||||
|
||||
/**
|
||||
* @var Provider
|
||||
*/
|
||||
private $provider;
|
||||
|
||||
/**
|
||||
* @var HookManager
|
||||
*/
|
||||
private $manager;
|
||||
|
||||
/**
|
||||
* @var Entity
|
||||
*/
|
||||
private $entity;
|
||||
|
||||
private $entityType = 'Test';
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\CreateHook;
|
||||
use Espo\Core\Record\CreateParams;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeCreateHook implements CreateHook
|
||||
{
|
||||
public function process(Entity $entity, CreateParams $params): void
|
||||
{}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\DeleteHook;
|
||||
use Espo\Core\Record\DeleteParams;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeDeleteHook implements DeleteHook
|
||||
{
|
||||
public function process(Entity $entity, DeleteParams $params): void
|
||||
{}
|
||||
}
|
||||
40
tests/unit/testClasses/Core/Record/Hooks/BeforeLinkHook.php
Normal file
40
tests/unit/testClasses/Core/Record/Hooks/BeforeLinkHook.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\LinkHook;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeLinkHook implements LinkHook
|
||||
{
|
||||
public function process(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{}
|
||||
}
|
||||
41
tests/unit/testClasses/Core/Record/Hooks/BeforeReadHook.php
Normal file
41
tests/unit/testClasses/Core/Record/Hooks/BeforeReadHook.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\ReadHook;
|
||||
use Espo\Core\Record\ReadParams;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeReadHook implements ReadHook
|
||||
{
|
||||
public function process(Entity $entity, ReadParams $params): void
|
||||
{}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\UnlinkHook;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeUnlinkHook implements UnlinkHook
|
||||
{
|
||||
public function process(Entity $entity, string $link, Entity $foreignEntity): void
|
||||
{}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\unit\testClasses\Core\Record\Hooks;
|
||||
|
||||
use Espo\Core\Record\Hook\UpdateHook;
|
||||
use Espo\Core\Record\UpdateParams;
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class BeforeUpdateHook implements UpdateHook
|
||||
{
|
||||
public function process(Entity $entity, UpdateParams $params): void
|
||||
{}
|
||||
}
|
||||
Reference in New Issue
Block a user