From fee5b00e35f811ee8698049fd79dc866f7307172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Thu, 8 Oct 2015 22:36:08 +0200 Subject: [PATCH] use random activityType, activityReason, scope in test --- Tests/Controller/ActivityControllerTest.php | 66 ++++++++++++++++++++- 1 file changed, 63 insertions(+), 3 deletions(-) diff --git a/Tests/Controller/ActivityControllerTest.php b/Tests/Controller/ActivityControllerTest.php index f3587f443..517366ceb 100644 --- a/Tests/Controller/ActivityControllerTest.php +++ b/Tests/Controller/ActivityControllerTest.php @@ -3,6 +3,7 @@ namespace Chill\ActivityBundle\Tests\Controller; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\Security\Core\Role\Role; class ActivityControllerTest extends WebTestCase { @@ -41,9 +42,9 @@ class ActivityControllerTest extends WebTestCase 'minute' => '30' ), 'remark' => 'blabla', - 'scope' => 1, - 'reason' => 2, - 'type' => 3 + 'scope' => $this->getRandomScope('center a_social', 'Center A')->getId(), + 'reason' => $this->getRandomActivityReason()->getId(), + 'type' => $this->getRandomActivityType()->getId() ) )); @@ -115,4 +116,63 @@ class ActivityControllerTest extends WebTestCase return $person; } + + /** + * + * @param string $username + * @param string $centerName + * @return \Chill\MainBundle\Entity\Scope + */ + private function getRandomScope($username, $centerName) + { + $user = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('ChillMainBundle:User') + ->findOneByUsername($username); + + if ($user === NULL) { + throw new \RuntimeException("The user with username $username " + . "does not exists in database. Did you add fixtures ?"); + } + + $center = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('ChillMainBundle:Center') + ->findOneByName($centerName); + + $reachableScopes = static::$kernel->getContainer() + ->get('chill.main.security.authorization.helper') + ->getReachableScopes($user, new Role('CHILL_ACTIVITY_UPDATE'), + $center); + + return $reachableScopes[array_rand($reachableScopes)]; + } + + /** + * + * @return \Chill\ActivityBundle\Entity\ActivityReason + */ + private function getRandomActivityReason() + { + $reasons = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('ChillActivityBundle:ActivityReason') + ->findAll(); + + return $reasons[array_rand($reasons)]; + } + + /** + * + * @return \Chill\ActivityBundle\Entity\ActivityType + */ + private function getRandomActivityType() + { + $types = static::$kernel->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('ChillActivityBundle:ActivityType') + ->findAll(); + + return $types[array_rand($types)]; + } }