<?php

define('APP_ROOT_DIR', __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR);
require_once (defined('COMPOSER_ROOT_DIR') ? COMPOSER_ROOT_DIR : APP_ROOT_DIR) . '/vendor/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoload.php';
require_once APP_ROOT_DIR . '/core/php/autoloadCLI.php';
/**
 *
 * @package com.meetyournextmp
 * @license Closed Source
 * @copyright (c) 2013-2015, JMB Technology Limited, http://jmbtechnology.co.uk/
 * @author James Baster <*****@*****.**>
 */
$slug = intval($argv[1]);
if ($slug < 1) {
    die("PASS SLUG");
}
$siteRepo = new \repositories\SiteRepository();
$app['currentSite'] = $siteRepo->loadById($CONFIG->singleSiteID);
$humanRepo = new \com\meetyournextmp\repositories\HumanRepository();
$human = $humanRepo->loadBySlug($app['currentSite'], $slug);
if ($human) {
    $humanRepo->delete($human);
}
 function testEventInNoAreaHumanInCentre()
 {
     $this->addCountriesToTestDB();
     TimeSource::mock(2014, 5, 1, 7, 0, 0);
     $user = new UserAccountModel();
     $user->setEmail("*****@*****.**");
     $user->setUsername("test");
     $user->setPassword("password");
     $userWatching = new UserAccountModel();
     $userWatching->setEmail("*****@*****.**");
     $userWatching->setUsername("test1");
     $userWatching->setPassword("password");
     $userRepo = new UserAccountRepository();
     $userRepo->create($user);
     $userRepo->create($userWatching);
     $site = new SiteModel();
     $site->setTitle("Test");
     $site->setSlug("test");
     $siteRepo = new SiteRepository();
     $siteRepo->create($site, $user, array(), $this->getSiteQuotaUsedForTesting());
     $countryRepo = new \repositories\CountryRepository();
     $gb = $countryRepo->loadByTwoCharCode("GB");
     $areaParent = new \models\AreaModel();
     $areaParent->setTitle("Parent");
     $areaChild = new \models\AreaModel();
     $areaChild->setTitle("Child");
     $areaOtherChild = new \models\AreaModel();
     $areaOtherChild->setTitle("Other Child");
     $areaRepo = new \repositories\AreaRepository();
     $areaRepo->create($areaParent, null, $site, $gb);
     $areaRepo->create($areaChild, $areaParent, $site, $gb);
     $areaRepo->create($areaOtherChild, $areaParent, $site, $gb);
     $areaRepo->buildCacheAreaHasParent($areaParent);
     $areaRepo->buildCacheAreaHasParent($areaChild);
     $areaRepo->buildCacheAreaHasParent($areaOtherChild);
     $event = new EventModel();
     $event->setSummary("test");
     $event->setDescription("test test");
     $event->setStartAt($this->mktime(2014, 5, 10, 19, 0, 0, 'Europe/London'));
     $event->setEndAt($this->mktime(2014, 5, 10, 21, 0, 0, 'Europe/London'));
     $event->setUrl("http://www.info.com");
     $event->setTicketUrl("http://www.tickets.com");
     $eventRepository = new EventRepository();
     $eventRepository->create($event, $site, $user);
     $human = new \com\meetyournextmp\models\HumanModel();
     $human->setTitle("Bob");
     $humanRepo = new \com\meetyournextmp\repositories\HumanRepository();
     $humanRepo->create($human, $site, null);
     $humanRepo->addHumanToArea($human, $areaChild, null);
     $humanRepo->addHumanToEvent($human, $event, null);
     $userWatchesAreaRepo = new \repositories\UserWatchesAreaRepository();
     //////////////////////////////// TEST ALL EVENTS
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST OTHER CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaOtherChild);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaOtherChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(0, count($histories));
     //////////////////////////////// TEST CHILD
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// TEST PARENT
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setArea($areaChild);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     $hrb = new \repositories\builders\HistoryRepositoryBuilder();
     $hrb->getHistoryRepositoryBuilderConfig()->setArea($areaChild);
     $hrb->setIncludeAreaHistory(false);
     $hrb->setIncludeEventHistory(true);
     $histories = $hrb->fetchAll();
     $this->assertEquals(1, count($histories));
     //////////////////////////////// USER WATCHES NOTHING
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(0, count($events));
     //////////////////////////////// USER WATCHES PARENT
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaParent);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
     //////////////////////////////// USER WATCHES CHILD
     $userWatchesAreaRepo->stopUserWatchingArea($userWatching, $areaParent);
     $userWatchesAreaRepo->startUserWatchingArea($userWatching, $areaChild);
     $erb = new \repositories\builders\EventRepositoryBuilder();
     $erb->setUserAccount($userWatching, false, true, true, true);
     $events = $erb->fetchAll();
     $this->assertEquals(1, count($events));
 }