. */ namespace SP\Tests\Services\Plugin; use Closure; use Defuse\Crypto\Exception\CryptoException; use DI\DependencyException; use DI\NotFoundException; use SP\Core\Context\ContextException; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\NoSuchPropertyException; use SP\Core\Exceptions\QueryException; use SP\Plugin\PluginOperation; use SP\Repositories\NoSuchItemException; use SP\Services\Plugin\PluginDataService; use SP\Services\ServiceException; use SP\Tests\DatabaseTestCase; use stdClass; use function SP\Tests\setupContext; /** * Class PluginOperationTest * * @package SP\Tests\Services\Plugin */ class PluginOperationTest extends DatabaseTestCase { /** * @var Closure */ private static $pluginOperation; /** * @throws NotFoundException * @throws ContextException * @throws DependencyException */ public static function setUpBeforeClass(): void { $dic = setupContext(); self::$loadFixtures = true; // Inicializar el servicio self::$pluginOperation = function ($name) use ($dic) { return new PluginOperation($dic->get(PluginDataService::class), $name); }; } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testUpdate() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $data = [1, 2, 3]; $this->assertEquals(1, $pluginOperation->update(1, $data)); $this->assertEquals($data, $pluginOperation->get(1)); $data = new stdClass(); $data->id = 1; $data->name = 'test'; $data->test = new stdClass(); $this->assertEquals(1, $pluginOperation->update(1, $data)); $this->assertEquals($data, $pluginOperation->get(1)); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testUpdateUnknown() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $data = [1, 2, 3]; $this->assertEquals(0, $pluginOperation->update(4, $data)); $this->assertNull($pluginOperation->get(4)); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testUpdateWrongPlugin() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Test'); $data = [1, 2, 3]; $this->assertEquals(0, $pluginOperation->update(1, $data)); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function testDelete() { $this->assertEquals(4, self::getRowCount('PluginData')); /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $pluginOperation->delete(1); $this->assertEquals(3, self::getRowCount('PluginData')); } /** * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function testDeleteUnknown() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $this->expectException(NoSuchItemException::class); $pluginOperation->delete(4); } public function testGet() { $this->markTestSkipped('Already tested'); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testGetUnknown() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $this->assertNull($pluginOperation->get(4)); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testCreate() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $data = new stdClass(); $data->id = 1; $data->name = 'test'; $data->test = new stdClass(); $pluginOperation->create(4, $data); $this->assertEquals($data, $pluginOperation->get(4)); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testCreateDuplicated() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $data = new stdClass(); $data->id = 1; $data->name = 'test'; $data->test = new stdClass(); $this->expectException(ConstraintException::class); $this->assertEquals(1, $pluginOperation->create(2, $data)); } /** * @throws CryptoException * @throws ConstraintException * @throws NoSuchPropertyException * @throws QueryException * @throws ServiceException */ public function testCreateWrongPlugin() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Test'); $data = new stdClass(); $data->id = 1; $data->name = 'test'; $data->test = new stdClass(); $this->expectException(ConstraintException::class); $this->assertEquals(1, $pluginOperation->create(2, $data)); } }