Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pfilsx committed Oct 18, 2019
1 parent 123c20c commit cc42d4f
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 3 deletions.
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
<exclude>
<directory suffix="Interface.php">./src</directory>
<directory prefix="Abstract">./src</directory>
<directory>./src/DependencyInjection</directory>
<directory>./src/Maker</directory>
<directory>./src/Resources</directory>
<file>./src/Grid/Providers/DataProvider.php</file>
</exclude>
Expand Down
4 changes: 3 additions & 1 deletion src/Grid/Items/EntityGridItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
133 changes: 133 additions & 0 deletions tests/items/DataGridItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php


namespace Pfilsx\tests\items;


use Pfilsx\DataGrid\DataGridException;
use Pfilsx\DataGrid\Grid\Items\ArrayGridItem;
use Pfilsx\DataGrid\Grid\Items\EntityGridItem;
use PHPUnit\Framework\TestCase;

class DataGridItemTest extends TestCase
{

/**
* @param EntityGridItem|ArrayGridItem $item
* @dataProvider getGridItems
*/
public function testHas($item)
{
$this->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]
];
}
}
45 changes: 43 additions & 2 deletions tests/providers/ArrayDataProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit cc42d4f

Please sign in to comment.