. */ namespace SP\Tests\Services\Plugin; use SP\Core\Exceptions\ConstraintException; use SP\Plugin\PluginOperation; use SP\Repositories\NoSuchItemException; use SP\Services\Plugin\PluginDataService; use SP\Storage\Database\DatabaseConnectionData; use SP\Tests\DatabaseTestCase; use function SP\Tests\setupContext; /** * Class PluginOperationTest * * @package SP\Tests\Services\Plugin */ class PluginOperationTest extends DatabaseTestCase { /** * @var \Closure */ private static $pluginOperation; /** * @throws \DI\NotFoundException * @throws \SP\Core\Context\ContextException * @throws \DI\DependencyException */ public static function setUpBeforeClass() { $dic = setupContext(); self::$dataset = 'syspass_plugin.xml'; // Datos de conexión a la BBDD self::$databaseConnectionData = $dic->get(DatabaseConnectionData::class); // Inicializar el servicio self::$pluginOperation = function ($name) use ($dic) { return new PluginOperation($dic->get(PluginDataService::class), $name); }; } /** * @throws \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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 \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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 \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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 \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Repositories\NoSuchItemException */ public function testDelete() { $this->assertTableRowCount('PluginData', 4); /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $pluginOperation->delete(1); $this->assertTableRowCount('PluginData', 3); } /** * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Repositories\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 \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\ServiceException */ public function testGetUnknown() { /** @var PluginOperation $pluginOperation */ $pluginOperation = self::$pluginOperation->call($this, 'Authenticator'); $this->assertNull($pluginOperation->get(4)); } /** * @throws \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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 \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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 \Defuse\Crypto\Exception\CryptoException * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\NoSuchPropertyException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Services\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)); } }