Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"require": {
"php": "^7.0",
"twig/twig": "^1.7",
"symfony/framework-bundle" : "^3.0",
"symfony/framework-bundle" : "^3.3",
"symfony/form" : "^3.0",
"symfony/intl" : "^3.0",
"symfony/options-resolver": "^3.0",
Expand All @@ -20,7 +20,8 @@
"symfony/translation": "^3.0",
"sensio/framework-extra-bundle": "^3.0",
"symfony/twig-bundle": "^3.0",
"doctrine/orm": "^2.5"
"doctrine/orm": "^2.5",
"symfony/security": "^3.3"
},
"require-dev": {
"symfony/class-loader": "^3.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace spec\AdminPanel\Symfony\AdminBundle\Admin\CRUD\Context\Request;

use AdminPanel\Component\DataSource\DataSource;
use AdminPanel\Component\DataSource\Exception\PageNotFoundException;
use AdminPanel\Symfony\AdminBundle\Event\AdminEvent;
use AdminPanel\Symfony\AdminBundle\Event\ListEvent;
use AdminPanel\Symfony\AdminBundle\Event\ListEvents;
Expand All @@ -14,6 +15,7 @@
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class DataGridSetDataHandlerSpec extends ObjectBehavior
{
Expand Down Expand Up @@ -96,4 +98,18 @@ public function it_return_response_from_datagrid_post_bind_data(
$this->handleRequest($event, $request)
->shouldReturnAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}

public function it_throws_not_found_exception_if_current_page_is_out_of_bound(
DataSource $dataSource,
DataGrid $dataGrid,
ListEvent $event,
Request $request
) {
$dataSource->getResult()->willThrow(new PageNotFoundException());

$event->getDataGrid()->willReturn($dataGrid);
$event->getDataSource()->willReturn($dataSource);

$this->shouldThrow(NotFoundHttpException::class)->during('handleRequest', [$event, $request]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,7 @@ public function it_loads_resources_config_only_if_resource_repository_extension_
) {
$container->hasExtension(Argument::type('string'))->willReturn(false);
$container->hasExtension('admin_panel_resource_repository')->willReturn(true);

$container->addResource(Argument::allOf(
Argument::type('Symfony\Component\Config\Resource\FileResource'),
Argument::that(function ($value) {
return $value instanceof FileResource &&
preg_match('/context\/resource\.xml$/', $value->getResource());
})
))->shouldBeCalled();
$container->fileExists(Argument::type('string'))->willReturn(true);

$container->getParameterBag()->willReturn($bag);
$container->setDefinition(
Expand Down
20 changes: 20 additions & 0 deletions src/AdminPanel/Component/DataSource/DataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use AdminPanel\Component\DataSource\Event\DataSourceEvent\ResultEventArgs;
use AdminPanel\Component\DataSource\Event\DataSourceEvent\ViewEventArgs;
use AdminPanel\Component\DataSource\Exception\DataSourceException;
use AdminPanel\Component\DataSource\Exception\PageNotFoundException;
use AdminPanel\Component\DataSource\Field\FieldTypeInterface;
use Symfony\Component\EventDispatcher\EventDispatcher;
use AdminPanel\Component\DataSource\Event\DataSourceEvents;
Expand Down Expand Up @@ -251,6 +252,8 @@ public function getResult()

$result = $this->driver->getResult($this->fields, $this->getFirstResult(), $this->getMaxResults());

$this->assertPage($result);

foreach ($this->getFields() as $field) {
$field->setDirty(false);
}
Expand Down Expand Up @@ -473,4 +476,21 @@ private function checkFieldsClarity()
$this->dirty = false;
}
}

private function assertPage($result)
{
if (!$result instanceof \IteratorAggregate) {
// Invalid results
return;
}

if (count($result) === 0) {
// List with no results at all
return;
}

if (count($result->getIterator()) === 0) {
throw new PageNotFoundException();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace AdminPanel\Component\DataSource\Exception;

final class PageNotFoundException extends DataSourceException
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

namespace AdminPanel\Symfony\AdminBundle\Admin\CRUD\Context\Request;

use AdminPanel\Component\DataSource\Exception\PageNotFoundException;
use AdminPanel\Symfony\AdminBundle\Admin\Context\Request\AbstractHandler;
use AdminPanel\Symfony\AdminBundle\Event\AdminEvent;
use AdminPanel\Symfony\AdminBundle\Event\ListEvent;
use AdminPanel\Symfony\AdminBundle\Event\ListEvents;
use AdminPanel\Symfony\AdminBundle\Exception\RequestHandlerException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class DataGridSetDataHandler extends AbstractHandler
{
Expand All @@ -28,7 +30,12 @@ public function handleRequest(AdminEvent $event, Request $request)
return $event->getResponse();
}

$event->getDataGrid()->setData($event->getDataSource()->getResult());
try {
$event->getDataGrid()->setData($event->getDataSource()->getResult());
} catch (PageNotFoundException $e) {
throw new NotFoundHttpException($e->getMessage(), $e, $e->getCode());
}

$this->eventDispatcher->dispatch(ListEvents::LIST_DATAGRID_DATA_POST_BIND, $event);

if ($event->hasResponse()) {
Expand Down
30 changes: 30 additions & 0 deletions tests/AdminPanel/Component/DataSource/Tests/DataSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,36 @@ public function testPagination()
$this->assertEquals($datasource->getFirstResult(), $first);
}

/**
* Checks if exception is thrown when page is out of bound
*/
public function testPageNotFoundException()
{
$driver = $this->createMock(DriverInterface::class);
$dataSource = new DataSource($driver);

$driver
->expects($this->once())
->method('getResult')
->will($this->returnValue(
new class implements \IteratorAggregate, \Countable
{
public function count()
{
return 12;
}

public function getIterator()
{
return new \ArrayIterator([]);
}
}
));

$this->expectException('AdminPanel\Component\DataSource\Exception\PageNotFoundException');
$dataSource->getResult();
}

/**
* Checks preGetParameters and postGetParameters calls.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ public function testHavingClauseInEntityField()
->select('n')
->from('AdminPanel\Component\DataSource\Tests\Fixtures\News', 'n')
->join('n.category', 'c')
->groupBy('n.category')
;

$driverOptions = [
Expand All @@ -615,7 +616,7 @@ public function testHavingClauseInEntityField()

$this->assertEquals(
$this->testDoctrineExtension->getQueryBuilder()->getQuery()->getDQL(),
'SELECT n FROM AdminPanel\Component\DataSource\Tests\Fixtures\News n INNER JOIN n.category c HAVING n.category IN (:category)'
'SELECT n FROM AdminPanel\Component\DataSource\Tests\Fixtures\News n INNER JOIN n.category c GROUP BY n.category HAVING n.category IN (:category)'
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,7 @@ public function testHavingClauseInEntityField()
->select('n')
->from('AdminPanel\Component\DataSource\Tests\Fixtures\News', 'n')
->join('n.category', 'c')
->groupBy('n.category')
;

$driverOptions = [
Expand All @@ -642,7 +643,7 @@ public function testHavingClauseInEntityField()

$this->assertEquals(
$this->testDoctrineExtension->getQueryBuilder()->getQuery()->getDQL(),
'SELECT n FROM AdminPanel\Component\DataSource\Tests\Fixtures\News n INNER JOIN n.category c HAVING n.category IN (:category)'
'SELECT n FROM AdminPanel\Component\DataSource\Tests\Fixtures\News n INNER JOIN n.category c GROUP BY n.category HAVING n.category IN (:category)'
);
}

Expand Down