diff --git a/phpunit.xml.dist b/phpunit.xml.dist index a98dd40..5c66379 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -27,6 +27,8 @@ ./src ./src + ./src/DependencyInjection + ./src/Maker ./src/Resources ./src/Grid/Providers/DataProvider.php diff --git a/src/Grid/Items/EntityGridItem.php b/src/Grid/Items/EntityGridItem.php index 1ac0c6f..20f4314 100644 --- a/src/Grid/Items/EntityGridItem.php +++ b/src/Grid/Items/EntityGridItem.php @@ -35,7 +35,9 @@ public function get(string $attribute) private function getPropertyAccessVariations(string $attribute): array { - $camelAttribute = str_replace('_', '', $attribute); + $camelAttribute = preg_replace_callback('/_([A-z]?)/', function ($matches) { + return isset($matches[1]) ? strtoupper($matches[1]) : ''; + }, $attribute); $getter = 'get' . $camelAttribute; return [$camelAttribute, $getter]; } diff --git a/tests/items/DataGridItemTest.php b/tests/items/DataGridItemTest.php new file mode 100644 index 0000000..53c3c5f --- /dev/null +++ b/tests/items/DataGridItemTest.php @@ -0,0 +1,133 @@ +assertTrue($item->has('id')); + $this->assertTrue(!empty($item['id'])); + $this->assertTrue(isset($item->id)); + + $this->assertTrue($item->has('name')); + $this->assertTrue(!empty($item['name'])); + $this->assertTrue(isset($item->name)); + + $this->assertTrue($item->has('created_by')); + $this->assertTrue(!empty($item['created_by'])); + $this->assertTrue(isset($item->created_by)); + + $this->assertFalse($item->has('test')); + $this->assertFalse(!empty($item['test'])); + $this->assertFalse(isset($item->test)); + } + + /** + * @param EntityGridItem|ArrayGridItem $item + * @dataProvider getGridItems + */ + public function testGet($item) + { + $this->assertEquals(1, $item->get('id')); + $this->assertEquals(1, $item->id); + $this->assertEquals(1, $item['id']); + + $this->assertEquals('FooBar', $item->get('name')); + $this->assertEquals('FooBar', $item->name); + $this->assertEquals('FooBar', $item['name']); + + $this->assertEquals('author', $item->get('created_by')); + $this->assertEquals('author', $item->created_by); + $this->assertEquals('author', $item['created_by']); + + $this->expectException(DataGridException::class); + $item->get('test'); + } + + /** + * @param EntityGridItem|ArrayGridItem $item + * @param array|object $result + * @dataProvider getGridItems + */ + public function testGetData($item, $result) + { + $this->assertEquals($result, $item->getData()); + } + /** + * @param EntityGridItem|ArrayGridItem $item + * @dataProvider getGridItems + */ + public function testSet($item) + { + $this->expectException(DataGridException::class); + $item->test = 'FooBar'; + } + /** + * @param EntityGridItem|ArrayGridItem $item + * @dataProvider getGridItems + */ + public function testAccessSet($item) + { + $this->expectException(DataGridException::class); + $item['test'] = 'FooBar'; + } + /** + * @param EntityGridItem|ArrayGridItem $item + * @dataProvider getGridItems + */ + public function testUnset($item) + { + $this->expectException(DataGridException::class); + unset($item->id); + } + /** + * @param EntityGridItem|ArrayGridItem $item + * @dataProvider getGridItems + */ + public function testAccessUnset($item) + { + $this->expectException(DataGridException::class); + unset($item['id']); + } + + + public function getGridItems() + { + $entity = new class + { + private $id = 1; + + public $name = 'FooBar'; + + public $createdBy = 'author'; + + public function getId() + { + return $this->id; + } + }; + $row = [ + 'id' => 1, + 'name' => 'FooBar', + 'created_by' => 'author' + ]; + + return [ + [new ArrayGridItem($row, 'id'), $row], + [new EntityGridItem($entity, 'id'), $entity] + ]; + } +} \ No newline at end of file diff --git a/tests/providers/ArrayDataProviderTest.php b/tests/providers/ArrayDataProviderTest.php index 11038b2..5b5178e 100644 --- a/tests/providers/ArrayDataProviderTest.php +++ b/tests/providers/ArrayDataProviderTest.php @@ -5,22 +5,29 @@ use DateTime; +use Pfilsx\DataGrid\DataGridException; use Pfilsx\DataGrid\Grid\Items\ArrayGridItem; use Pfilsx\DataGrid\Grid\Pager; use Pfilsx\DataGrid\Grid\Providers\ArrayDataProvider; use Pfilsx\DataGrid\Grid\Providers\DataProvider; -use PHPUnit\Framework\TestCase; +use Pfilsx\tests\OrmTestCase; -class ArrayDataProviderTest extends TestCase +class ArrayDataProviderTest extends OrmTestCase { /** * @var DataProvider */ protected $provider; + /** + * @var DataProvider + */ + protected $provider2; protected function setUp(): void { + parent::setUp(); + $itemsSets = $this->getItemSets(); $this->provider = DataProvider::create([ @@ -29,12 +36,35 @@ protected function setUp(): void $itemsSets[2], $itemsSets[4] ]); + $this->provider2 = DataProvider::create([ + new class{ + protected $id = 1; + public function getId(){ + return $this->id; + } + }, + new class{ + protected $id = 2; + public function getId(){ + return $this->id; + } + } + ], $this->containerArray['doctrine']); $this->provider->setPager(new Pager()); } public function testType(): void { $this->assertInstanceOf(ArrayDataProvider::class, $this->provider); + $this->assertInstanceOf(ArrayDataProvider::class, $this->provider2); + } + + public function testWrongCreation(): void { + $this->expectException(DataGridException::class); + DataProvider::create([ + 'first', + 'second' + ]); } public function testGetItems(): void @@ -45,6 +75,17 @@ public function testGetItems(): void $this->assertEquals(4, $this->provider->getTotalCount()); } + public function testPager(): void { + $this->provider->getPager()->disable(); + $this->assertEquals(false, $this->provider->getPager()->isEnabled()); + $this->provider->getPager()->enable(); + $this->assertEquals(true, $this->provider->getPager()->isEnabled()); + $this->provider->getPager()->setLimit(2); + $this->assertEquals(2, $this->provider->getPager()->getLimit()); + $items = $this->provider->getItems(); + $this->assertCount(2, $items); + } + /** * @dataProvider sortDataProvider * @param $attr