/**
  * Tests for success at creating the object.
  */
 public function testCreate()
 {
     $test_seconds = 4321;
     $t = new Deadline();
     $t->setSeconds($test_seconds);
     $this->assertTrue($t instanceof Deadline);
     $this->assertEquals($t->__toString(), $test_seconds);
 }
 /**
  * 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();
 }
<?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('/subredditdeadline')->with('request')->begin()->isParameter('module', 'subredditdeadline')->isParameter('action', 'index')->end()->with('response')->begin()->isStatusCode(200)->end()->with('json.response')->begin()->isJson()->end();
/**
 * Test the creation
 */
$entity = new Deadline();
$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('/subredditdeadline', 'post', array('content' => json_encode($entity_array)))->with('request')->begin()->isParameter('module', 'subredditdeadline')->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 ?
 /**
  * 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();
 }
 public function executeUpdatedeadline(sfWebRequest $request)
 {
     $this->forward404Unless($this->getUser()->isAuthenticated());
     $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
     $auth_key = $this->getUser()->getApiAuthKey();
     $deadline_data = Api::getInstance()->setUser($auth_key)->get('subredditdeadline/' . $request->getParameter('id'), true);
     $deadline = ApiDoctrine::createObject('Deadline', $deadline_data['body']);
     if (!$deadline || !$deadline->getId()) {
         $deadline = new Deadline();
         $deadline->setSubredditId($request->getParameter('subreddit_id'));
     }
     $this->subreddit_id = $deadline->getSubredditId();
     $subreddit_data = Api::getInstance()->setUser($auth_key)->get('subreddit/' . $this->subreddit_id, true);
     $this->subreddit = ApiDoctrine::createQuickObject($subreddit_data['body']);
     $my_membership_data = Api::getInstance()->setUser($auth_key)->get('subredditmembership?sf_guard_user_id=' . $this->getUser()->getApiUserId() . '&subreddit_id=' . $this->subreddit_id, true);
     $my_membership = is_array($my_membership_data['body']) && array_key_exists(0, $my_membership_data['body']) ? ApiDoctrine::createQuickObject($my_membership_data['body'][0]) : null;
     $this->forward404Unless($my_membership instanceof ApiDoctrineQuick && in_array($my_membership->getMembership()->getType(), array('admin')));
     $this->form = new DeadlineForm($deadline);
     unset($this->form['subreddit_id']);
     $this->processDeadlineForm($request, $this->form);
     if ($deadline && $deadline->getIncremented()) {
         $this->setTemplate('edit_deadline');
     } else {
         $this->setTemplate('add_deadline');
     }
 }