-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
09 - Add a list representation for locations in the admin interface
- Loading branch information
1 parent
1db527d
commit d95eb0f
Showing
10 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?xml version="1.0" ?> | ||
<list xmlns="http://schemas.sulu.io/list-builder/list"> | ||
<key>locations</key> | ||
|
||
<properties> | ||
<property name="id" visibility="no" translation="sulu_admin.id"> | ||
<field-name>id</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="name" visibility="always" searchability="yes" translation="sulu_admin.name"> | ||
<field-name>name</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="street" visibility="yes" searchability="yes" translation="sulu_contact.street"> | ||
<field-name>street</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="number" visibility="yes" searchability="yes" translation="sulu_contact.number"> | ||
<field-name>number</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="postalCode" visibility="yes" searchability="yes" translation="sulu_contact.zip"> | ||
<field-name>postalCode</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="city" visibility="yes" searchability="yes" translation="sulu_contact.city"> | ||
<field-name>city</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
|
||
<property name="countryCode" visibility="yes" searchability="yes" translation="sulu_contact.country"> | ||
<field-name>countryCode</field-name> | ||
<entity-name>App\Entity\Location</entity-name> | ||
</property> | ||
</properties> | ||
</list> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Admin; | ||
|
||
use App\Entity\Location; | ||
use Sulu\Bundle\AdminBundle\Admin\Admin; | ||
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItem; | ||
use Sulu\Bundle\AdminBundle\Admin\Navigation\NavigationItemCollection; | ||
use Sulu\Bundle\AdminBundle\Admin\View\ViewBuilderFactoryInterface; | ||
use Sulu\Bundle\AdminBundle\Admin\View\ViewCollection; | ||
|
||
class LocationAdmin extends Admin | ||
{ | ||
const LOCATION_LIST_KEY = 'locations'; | ||
|
||
const LOCATION_LIST_VIEW = 'app.locations_list'; | ||
|
||
/** | ||
* @var ViewBuilderFactoryInterface | ||
*/ | ||
private $viewBuilderFactory; | ||
|
||
public function __construct(ViewBuilderFactoryInterface $viewBuilderFactory) | ||
{ | ||
$this->viewBuilderFactory = $viewBuilderFactory; | ||
} | ||
|
||
public function configureNavigationItems(NavigationItemCollection $navigationItemCollection): void | ||
{ | ||
$module = $navigationItemCollection->get('app.events'); | ||
|
||
$locations = new NavigationItem('app.locations'); | ||
$locations->setPosition(10); | ||
$locations->setView(static::LOCATION_LIST_VIEW); | ||
|
||
$module->addChild($locations); | ||
} | ||
|
||
public function configureViews(ViewCollection $viewCollection): void | ||
{ | ||
$listView = $this->viewBuilderFactory->createListViewBuilder(self::LOCATION_LIST_VIEW, '/locations') | ||
->setResourceKey(Location::RESOURCE_KEY) | ||
->setListKey(self::LOCATION_LIST_KEY) | ||
->setTitle('app.locations') | ||
->addListAdapters(['table']) | ||
->addToolbarActions([]); | ||
$viewCollection->add($listView); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Admin; | ||
|
||
use App\Common\DoctrineListRepresentationFactory; | ||
use App\Entity\Location; | ||
use FOS\RestBundle\Controller\Annotations\RouteResource; | ||
use FOS\RestBundle\Routing\ClassResourceInterface; | ||
use FOS\RestBundle\View\ViewHandlerInterface; | ||
use Sulu\Component\Rest\AbstractRestController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; | ||
|
||
/** | ||
* @RouteResource("Location") | ||
*/ | ||
class LocationController extends AbstractRestController implements ClassResourceInterface | ||
{ | ||
/** | ||
* @var DoctrineListRepresentationFactory | ||
*/ | ||
private $doctrineListRepresentationFactory; | ||
|
||
public function __construct( | ||
DoctrineListRepresentationFactory $doctrineListRepresentationFactory, | ||
ViewHandlerInterface $viewHandler, | ||
?TokenStorageInterface $tokenStorage = null | ||
) { | ||
$this->doctrineListRepresentationFactory = $doctrineListRepresentationFactory; | ||
|
||
parent::__construct($viewHandler, $tokenStorage); | ||
} | ||
|
||
public function cgetAction(): Response | ||
{ | ||
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation( | ||
Location::RESOURCE_KEY | ||
); | ||
|
||
return $this->handleView($this->view($listRepresentation)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,8 @@ | |
*/ | ||
class Location | ||
{ | ||
const RESOURCE_KEY = 'locations'; | ||
|
||
/** | ||
* @var int|null | ||
* | ||
|
49 changes: 49 additions & 0 deletions
49
tests/Functional/Controller/Admin/LocationControllerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Functional\Controller\Admin; | ||
|
||
use App\Tests\Functional\Traits\LocationTrait; | ||
use Sulu\Bundle\TestBundle\Testing\SuluTestCase; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
class LocationControllerTest extends SuluTestCase | ||
{ | ||
use LocationTrait; | ||
|
||
/** | ||
* @var KernelBrowser | ||
*/ | ||
private $client; | ||
|
||
public function setUp(): void | ||
{ | ||
$this->client = $this->createAuthenticatedClient(); | ||
$this->purgeDatabase(); | ||
} | ||
|
||
public function testCGet(): void | ||
{ | ||
$location1 = $this->createLocation('Sulu'); | ||
$location2 = $this->createLocation('Symfony'); | ||
|
||
$this->client->request('GET', '/admin/api/locations'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertInstanceOf(Response::class, $response); | ||
$result = json_decode($response->getContent() ?: '', true); | ||
$this->assertHttpStatusCode(200, $response); | ||
|
||
$this->assertSame(2, $result['total']); | ||
$this->assertCount(2, $result['_embedded']['locations']); | ||
$items = $result['_embedded']['locations']; | ||
|
||
$this->assertSame($location1->getId(), $items[0]['id']); | ||
$this->assertSame($location2->getId(), $items[1]['id']); | ||
|
||
$this->assertSame($location1->getName(), $items[0]['name']); | ||
$this->assertSame($location2->getName(), $items[1]['name']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Functional\Traits; | ||
|
||
use App\Entity\Location; | ||
use App\Repository\LocationRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
|
||
trait LocationTrait | ||
{ | ||
public function createLocation(string $name): Location | ||
{ | ||
$location = $this->getLocationRepository()->create(); | ||
$location->setName($name); | ||
$location->setStreet(''); | ||
$location->setNumber(''); | ||
$location->setPostalCode(''); | ||
$location->setCity(''); | ||
$location->setCountryCode(''); | ||
|
||
$this->getEntityManager()->persist($location); | ||
$this->getEntityManager()->flush(); | ||
|
||
return $location; | ||
} | ||
|
||
protected function getLocationRepository(): LocationRepository | ||
{ | ||
return $this->getEntityManager()->getRepository(Location::class); | ||
} | ||
|
||
abstract protected function getEntityManager(): EntityManagerInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters