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