/**
  * Tests saving a sfGuardUserSubredditMembership that identifies a User as
  * "blocked" for a particular Subreddit.  When this occurs, all existing
  * future EpisodeAssignments should be deleted because blocked users cannot
  * participate with the Subreddit.
  */
 public function testSavingBlockedUser()
 {
     // Establish fake Subreddit
     $subreddit = new Subreddit();
     $subreddit->setName(rand(0, 1000));
     $subreddit->setDomain(rand(0, 1000));
     $subreddit->save();
     // Establish User
     $user = new sfGuardUser();
     $user->setEmailAddress(rand(0, 100000));
     $user->setUsername(rand(0, 10000));
     $user->setIsValidated(1);
     $user->save();
     $user_id = $user->getIncremented();
     $this->assertNotEquals(0, $user_id);
     // Establish Episode for Subreddit
     $episode = new Episode();
     $episode->setSubreddit($subreddit);
     $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 34000));
     $episode->save();
     $author_type = AuthorTypeTable::getInstance()->findOneBy('type', 'squid');
     $deadline = new Deadline();
     $deadline->setSubreddit($subreddit);
     $deadline->setAuthorType($author_type);
     $deadline->setSeconds(0);
     $deadline->save();
     $episode_assignment = new EpisodeAssignment();
     $episode_assignment->setSfGuardUser($user);
     $episode_assignment->setEpisode($episode);
     $episode_assignment->setAuthorType($author_type);
     $episode_assignment->save();
     $number_of_episodes = EpisodeAssignmentTable::getInstance()->createQuery()->select('COUNT(EpisodeAssignment.id)')->leftJoin('Episode')->where('subreddit_id = ?', $subreddit->getIncremented())->andWhere('EpisodeAssignment.sf_guard_user_id = ?', $user_id)->andWhere('Episode.release_date > NOW()')->groupBy('EpisodeAssignment.id')->count();
     $this->assertEquals(1, $number_of_episodes);
     // Establish User Membership as Blocked
     $blocked = MembershipTable::getInstance()->findOneBy('type', 'blocked');
     $user_membership = new sfGuardUserSubredditMembership();
     $user_membership->setSubreddit($subreddit);
     $user_membership->setSfGuardUser($user);
     $user_membership->setMembership($blocked);
     // Save Membership
     $user_membership->save();
     // Asert that User has zero Episodes
     $number_of_episodes = EpisodeAssignmentTable::getInstance()->createQuery()->select('COUNT(EpisodeAssignment.id)')->leftJoin('Episode')->where('subreddit_id = ?', $subreddit->getIncremented())->andWhere('EpisodeAssignment.sf_guard_user_id = ?', $user_id)->andWhere('Episode.release_date > NOW()')->groupBy('EpisodeAssignment.id');
     $sql = $number_of_episodes->getSqlQuery();
     $number_of_episodes = $number_of_episodes->count();
     $this->assertTrue(0 == $number_of_episodes, $sql . "\n" . $subreddit->getIncremented() . "\n" . $user_id);
     // Remove User Membership
     $user_membership->delete();
     // Delete User
     $user->delete();
     // Delete Episode
     $episode->delete();
     // Delete Deadline
     $deadline->delete();
     // Delete Subreddit
     $subreddit->delete();
 }
 /**
  * We've combined the process for preventing saving with a specific call for
  * deletion to aid in handling errors and dealing with unsaved objects.
  * This tests whether the exception is thrown
  */
 public function testdeleteWithException()
 {
     $subreddit = new Subreddit();
     $subreddit->setName(rand(0, 10000));
     $subreddit->setDomain(rand(0, 10000));
     $subreddit->save();
     $user = new sfGuardUser();
     $user->setUsername(rand(0, 10000));
     $user->setEmailAddress(rand(0, 10000));
     $user->setIsValidated(1);
     $user->save();
     $first = AuthorTypeTable::getInstance()->findOneByType('squid');
     $episode = new Episode();
     $episode->setReleaseDate(date('Y-m-d H:i:s', time() + 100000));
     $episode->setSubreddit($subreddit);
     $episode->save();
     $deadline = new Deadline();
     $deadline->setSubreddit($subreddit);
     $deadline->setAuthorType($first);
     $deadline->setSeconds(0);
     $deadline->save();
     $assignment = new EpisodeAssignment();
     $assignment->setSfGuardUser($user);
     $assignment->setEpisode($episode);
     $assignment->setAuthorType($first);
     $assignment->save();
     // Delete with exception
     $exception_thrown = false;
     $exception_code = 987654321;
     try {
         $assignment->deleteWithException("Test exception", $exception_code);
     } catch (sfException $exception) {
         $this->assertEquals($exception_code, $exception->getCode());
         unset($exception);
         $exception_thrown = true;
     }
     $this->assertTrue($exception_thrown);
     // Delete the rest of the objects
     $episode->delete();
     $user->delete();
     $subreddit->delete();
 }
 /**
  * This tests whether the
  * EpisodeAssignmentTable::getFirstByUserAuthorTypeAndSubreddit() function
  * retrieves the correct EpisodeAssignment for a particular User in a
  * Subreddit based on the AuthorType used in the EpisodeAssignment.  Users
  * can sign up for one Episode per AuthorType in each Subreddit, which means
  * future Episodes (sicne we don't want the existence of past episode to
  * disqualify Users from ever signing up again).
  */
 public function testGetFirstByUserAuthorTypeAndSubreddit()
 {
     // Create two episode assignments: one for a past Episode, one for a future
     $subreddit = new Subreddit();
     $subreddit->setName(rand(0, 10000));
     $subreddit->setDomain(rand(0, 10000));
     $subreddit->save();
     $user = new sfGuardUser();
     $user->setUsername(rand(0, 10000));
     $user->setEmailAddress(rand(0, 10000));
     $user->setisValidated(1);
     $user->save();
     $first = AuthorTypeTable::getInstance()->findOneBy('type', 'squid');
     $understudy = AuthorTypeTable::getInstance()->findOneBy('type', 'shark');
     $this->assertTrue($first instanceof AuthorType);
     $this->assertNotEquals(null, $first->getIncremented());
     $this->assertTrue($understudy instanceof AuthorType);
     $this->assertNotEquals(null, $understudy->getIncremented());
     $deadline_one = new Deadline();
     $deadline_one->setSeconds(100);
     $deadline_one->setAuthorType($first);
     $deadline_one->setSubreddit($subreddit);
     $deadline_one->save();
     $deadline_two = new Deadline();
     $deadline_two->setSeconds(0);
     $deadline_two->setAuthorType($understudy);
     $deadline_two->setSubreddit($subreddit);
     $deadline_two->save();
     $episode_one = new Episode();
     $episode_one->setReleaseDate(date('Y-m-d H:i:s', time() + 200000));
     $episode_one->setSubreddit($subreddit);
     $episode_one->save();
     $episode_two = new Episode();
     $episode_two->setReleaseDate(date('Y-m-d H:i:s', time() + 100000));
     $episode_two->setSubreddit($subreddit);
     $episode_two->save();
     $episode_three = new Episode();
     $episode_three->setReleaseDate(date('Y-m-d H:i:s', time() + 300000));
     $episode_three->setSubreddit($subreddit);
     $episode_three->save();
     $assignment_one = new EpisodeAssignment();
     $assignment_one->setEpisode($episode_one);
     $assignment_one->setSfGuardUser($user);
     $assignment_one->setAuthorType($first);
     $assignment_one->save();
     $assignment_two = new EpisodeAssignment();
     $assignment_two->setEpisode($episode_two);
     $assignment_two->setSfGuardUser($user);
     $assignment_two->setAuthorType($understudy);
     $assignment_two->save();
     /* There should only be one Episode Assignment for future episodes per 
      * authortype per subreddit, so saving the third assignment should fail
      * --- this is covered more in EpisodeAssignmentTest.phop
      */
     $exception_thrown = false;
     $assignment_three = new EpisodeAssignment();
     $assignment_three->setEpisode($episode_three);
     $assignment_three->setSfGuardUser($user);
     $assignment_three->setAuthorType($first);
     try {
         $assignment_three->save();
     } catch (Exception $exception) {
         $exception_thrown = true;
         $this->assertEquals(102, $exception->getCode());
         unset($exception);
     }
     $this->assertTrue($exception_thrown);
     /* Since we can trust that only one assignment per authortype per user
      * per subreddit exists for future episodes (based on the failure to
      * save $assignment_three), we check now to ensure that the
      * getFirstByUserAuthorTypeAndSubreddit() function returns a valid
      * EpisodeAssignment.
      */
     $test_one = EpisodeAssignmentTable::getInstance()->getFirstByUserAuthorTypeAndSubreddit($assignment_one->getAuthorTypeId(), $assignment_one->getSfGuardUserId(), $assignment_one->getEpisode()->getSubredditId());
     $test_two = EpisodeAssignmentTable::getInstance()->getFirstByUserAuthorTypeAndSubreddit($assignment_two->getAuthorTypeId(), $assignment_two->getSfGuardUserId(), $assignment_two->getEpisode()->getSubredditId());
     $this->assertEquals($test_one->getIncremented(), $assignment_one->getIncremented());
     $this->assertEquals($test_two->getIncremented(), $assignment_two->getIncremented());
     $assignment_one->delete();
     $assignment_two->delete();
     $episode_one->delete();
     $episode_two->delete();
     $episode_three->delete();
     $deadline_one->delete();
     $deadline_two->delete();
     $user->delete();
     $subreddit->delete();
 }
<?php

include dirname(__FILE__) . '/../../bootstrap/functional.php';
/**
 * This is an example class of sfTestFunctional
 * It may require some attention to work with the default values (line 40).
 */
$browser = new sfBrowser();
$test_browser = new sfTestFunctional($browser);
$test_browser->setTester('json.response', 'sfTesterJsonResponse');
$test_browser->get('/episodeassignment')->with('request')->begin()->isParameter('module', 'episodeassignment')->isParameter('action', 'index')->end()->with('response')->begin()->isStatusCode(200)->end()->with('json.response')->begin()->isJson()->end();
/**
 * Test the creation
 */
$entity = new EpisodeAssignment();
$entity_array = $entity->exportTo('array');
$identifier = $entity->getTable()->getIdentifier();
/**
 * Please build a valid $entity_array here
 */
unset($entity_array[$identifier]);
//$entity_array['name'] = "pony";
//$entity_array['created_at'] = date('Y-m-d H:i:s');
//$entity_array['updated_at'] = date('Y-m-d H:i:s');
$test_browser->call('/episodeassignment', 'post', array('content' => json_encode($entity_array)))->with('request')->begin()->isParameter('module', 'episodeassignment')->isParameter('action', 'create')->end()->with('response')->begin()->isStatusCode(200)->end();
/**
 * If the new entity has been created
 */
$location = $browser->getResponse()->getHttpHeader('Location');
if ($location) {
    // Get ?