-
Notifications
You must be signed in to change notification settings - Fork 20
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
f635af0
commit b2fe808
Showing
9 changed files
with
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?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 | ||
{ | ||
final public const LOCATION_LIST_KEY = 'locations'; | ||
|
||
final public const LOCATION_LIST_VIEW = 'app.locations_list'; | ||
|
||
public function __construct( | ||
private readonly ViewBuilderFactoryInterface $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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Controller\Admin; | ||
|
||
use App\Common\DoctrineListRepresentationFactory; | ||
use App\Entity\Location; | ||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
class LocationController extends AbstractController | ||
{ | ||
public function __construct( | ||
private readonly DoctrineListRepresentationFactory $doctrineListRepresentationFactory, | ||
) { | ||
} | ||
|
||
#[Route(path: '/admin/api/locations', methods: ['GET'], name: 'app.get_location_list')] | ||
public function getListAction(): Response | ||
{ | ||
$listRepresentation = $this->doctrineListRepresentationFactory->createDoctrineListRepresentation( | ||
Location::RESOURCE_KEY, | ||
); | ||
|
||
return $this->json($listRepresentation->toArray()); | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
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,57 @@ | ||
<?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; | ||
|
||
private KernelBrowser $client; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->client = $this->createAuthenticatedClient(); | ||
$this->purgeDatabase(); | ||
} | ||
|
||
public function testGetList(): void | ||
{ | ||
$location1 = $this->createLocation('Sulu'); | ||
$location2 = $this->createLocation('Symfony'); | ||
|
||
$this->client->jsonRequest('GET', '/admin/api/locations'); | ||
|
||
$response = $this->client->getResponse(); | ||
$this->assertInstanceOf(Response::class, $response); | ||
/** | ||
* @var array{ | ||
* _embedded: array{ | ||
* locations: array<array{ | ||
* id: int, | ||
* name: string, | ||
* }> | ||
* }, | ||
* total: int, | ||
* } $result | ||
*/ | ||
$result = \json_decode($response->getContent() ?: '', true, 512, \JSON_THROW_ON_ERROR); | ||
$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(''); | ||
|
||
static::getEntityManager()->persist($location); | ||
static::getEntityManager()->flush(); | ||
|
||
return $location; | ||
} | ||
|
||
protected function getLocationRepository(): LocationRepository | ||
{ | ||
return static::getEntityManager()->getRepository(Location::class); | ||
} | ||
|
||
abstract protected static 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