Skip to content

Commit

Permalink
Add composite primary keys tests
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Jan 17, 2021
1 parent af7a90a commit 81b5038
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 0 deletions.
45 changes: 45 additions & 0 deletions tests/App/Admin/CarAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\App\Admin;

use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;

final class CarAdmin extends AbstractAdmin
{
protected function configureListFields(ListMapper $list): void
{
$list
->add('name')
->add('year');
}

protected function configureDatagridFilters(DatagridMapper $filter): void
{
$filter
->add('name')
->add('year');
}

protected function configureFormFields(FormMapper $form): void
{
$form
->add('name', TextType::class)
->add('year', IntegerType::class);
}
}
33 changes: 33 additions & 0 deletions tests/App/DataFixtures/CarFixtures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Car;

final class CarFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
$peugeot2000 = new Car('Peugeot', 2000);
$peugeot2010 = new Car('Peugeot', 2010);
$ferrari2000 = new Car('Ferrari', 2000);

$manager->persist($peugeot2000);
$manager->persist($peugeot2010);
$manager->persist($ferrari2000);
$manager->flush();
}
}
67 changes: 67 additions & 0 deletions tests/App/Entity/Car.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\App\Entity;

use Doctrine\ORM\Mapping as ORM;

/** @ORM\Entity */
class Car
{
/**
* @ORM\Id
* @ORM\Column(type="string")
*
* @var string
*/
private $name;

/**
* @ORM\Id
* @ORM\Column(type="integer")
*
* @var int
*/
private $year;

public function __construct(string $name = '', int $year = 0)
{
$this->name = $name;
$this->year = $year;
}

public function __toString(): string
{
return $this->name.' - '.$this->year;
}

public function getName(): string
{
return $this->name;
}

public function setName(string $name): void
{
$this->name = $name;
}

public function getYear(): int
{
return $this->year;
}

public function setYear(int $year): void
{
$this->year = $year;
}
}
13 changes: 13 additions & 0 deletions tests/App/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@

use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\AuthorAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\BookAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\CarAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Admin\CategoryAdmin;
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Author;
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Book;
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Car;
use Sonata\DoctrineORMAdminBundle\Tests\App\Entity\Category;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

Expand Down Expand Up @@ -62,5 +64,16 @@
null,
])

->set(CarAdmin::class)
->public()
->tag('sonata.admin', [
'manager_type' => 'orm',
'label' => 'Car',
])
->args([
'',
Car::class,
null,
])
;
};
26 changes: 26 additions & 0 deletions tests/Functional/CompositePrimaryKeysTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\DoctrineORMAdminBundle\Tests\Functional;

use Symfony\Component\HttpFoundation\Request;

final class CompositePrimaryKeysTest extends BasePantherTestCase
{
public function testListCompositePrimaryKeys(): void
{
$this->client->request(Request::METHOD_GET, '/admin/tests/app/car/list');

self::assertSelectorTextContains('.box-footer', '1 / 1 - 3 results');
}
}

0 comments on commit 81b5038

Please sign in to comment.