* @author Jean-Sébastien Conan */ class PluginModuleTest extends TestCase { /** * Data provider * @return array the data */ public function accessorsProvider() { return [ [ [ 'id' => 'foo', 'name' => 'Foo', 'module' => 'plugin/foo', 'category' => 'dummy', 'description' => 'The best foo ever', 'active' => true, 'tags' => ['required'] ], [ 'id' => 'foo', 'name' => 'Foo', 'module' => 'plugin/foo', 'category' => 'dummy', 'description' => 'The best foo ever', 'active' => true, 'tags' => ['required'] ] ], [ [ 'id' => '12', 'name' => 21, 'module' => 'plugin/foo', 'category' => 'dummy', ], [ 'id' => '12', 'name' => '21', 'module' => 'plugin/foo', 'category' => 'dummy', 'description' => '', 'active' => true, 'tags' => [] ] ] ]; } public function testConstructBadId() { $this->expectException(common_exception_InconsistentData::class); new PluginModule(12, 'foo', 'bar'); } public function testConstructEmptyId() { $this->expectException(common_exception_InconsistentData::class); new PluginModule('', 'foo', 'bar'); } public function testConstructBadModule() { $this->expectException(common_exception_InconsistentData::class); new PluginModule('foo', true, 'bar'); } public function testConstructiEmptyModule() { $this->expectException(common_exception_InconsistentData::class); new PluginModule('foo', '', 'bar'); } public function testConstructBadCategory() { $this->expectException(common_exception_InconsistentData::class); new PluginModule('foo', 'bar', []); } public function testConstructNoCategory() { $this->expectException(common_exception_InconsistentData::class); new PluginModule('foo', 'bar', null); } public function testFromArrayNoRequiredData() { $this->expectException(common_exception_InconsistentData::class); PluginModule::fromArray([]); } /** * Test constructor and getter * @dataProvider accessorsProvider */ public function testConstruct($input, $output) { $PluginModule = new PluginModule($input['id'], $input['module'], $input['category'], $input); $this->assertEquals($output['id'], $PluginModule->getId()); $this->assertEquals($output['name'], $PluginModule->getName()); $this->assertEquals($output['module'], $PluginModule->getModule()); $this->assertEquals($output['category'], $PluginModule->getCategory()); $this->assertEquals($output['description'], $PluginModule->getDescription()); $this->assertEquals($output['active'], $PluginModule->isActive()); $this->assertEquals($output['tags'], $PluginModule->getTags()); $PluginModule->setActive(!$output['active']); $this->assertEquals(!$output['active'], $PluginModule->isActive()); } /** * Test from array and getters * @dataProvider accessorsProvider */ public function testFromArray($input, $output) { $PluginModule = PluginModule::fromArray($input); $this->assertEquals($output['id'], $PluginModule->getId()); $this->assertEquals($output['name'], $PluginModule->getName()); $this->assertEquals($output['module'], $PluginModule->getModule()); $this->assertEquals($output['category'], $PluginModule->getCategory()); $this->assertEquals($output['description'], $PluginModule->getDescription()); $this->assertEquals($output['active'], $PluginModule->isActive()); $this->assertEquals($output['tags'], $PluginModule->getTags()); $PluginModule->setActive(!$output['active']); $this->assertEquals(!$output['active'], $PluginModule->isActive()); } /** * Test encoding the object to json */ public function testJsonSerialize() { $expected = '{"id":"bar","module":"bar\/bar","bundle":"plugins\/bundle.min","position":12,"name":"Bar","description":"The best bar ever","category":"dummy","active":false,"tags":["dummy","goofy"]}'; $PluginModule = new PluginModule('bar', 'bar/bar', 'dummy', [ 'name' => 'Bar', 'description' => 'The best bar ever', 'active' => false, 'position' => 12, 'bundle' => 'plugins/bundle.min', 'tags' => ['dummy', 'goofy'] ]); $serialized = json_encode($PluginModule); $this->assertEquals($expected, $serialized); } }