/**
  * 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();
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $quiet = (bool) $options['quiet'];
     $domain = $arguments['domain'];
     if (!$quiet) {
         echo "Creating deadlines...";
     }
     $subreddit = SubredditTable::getInstance()->findOneBy('domain', $domain);
     /* @var $subreddit Subreddit */
     if (!$subreddit) {
         return new sfException("Cannot find subreddit by the domain name {$domain}.", 404);
     }
     if (count($subreddit->getDeadlines())) {
         return new sfException("Subreddit has deadlines already.");
     }
     $authortype_one = AuthorTypeTable::getInstance()->findOneBy('type', 'first_place');
     $authortype_two = AuthorTypeTable::getInstance()->findOneBy('type', 'second_place');
     $authortype_three = AuthorTypeTable::getInstance()->findOneBy('type', 'third_place');
     $authortype_four = AuthorTypeTable::getInstance()->findOneBy('type', 'sudden_death');
     $deadlines = new Doctrine_Collection('Deadline');
     $deadlines[0] = new Deadline();
     $deadlines[0]->setAuthorType($authortype_one);
     $deadlines[0]->setSubreddit($subreddit);
     $deadlines[0]->setSeconds(259200);
     $deadlines[1] = new Deadline();
     $deadlines[1]->setAuthorType($authortype_two);
     $deadlines[1]->setSubreddit($subreddit);
     $deadlines[1]->setSeconds(172800);
     $deadlines[2] = new Deadline();
     $deadlines[2]->setAuthorType($authortype_three);
     $deadlines[2]->setSubreddit($subreddit);
     $deadlines[2]->setSeconds(86400);
     $deadlines[3] = new Deadline();
     $deadlines[3]->setAuthorType($authortype_four);
     $deadlines[3]->setSubreddit($subreddit);
     $deadlines[3]->setSeconds(0);
     $deadlines->save();
     if (!$quiet) {
         echo "\nFinished.\n";
     }
 }
 /**
  * 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();
 }
 /**
  * We set up the following situation:
  * We have three users, each of different AuthorTypes, who have 
  * EpisodeAssignments with the Episode in question.  The deadline for the 
  * first user has passed, but the deadline for the second has not.  The 
  * third user is in an AuthorType that should prevent creating an 
  * AuthorType for the Episode before the second user's deadline passes, even
  * though that User is an admin.
  * 
  * The second user (the one who is able to submit their episode) is a
  * moderator, but she should not be able to approve her own episode.
  */
 public function setUp()
 {
     ProjectConfiguration::registerAws();
     $original_filename = '1234567890abcde.mp3';
     $this->episode_filename = 'abcde0123456789.mp3';
     $this->unapproved_file_location = sfConfig::get('sf_data_dir') . '/temp/';
     if (!copy($this->unapproved_file_location . $original_filename, $this->unapproved_file_location . $this->episode_filename)) {
         echo "failed to copy {$this->unapproved_file_location}{$original_filename}...\n";
     }
     $this->aws = new AmazonS3();
     $this->subreddit = new Subreddit();
     $this->subreddit->setName(rand(0, 1000));
     $this->subreddit->setDomain(rand(0, 1000));
     $this->subreddit->save();
     $first = AuthorTypeTable::getInstance()->findOneByType('squid');
     $understudy = AuthorTypeTable::getInstance()->findOneByType('shark');
     $dark_horse = AuthorTypeTable::getInstance()->findOneByType('blue_whale');
     $this->first_deadline = new Deadline();
     $this->first_deadline->setAuthorType($first);
     $this->first_deadline->setSeconds(1000);
     $this->first_deadline->setSubreddit($this->subreddit);
     $this->first_deadline->save();
     $this->second_deadline = new Deadline();
     $this->second_deadline->setAuthorType($understudy);
     $this->second_deadline->setSeconds(500);
     $this->second_deadline->setSubreddit($this->subreddit);
     $this->second_deadline->save();
     $this->third_deadline = new Deadline();
     $this->third_deadline->setAuthorType($dark_horse);
     $this->third_deadline->setSeconds(100);
     $this->third_deadline->setSubreddit($this->subreddit);
     $this->third_deadline->save();
     $this->user = new sfGuardUser();
     $this->user->setEmailAddress(rand(0, 1000));
     $this->user->setUsername(rand(0, 1000));
     $this->user->setIsValidated(1);
     $this->user->save();
     $this->after_deadline_user = new sfGuardUser();
     $this->after_deadline_user->setEmailAddress(rand(0, 1000));
     $this->after_deadline_user->setUsername(rand(0, 1000));
     $this->after_deadline_user->setIsValidated(1);
     $this->after_deadline_user->save();
     $this->dark_horse_user = new sfGuardUser();
     $this->dark_horse_user->setEmailAddress(rand(0, 1000));
     $this->dark_horse_user->setUsername(rand(0, 1000));
     $this->dark_horse_user->setIsValidated(1);
     $this->dark_horse_user->save();
     $this->approver = new sfGuardUser();
     $this->approver->setEmailAddress(rand(0, 1000));
     $this->approver->setUsername(rand(0, 1000));
     $this->approver->setIsValidated(1);
     $this->approver->save();
     $moderator = MembershipTable::getInstance()->findOnebyType('moderator');
     $admin = MembershipTable::getInstance()->findOnebyType('admin');
     $member = MembershipTable::getInstance()->findOnebyType('user');
     $this->first_membership = new sfGuardUserSubredditMembership();
     $this->first_membership->setMembership($member);
     $this->first_membership->setSubreddit($this->subreddit);
     $this->first_membership->setSfGuardUser($this->after_deadline_user);
     $this->first_membership->save();
     $this->second_membership = new sfGuardUserSubredditMembership();
     $this->second_membership->setMembership($moderator);
     $this->second_membership->setSubreddit($this->subreddit);
     $this->second_membership->setSfGuardUser($this->user);
     $this->second_membership->save();
     $this->third_membership = new sfGuardUserSubredditMembership();
     $this->third_membership->setMembership($admin);
     $this->third_membership->setSubreddit($this->subreddit);
     $this->third_membership->setSfGuardUser($this->dark_horse_user);
     $this->third_membership->save();
     $this->fourth_membership = new sfGuardUserSubredditMembership();
     $this->fourth_membership->setMembership($moderator);
     $this->fourth_membership->setSubreddit($this->subreddit);
     $this->fourth_membership->setSfGuardUser($this->approver);
     $this->fourth_membership->save();
     $this->episode = new Episode();
     $this->episode->setReleaseDate(date('Y-m-d H:i:s', time() + 20000));
     $this->episode->setAudioFile($this->episode_filename);
     $this->episode->setNiceFilename('14. We Will Rock You.mp3');
     //$this->episode->setSfGuardUserId($this->user);
     $this->episode->setDescription('This is a test.');
     $this->episode->setTitle('Test Episode');
     $this->episode->setIsNsfw(false);
     $this->episode->setSubreddit($this->subreddit);
     $this->episode->save();
     $this->first_ep_assignment = new EpisodeAssignment();
     $this->first_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->first_ep_assignment->setAuthorType($first);
     $this->first_ep_assignment->setSfGuardUser($this->after_deadline_user);
     $this->first_ep_assignment->save();
     $this->second_ep_assignment = new EpisodeAssignment();
     $this->second_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->second_ep_assignment->setAuthorType($understudy);
     $this->second_ep_assignment->setSfGuardUser($this->user);
     $this->second_ep_assignment->save();
     $this->third_ep_assignment = new EpisodeAssignment();
     $this->third_ep_assignment->setEpisodeId($this->episode->getIncremented());
     $this->third_ep_assignment->setAuthorType($dark_horse);
     $this->third_ep_assignment->setSfGuardUser($this->dark_horse_user);
     try {
         $this->third_ep_assignment->save();
     } catch (sfException $exception) {
         unset($exception);
     }
     $this->episode->setReleaseDate(date('Y-m-d H:i:s', time() + 750));
     $this->episode->save();
 }
 /**
  * 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();
 }
 /**
  * applies the changes made to this object into database
  * this method is smart enough to know if any changes are made
  * and whether to use INSERT or UPDATE statement
  *
  * this method also saves the related components
  * 
  * Before creating a new EpisodeAssignment, this function verifies the
  * following rules:
  * 
  * 101) Blocked users should not be able to sign up for an Episode. Throws
  *      an sfException upon this failure.
  * 
  * 102) Only one sfGuardUser can sign up for one Episode with the same
  *      AuthorType for each Application period. Throws an sfException upon
  *      this failure.
  * 
  * 103) An sfGuardUser can only sign up for the same Episode with one
  *      AuthorType. Throws an sfException upon this failure.
  * 
  * 104) The EpisodeAssignment must be within the deadline for the
  *      AuthorType. Throws an sfException upon this failure.
  * 
  * 105) If the current AuthorType is flagged to be unavailable before the
  *      previous AuthorType has passed its Deadline by its Application
  *      record for the Subreddit and this previous Deadline has not passed,
  *      then the object will not be saved.  Throws an sfException upon this
  *      failure.
  * 
  * 106) The user attached to the EpisodeAssignment must be validated.
  * 
  * @see Doctrine_Record::save()
  * @throws sfException
  *
  * @param Doctrine_Connection $conn     optional connection parameter
  * @throws Exception                    if record is not valid and validation is active
  * @return void
  */
 public function save(Doctrine_Connection $conn = null)
 {
     /* If an EpisodeAssignment already exists we don't care about it. */
     if ($this->isNew()) {
         /* Blocked users should not be able to sign up for an Episode.
          */
         if (!$this->isAMember()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has not yet joined the Subreddit.", 101);
         }
         /* Blocked users should not be able to sign up for an Episode.
          */
         if ($this->hasBlockedUser()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has a blocked Membership within Subreddit.", 101);
         }
         /* If the Subreddit doesn't allow pending users to post, then we fail if the user is a pending member of the Subreddit. */
         if ($this->subredditBlocksPendingUsers()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has a pending Membership within Subreddit.", 101);
         }
         /* Only one sfGuardUser can sign up for one Episode with the same
          * AuthorType for each Application period.
          */
         if ($this->hasExistingUserAuthorTypeAssignment()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has already registered with AuthorType " . $this->getAuthorType() . " within Subreddit.", 102);
         }
         /* Only one sfGuardUser can sign up for one Episode with the same
          * AuthorType for each Application period.
          */
         if ($this->hasExistingAssignmentOnOtherEpisode()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has already registered with AuthorType " . $this->getAuthorType() . " within Subreddit.", 103);
         }
         /* The EpisodeAssignment must be within the deadline for the
          * AuthorType.
          */
         if (!$this->isBeforeDeadlineForAuthorType()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because the deadline has already passed for " . "AuthorType " . $this->getAuthorType() . " within " . "Subreddit.", 104);
         }
         /* Even if the deadline has not yet passed, we may only sign up an 
          * AuthorType if the AuthorType is allowed to register before the 
          * previous AuthorType (meaning the AuthorType with the next-longest
          * deadline).  The next few checks are for this purpose.
          */
         $deadline_seconds = Doctrine::getTable('Deadline')->getSecondsByAuthorAndSubreddit($this->getAuthorTypeId(), $this->getEpisode()->getSubredditId());
         if ($deadline_seconds === false) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because the deadline doesn't exist for AuthorType " . $this->getAuthorType() . " within Subreddit.", 107);
         }
         /* Check to see if there *is* a previous AuthorType by Deadline
          * length.
          */
         if ($previous_author_type_id = DeadlineTable::getInstance()->getFirstAuthorTypeIdBySubredditWhereDeadlineIsGreaterThan($deadline_seconds, $this->getEpisode()->getSubredditId())) {
             /* If a previous AuthorType exists, we need to see if the
              * current AuthorType is restricted until that previous 
              * uthorType is expired.  If it *is* restricted, then we need to
              * see if the previous AuthorType has yet expired.  If the
              * previous AuthorType is still beyond its Deadline for the
              * Episode (and has not expired) then we cannot allow the
              * current EpisodeAssignment to be saved.
              */
             if (DeadlineTable::getInstance()->getIfDeadlineRestrictedByAuthorTypeAndSubreddit($this->getAuthorTypeId(), $this->getEpisode()->getSubredditId()) && $this->isBeforeDeadlineForAuthorType($previous_author_type_id)) {
                 $prev_author_type = AuthorTypeTable::getInstance()->find($previous_author_type_id);
                 $this->deleteWithException("Cannot create " . "EpisodeAssignment because the deadline has " . "not yet passed for the previous AuthorType " . $prev_author_type . " within Subreddit.", 105);
             }
         }
         if (!$this->hasVerifiedUser()) {
             $this->deleteWithException("Cannot create EpisodeAssignment " . "because User " . $this->getSfGuardUser()->getUsername() . " has not validated their Reddit username yet.", 106);
         }
         $isNew = true;
     } else {
         $isNew = false;
     }
     /* If the obejct is not new or has passed all rules for saving, we pass
      * it on to the parent save function.
      */
     $this->setIdHashIfNotSet();
     parent::save($conn);
     if ($isNew) {
         /* Now that we have a new EpisodeAssignment that has passed all
          * rules, let's set it to be valid if it belongs to the first
          * Deadline and send an email to the user about it.
          */
         $subreddit = SubredditTable::getInstance()->find($this->getEpisode()->getSubredditId());
         $deadline = DeadlineTable::getInstance()->find($subreddit->getFirstDeadlineId());
         if ($deadline) {
             if ($this->getAuthorTypeId() == $deadline->getAuthorTypeId()) {
                 $episode = $this->getEpisode();
                 $episode->setEpisodeAssignmentId($this->getIncremented());
                 $episode->save();
                 $release_date = strtotime($episode->getReleaseDate());
                 $seconds = $deadline->getSeconds();
                 $deadline = $release_date - $seconds;
                 $subreddit->sendEmailAboutNewAssignment($this->getSfGuardUserId(), $this->getEpisodeId(), $deadline);
             }
         }
     }
 }
 protected function execute($arguments = array(), $options = array())
 {
     // initialize the database connection
     $databaseManager = new sfDatabaseManager($this->configuration);
     $connection = $databaseManager->getDatabase($options['connection'])->getConnection();
     $quiet = (bool) $options['quiet'];
     // add your code here
     $num_subreddits = 1000;
     $num_deadlines = 5;
     $num_users = 3000;
     $create_episodes_attempt = 3;
     echo " Creating AuthorTypes...";
     $authortypes = new Doctrine_Collection('AuthorType');
     for ($i = 0; $i < $num_deadlines; $i++) {
         $exists = AuthorTypeTable::getInstance()->findOneBy('type', $i);
         if ($exists) {
             $authortypes[$i] = $exists;
         } else {
             $authortypes[$i] = new AuthorType();
         }
         $authortypes[$i]->setType($i);
         $authortypes[$i]->setDescription($i);
         if (!$quiet) {
             echo $authortypes[$i]->getType() . ' ';
         }
     }
     $authortypes->save();
     /*echo "\n Creating Subreddits...";
             $subreddits = new Doctrine_Collection('Subreddit');
             $x = 0;
             for ($i = 0; $i < $num_subreddits; $i++) {
                 $exists = SubredditTable::getInstance()->findOneBy('name', $i);
                 if ($exists) {
                     if (!$quiet)
                         echo $exists->getName() . ' ';
                     continue;
                 }
                 $subreddits[$x] = new Subreddit();
                 $subreddits[$x]->setName($i);
                 $subreddits[$x]->setDomain($i);
                 $subreddits[$x]->setEpisodeScheduleCronFormatted('0 0 * * *');
                 $subreddits[$x]->setCreateNewEpisodesCronFormatted('0 0 1 * *');
                 if (!$quiet)
                     echo $subreddits[$x]->getName() . ' ';
                 $x++;
             }
             if ($x)
                 $subreddits->save();
     
             echo "\n Creating Deadlines...";
             $deadlines = new Doctrine_Collection('Deadline');
             $x = 0;
             for ($i = 0; $i < $num_subreddits; $i++) {
                 $subreddit = SubredditTable::getInstance()->findOneBy('name', $i);
                 $deadlines = $subreddit->getDeadlines();
                 if (!$quiet)
                     echo $subreddit->getName() . ' ';
                 if (count($deadlines) == 0) {
                     for ($j = 0; $j < $num_deadlines; $j++) {
                         $deadlines[$x] = new Deadline();
                         $deadlines[$x]->setAuthorType($authortypes[$j]);
                         $deadlines[$x]->setSubreddit($subreddit);
                         $deadlines[$x]->setSeconds((24 / $num_deadlines) * $j);
                         $x++;
                     }
                 }
             }
             if ($x)
                 $deadlines->save();
             unset($deadlines);
     
             echo "\n Creating fake users for signup...";
     
             $users = new Doctrine_collection("sfGuardUser");
             $x = 0;
             for ($i = 0; $i < $num_users; $i++) {
                 $exists = sfGuardUserTable::getInstance()->findOneBy('username', $i);
                 if ($exists) {
                     $users[$x] = $exists;
                     continue;
                 } else {
                     $users[$x] = new sfGuardUser();
                 }
                 $users[$x]->setUsername($i);
                 $users[$x]->setEmailAddress($i);
                 $users[$x]->setIsValidated(1);
                 if (!$quiet)
                     echo $users[$x]->getUsername() . ' ';
                 $x++;
             }
             $users->save();
             
             echo "Joining to subreddits...";
             $memberships = new Doctrine_Collection('sfGuardUserSubredditMembership');
             $x = 0;
             $member = MembershipTable::getInstance()->findOneBy('type', 'member');
             foreach($users as $user)
             {
                 echo $user->getUsername() .' ';
                 foreach($subreddits as $subreddit)
                 {
                     $exists = sfGuardUserSubredditMembershipTable::getInstance()->getFirstByUserSubredditAndMemberships($user->getIncremented(), $subreddit->getIncremented(), array('member'));
                     if ($exists)
                     {
                         continue;
                     }
                     $memberships[$x] = new sfGuardUserSubredditMembership();
                     $memberships[$x]->setSfGuardUserId($user->getIncremented());
                     $memberships[$x]->setSubredditId($subreddit->getIncremented());
                     $memberships[$x]->setMembershipId($member->getIncremented());
                 }
             }
             if ($x)
                 $memberships->save();
             
             echo "\n Analyizing which need Episodes...";
             $subreddits = Doctrine::getTable('Subreddit')
                     ->getSubredditsNeedingEpisodeGeneration();
             foreach ($subreddits as $subreddit) {
                 if (!$quiet)
                     echo $subreddit->getName() . ' ';
                 $episodes = $subreddit->collectGeneratedEpisodes();
                 if (!$quiet)
                     echo ' Generated...';
                 $episodes->save();
                 if (!$quiet)
                     echo " Saved!\n";
             }*/
     echo "\n Randomly having users attempt to sign up for Episodes...";
     $assignments = new Doctrine_Collection('EpisodeAssignment');
     $x = 0;
     for ($i = 0; $i < $num_users; $i++) {
         $user = sfGuardUserTable::getInstance()->findOneBy('username', $i);
         echo $user->getUsername();
         for ($j = 0; $j < $create_episodes_attempt; $j++) {
             $episode = EpisodeTable::getInstance()->getRandomUnassignedFutureEpisode();
             if (!$episode) {
                 continue;
             }
             $assignments[$x] = new EpisodeAssignment();
             $assignments[$x]->setEpisodeId($episode->getIncremented());
             $assignments[$x]->setSfGuardUserId($user->getIncremented());
             $assignments[$x]->setAuthorType($authortypes[$j]);
             try {
                 $assignments[$x]->save();
             } catch (Exception $e) {
                 unset($e);
             }
         }
     }
 }
 /**
  * Tests for success at creating the object.
  */
 public function testCreate()
 {
     $t = AuthorTypeTable::getInstance();
     $this->assertTrue($t instanceof Doctrine_Table);
 }