order the type alphabetically

Improve also the form "TranslatableActivityType" to use the
TranslatableStringHelper.
This commit is contained in:
2016-03-09 20:54:38 +01:00
parent 97df048dfb
commit 1ab0b95e74
5 changed files with 163 additions and 10 deletions

View File

@@ -0,0 +1,115 @@
<?php
/*
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace Chill\ActivityBundle\Tests\Form\Type;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class TranslatableActivityTypeTest extends KernelTestCase
{
/**
*
* @var \Symfony\Component\Form\FormBuilderInterface
*/
protected $builder;
/**
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
public function setUp()
{
self::bootKernel();
$this->container = self::$kernel->getContainer();
$this->builder = $this->container
->get('form.factory')
->createBuilder(FormType::class, null, array(
'csrf_protection' => false,
'csrf_field_name' => '_token'
));
$request = new \Symfony\Component\HttpFoundation\Request();
$request->setLocale('fr');
$this->container->get('request_stack')
->push($request);
}
/**
*
* @return \Chill\ActivityBundle\Entity\ActivityType
*/
protected function getRandomType()
{
$types = $this->container->get('doctrine.orm.entity_manager')
->getRepository('ChillActivityBundle:ActivityType')
->findAll();
return $types[array_rand($types)];
}
public function testForm()
{
$type = $this->getRandomType();
$form = $this->builder->add('type', TranslatableActivityType::class)
->getForm();
$form->submit(array(
'type' => $type->getId()
));
$this->assertTrue($form->isSynchronized());
$this->assertInstanceOf(\Chill\ActivityBundle\Entity\ActivityType::class,
$form->getData()['type'],
"The data is an instance of Chill\ActivityBundle\Entity\ActivityType");
$this->assertEquals($type->getId(), $form->getData()['type']->getId());
// test the ordering of the types in the form
$view = $form->createView();
$this->assertGreaterThan(0, count($view['type']->vars['choices']),
"test that there are at least one choice");
foreach($view['type']->vars['choices'] as $choice) {
// initialize the previous value is not set (this is the first)
if (!isset($previous)) {
$previous = $choice->label;
} else {
$this->assertTrue($previous < $choice->label);
$previous = $choice->label;
}
}
}
}