<?php

/**
 * 
 * Send notification mails about new possible rides to all people
 * in the "show interest" list, by invoking the ShowInterest service.
 * 
 * This script should run using a scheduling mechanism such as
 * cron; it is not required in case the application is configured to
 * send immediate notifications. In other words, it should only run if
 * the configuration key notify.immediate is not set 
 * 
 * @author itay
 * 
 */
require '../public/env.php';
require APP_PATH . '/Bootstrap.php';
$rideId = null;
if (isset($_GET['rideId'])) {
    $rideId = (int) $_GET['rideId'];
}
info('Show interest job started' . ($rideId !== null ? ' - only check ride ' . $rideId : ' - checking all rides'));
Service_ShowInterest::run($rideId);
info('Show interest job terminated');
 /**
  * 
  * @depends testFindPotentialRides
  * @depends testFindRidesToNotify
  */
 function testSearchForMatchingRides()
 {
     TestUtils::clearDatabase();
     $ride1 = TestUtils::createSimpleRide(1, 2, STATUS_LOOKING);
     $ride2 = TestUtils::createSimpleRide(1, 3, STATUS_LOOKING);
     $ride3 = TestUtils::createSimpleRide(1, 2, STATUS_OFFERED);
     $ride4 = TestUtils::createSimpleRide(3, 4, STATUS_OFFERED);
     $ride5 = TestUtils::createSimpleRide(1, 7, STATUS_OFFERED);
     $toNotify = Service_ShowInterest::findRidesToNotify(STATUS_LOOKING, 1);
     $potentials = Service_ShowInterest::findPotentialRides(STATUS_LOOKING, 1);
     $matching = Service_ShowInterest::searchForMatchingRides($potentials, $toNotify);
     $expectedResults = array($ride1 => array($ride3));
     $this->assertMatchingResults($matching, $expectedResults, "TestSearchForMatchingRides: Test 1");
     // Now, see what happens with don't-cares
     $ride6 = TestUtils::createSimpleRide(1, LOCATION_DONT_CARE, STATUS_LOOKING);
     $ride7 = TestUtils::createSimpleRide(LOCATION_DONT_CARE, 7, STATUS_LOOKING);
     // One for the sharing
     $ride8 = TestUtils::createSimpleRide(1, 2, STATUS_SHARING);
     $toNotify = Service_ShowInterest::findRidesToNotify(STATUS_LOOKING, 1);
     $potentials = Service_ShowInterest::findPotentialRides(STATUS_LOOKING, 1);
     $matching = Service_ShowInterest::searchForMatchingRides($potentials, $toNotify);
     // We should now have 3 results:
     //   ride1: ride3, ride8
     //   ride6: ride3, ride5, ride8
     //   ride7: ride5
     $expectedResults = array($ride1 => array($ride3, $ride8), $ride6 => array($ride3, $ride5, $ride8), $ride7 => array($ride5));
     $this->assertMatchingResults($matching, $expectedResults, "TestSearchForMatchingRides: Test 2");
     // Now test the other way
     $toNotify = Service_ShowInterest::findRidesToNotify(STATUS_OFFERED, 1);
     $potentials = Service_ShowInterest::findPotentialRides(STATUS_OFFERED, 1);
     $matching = Service_ShowInterest::searchForMatchingRides($potentials, $toNotify);
     // We should now have (wildcards in the rides to notify are not supported yet):
     //   ride3: ride1
     // TODO: Implement support for wildcards on the other way as well
     $expectedResults = array($ride3 => array($ride1, $ride8));
     $this->assertMatchingResults($matching, $expectedResults, "TestSearchForMatchingRides: Test 3");
     // And what happens when they share
     $toNotify = Service_ShowInterest::findRidesToNotify(STATUS_SHARING, 1);
     $potentials = Service_ShowInterest::findPotentialRides(STATUS_SHARING, 1);
     $matching = Service_ShowInterest::searchForMatchingRides($potentials, $toNotify);
     // We should now have the following results:
     //   ride8: ride1, ride3
     $expectedResults = array($ride8 => array($ride3, $ride1));
     $this->assertMatchingResults($matching, $expectedResults, "TestSearchForMatchingRides: Test 4");
 }