mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-05 14:25:00 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,183 +1,170 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ActivityBundle\Tests\Form;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\ActivityBundle\Form\ActivityType;
|
||||
use Chill\ActivityBundle\Entity\Activity;
|
||||
use Chill\ActivityBundle\Form\ActivityType;
|
||||
use DateTime;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\AbstractToken;
|
||||
use Symfony\Component\Security\Core\Role\Role;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class ActivityTypeTest extends KernelTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Form\FormBuilderInterface
|
||||
* @var \Chill\MainBundle\Entity\Center
|
||||
*/
|
||||
protected $formBuilder;
|
||||
protected $center;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\DependencyInjection\ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Symfony\Component\Form\FormBuilderInterface
|
||||
*/
|
||||
protected $formBuilder;
|
||||
|
||||
/**
|
||||
* @var \Symfony\Component\Security\Core\User\UserInterface
|
||||
*/
|
||||
protected $user;
|
||||
|
||||
/**
|
||||
*
|
||||
* @var \Chill\MainBundle\Entity\Center
|
||||
*/
|
||||
protected $center;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
$this->container = self::$kernel->getContainer();
|
||||
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
|
||||
$prophet = new \Prophecy\Prophet();
|
||||
|
||||
$this->formBuilder = $this->container
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, null, array(
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token'
|
||||
));
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, null, [
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token',
|
||||
]);
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->setLocale('fr');
|
||||
|
||||
self::$kernel->getContainer()
|
||||
->get('request_stack')
|
||||
->push($request);
|
||||
->get('request_stack')
|
||||
->push($request);
|
||||
|
||||
$this->user = $this->container->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillMainBundle:User')
|
||||
->findOneBy(array('username' => 'center a_social'));
|
||||
->getRepository('ChillMainBundle:User')
|
||||
->findOneBy(['username' => 'center a_social']);
|
||||
$this->center = $this->container->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillMainBundle:Center')
|
||||
->findOneBy(array('name' => 'Center A'));
|
||||
->getRepository('ChillMainBundle:Center')
|
||||
->findOneBy(['name' => 'Center A']);
|
||||
$token = $prophet->prophesize();
|
||||
$token->willExtend(AbstractToken::class);
|
||||
$token->getUser()->willReturn($this->user);
|
||||
$this->container->get('security.token_storage')
|
||||
->setToken($token->reveal());
|
||||
|
||||
->setToken($token->reveal());
|
||||
}
|
||||
|
||||
public function testForm()
|
||||
{
|
||||
$form = $this->formBuilder
|
||||
->add('activity', ActivityType::class, array(
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE')
|
||||
))
|
||||
->getForm();
|
||||
->add('activity', ActivityType::class, [
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE'),
|
||||
])
|
||||
->getForm();
|
||||
|
||||
$form->submit(array());
|
||||
$form->submit([]);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertTrue($form->isValid());
|
||||
$this->assertInstanceOf(Activity::class, $form->getData()['activity']);
|
||||
|
||||
}
|
||||
|
||||
public function testFormSubmitting()
|
||||
{
|
||||
$form = $this->formBuilder
|
||||
->add('activity', ActivityType::class, array(
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE')
|
||||
))
|
||||
->getForm();
|
||||
->add('activity', ActivityType::class, [
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE'),
|
||||
])
|
||||
->getForm();
|
||||
|
||||
$form->submit(array( 'activity' => array(
|
||||
$form->submit(['activity' => [
|
||||
'date' => '9-3-2015',
|
||||
'durationTime' => 300,
|
||||
// 'remark' => 'blabla',
|
||||
'attendee' => true
|
||||
)));
|
||||
// 'remark' => 'blabla',
|
||||
'attendee' => true,
|
||||
]]);
|
||||
|
||||
// var_dump($form->getErrors()->count()); var_dump($form->isValid());
|
||||
// foreach($form->getErrors() as $e) { fwrite(STDOUT, var_dump($e->getMessage())); }
|
||||
// var_dump($form->getErrors());
|
||||
|
||||
$this->assertTrue($form->isSynchronized(), "Test the form is synchronized");
|
||||
$this->assertTrue($form->isValid(), "test the form is valid");
|
||||
$this->assertTrue($form->isSynchronized(), 'Test the form is synchronized');
|
||||
$this->assertTrue($form->isValid(), 'test the form is valid');
|
||||
$this->assertInstanceOf(Activity::class, $form->getData()['activity']);
|
||||
|
||||
// test the activity
|
||||
/* @var $activity Activity */
|
||||
$activity = $form->getData()['activity'];
|
||||
|
||||
$this->assertEquals('09-03-2015', $activity->getDate()->format('d-m-Y'),
|
||||
"Test the date is correct");
|
||||
$this->assertEquals('00:05', $activity->getDurationTime()->format('H:i'),
|
||||
"Test the formatted hour is correct");
|
||||
$this->assertEquals(
|
||||
'09-03-2015',
|
||||
$activity->getDate()->format('d-m-Y'),
|
||||
'Test the date is correct'
|
||||
);
|
||||
$this->assertEquals(
|
||||
'00:05',
|
||||
$activity->getDurationTime()->format('H:i'),
|
||||
'Test the formatted hour is correct'
|
||||
);
|
||||
$this->assertEquals(true, $activity->getAttendee());
|
||||
// $this->assertEquals('blabla', $activity->getRemark());
|
||||
|
||||
// $this->assertEquals('blabla', $activity->getRemark());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the form correctly build even with a durationTime which is not in
|
||||
* the listed in the possible durationTime
|
||||
* the listed in the possible durationTime.
|
||||
*/
|
||||
public function testFormWithActivityHavingDifferentTime()
|
||||
{
|
||||
$activity = new Activity();
|
||||
$activity->setDurationTime(\DateTime::createFromFormat('U', 60));
|
||||
$activity->setDurationTime(DateTime::createFromFormat('U', 60));
|
||||
|
||||
$builder = $this->container
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, array('activity' => $activity), array(
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token'
|
||||
));
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, ['activity' => $activity], [
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token',
|
||||
]);
|
||||
|
||||
$form = $builder
|
||||
->add('activity', ActivityType::class, array(
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE')
|
||||
))
|
||||
->getForm();
|
||||
->add('activity', ActivityType::class, [
|
||||
'center' => $this->center,
|
||||
'role' => new Role('CHILL_ACTIVITY_CREATE'),
|
||||
])
|
||||
->getForm();
|
||||
|
||||
|
||||
$form->submit(array( 'activity' => array(
|
||||
$form->submit(['activity' => [
|
||||
'date' => '9-3-2015',
|
||||
'durationTime' => 60,
|
||||
// 'remark' => 'blabla',
|
||||
'attendee' => true
|
||||
)));
|
||||
// 'remark' => 'blabla',
|
||||
'attendee' => true,
|
||||
]]);
|
||||
|
||||
$this->assertTrue($form->isSynchronized());
|
||||
$this->assertTrue($form->isValid());
|
||||
@@ -186,8 +173,11 @@ class ActivityTypeTest extends KernelTestCase
|
||||
/* @var $activity Activity */
|
||||
$activity = $form->getData()['activity'];
|
||||
|
||||
$this->assertEquals('00:01', $activity->getDurationTime()->format('H:i'),
|
||||
"Test the formatted hour is correct");
|
||||
$this->assertEquals(
|
||||
'00:01',
|
||||
$activity->getDurationTime()->format('H:i'),
|
||||
'Test the formatted hour is correct'
|
||||
);
|
||||
|
||||
// test the view : we want to be sure that the entry with 60 seconds exists
|
||||
$view = $form->createView();
|
||||
@@ -195,11 +185,11 @@ class ActivityTypeTest extends KernelTestCase
|
||||
$this->assertTrue(isset($view['activity']['durationTime']));
|
||||
|
||||
// map all the values in an array
|
||||
$values = array_map(function($choice) { return $choice->value; },
|
||||
$view['activity']['durationTime']->vars['choices']);
|
||||
$values = array_map(
|
||||
function ($choice) { return $choice->value; },
|
||||
$view['activity']['durationTime']->vars['choices']
|
||||
);
|
||||
|
||||
$this->assertContains(60, $values);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -1,110 +1,97 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
* Copyright (C) 2015 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ActivityBundle\Tests\Form\Type;
|
||||
|
||||
use Symfony\Component\Form\Test\TypeTestCase;
|
||||
use Chill\ActivityBundle\Form\Type\TranslatableActivityReason;
|
||||
use Chill\MainBundle\Templating\TranslatableStringHelper;
|
||||
use Symfony\Component\Form\PreloadedExtension;
|
||||
use Symfony\Component\Form\Test\TypeTestCase;
|
||||
|
||||
/**
|
||||
* Test translatableActivityReason
|
||||
* Test translatableActivityReason.
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @author Champs Libres <info@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
class TranslatableActivityReasonTest extends TypeTestCase
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @var Prophecy\Prophet
|
||||
* @var Prophecy\Prophet
|
||||
*/
|
||||
private static $prophet;
|
||||
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
|
||||
}
|
||||
|
||||
protected function getExtensions()
|
||||
{
|
||||
$entityType = $this->getEntityType();
|
||||
|
||||
return array(new PreloadedExtension(array(
|
||||
'entity' => $entityType
|
||||
), array()));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function testSimple()
|
||||
{
|
||||
$translatableActivityReasonType = new TranslatableActivityReason(
|
||||
$this->getTranslatableStringHelper()
|
||||
);
|
||||
|
||||
$this->markTestSkipped("See issue 651");
|
||||
$this->getTranslatableStringHelper()
|
||||
);
|
||||
|
||||
$this->markTestSkipped('See issue 651');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param string $locale
|
||||
* @param string $fallbackLocale
|
||||
* @return TranslatableStringHelper
|
||||
*/
|
||||
protected function getTranslatableStringHelper($locale = 'en',
|
||||
$fallbackLocale = 'en')
|
||||
{
|
||||
$prophet = new \Prophecy\Prophet;
|
||||
$requestStack = $prophet->prophesize();
|
||||
$request = $prophet->prophesize();
|
||||
$translator = $prophet->prophesize();
|
||||
|
||||
$request->willExtend('Symfony\Component\HttpFoundation\Request');
|
||||
$request->getLocale()->willReturn($fallbackLocale);
|
||||
|
||||
$requestStack->willExtend('Symfony\Component\HttpFoundation\RequestStack');
|
||||
$requestStack->getCurrentRequest()->will(function () use ($request) {
|
||||
return $request;
|
||||
});
|
||||
|
||||
$translator->willExtend('Symfony\Component\Translation\Translator');
|
||||
$translator->getFallbackLocales()->willReturn($locale);
|
||||
|
||||
return new TranslatableStringHelper($requestStack->reveal(),
|
||||
$translator->reveal());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return \Symfony\Bridge\Doctrine\Form\Type\EntityType
|
||||
*/
|
||||
protected function getEntityType()
|
||||
{
|
||||
$managerRegistry = (new \Prophecy\Prophet())->prophesize();
|
||||
|
||||
|
||||
$managerRegistry->willImplement('Doctrine\Common\Persistence\ManagerRegistry');
|
||||
|
||||
|
||||
return new \Symfony\Bridge\Doctrine\Form\Type\EntityType($managerRegistry->reveal());
|
||||
}
|
||||
|
||||
protected function getExtensions()
|
||||
{
|
||||
$entityType = $this->getEntityType();
|
||||
|
||||
return [new PreloadedExtension([
|
||||
'entity' => $entityType,
|
||||
], [])];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $locale
|
||||
* @param string $fallbackLocale
|
||||
*
|
||||
* @return TranslatableStringHelper
|
||||
*/
|
||||
protected function getTranslatableStringHelper(
|
||||
$locale = 'en',
|
||||
$fallbackLocale = 'en'
|
||||
)
|
||||
{
|
||||
$prophet = new \Prophecy\Prophet();
|
||||
$requestStack = $prophet->prophesize();
|
||||
$request = $prophet->prophesize();
|
||||
$translator = $prophet->prophesize();
|
||||
|
||||
$request->willExtend('Symfony\Component\HttpFoundation\Request');
|
||||
$request->getLocale()->willReturn($fallbackLocale);
|
||||
|
||||
$requestStack->willExtend('Symfony\Component\HttpFoundation\RequestStack');
|
||||
$requestStack->getCurrentRequest()->will(function () use ($request) {
|
||||
return $request;
|
||||
});
|
||||
|
||||
$translator->willExtend('Symfony\Component\Translation\Translator');
|
||||
$translator->getFallbackLocales()->willReturn($locale);
|
||||
|
||||
return new TranslatableStringHelper(
|
||||
$requestStack->reveal(),
|
||||
$translator->reveal()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@@ -1,116 +1,101 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* Copyright (C) 2016 Champs-Libres <info@champs-libres.coop>
|
||||
/**
|
||||
* Chill is a software for social workers
|
||||
*
|
||||
* 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/>.
|
||||
* For the full copyright and license information, please view
|
||||
* the LICENSE file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Chill\ActivityBundle\Tests\Form\Type;
|
||||
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Chill\ActivityBundle\Form\Type\TranslatableActivityType;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Form\Extension\Core\Type\FormType;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
||||
* @internal
|
||||
* @coversNothing
|
||||
*/
|
||||
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'
|
||||
));
|
||||
|
||||
->get('form.factory')
|
||||
->createBuilder(FormType::class, null, [
|
||||
'csrf_protection' => false,
|
||||
'csrf_field_name' => '_token',
|
||||
]);
|
||||
|
||||
$request = new \Symfony\Component\HttpFoundation\Request();
|
||||
$request->setLocale('fr');
|
||||
|
||||
|
||||
$this->container->get('request_stack')
|
||||
->push($request);
|
||||
->push($request);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @return \Chill\ActivityBundle\Entity\ActivityType
|
||||
*/
|
||||
protected function getRandomType($active = true)
|
||||
{
|
||||
$types = $this->container->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillActivityBundle:ActivityType')
|
||||
->findBy(array('active' => $active));
|
||||
|
||||
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()
|
||||
));
|
||||
|
||||
->getForm();
|
||||
|
||||
$form->submit([
|
||||
'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->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
|
||||
// since 2016-11-14 the types are not alphabetically ordered, skipping
|
||||
/*$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;
|
||||
if (!isset($previous)) {
|
||||
$previous = $choice->label;
|
||||
} else {
|
||||
$this->assertTrue($previous < $choice->label);
|
||||
$previous = $choice->label;
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $active
|
||||
*
|
||||
* @return \Chill\ActivityBundle\Entity\ActivityType
|
||||
*/
|
||||
protected function getRandomType($active = true)
|
||||
{
|
||||
$types = $this->container->get('doctrine.orm.entity_manager')
|
||||
->getRepository('ChillActivityBundle:ActivityType')
|
||||
->findBy(['active' => $active]);
|
||||
|
||||
return $types[array_rand($types)];
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user