public function testAggregateValue()
 {
     $store = new SqlLiteDBStore($this->pdo, 'availability_event');
     $calendar = new Calendar(array($this->u1, $this->u2, $this->u3), $store);
     $calendar->addEvents(array($this->e1, $this->e2), Event::BAT_HOURLY);
     // Interval: 11 days
     // Duration: 1 day
     // Value: 5 * 10 + 3 * 1
     $valuator = new IntervalValuator(new \DateTime('2016-01-01 00:00'), new \DateTime('2016-01-11 23:59'), $this->u1, $store, new \DateInterval('P1D'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 53);
     // Interval: 2 days
     // Duration: 1 day
     // Value: 3 * 2
     $valuator = new IntervalValuator(new \DateTime('2016-01-11 00:00'), new \DateTime('2016-01-12 23:59'), $this->u1, $store, new \DateInterval('P1D'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 6);
     // Interval: 6 day
     // Duration: 1 day
     // Value: 5 * 5 + 3 * 1
     $valuator = new IntervalValuator(new \DateTime('2016-01-06 00:00'), new \DateTime('2016-01-11 23:59'), $this->u1, $store, new \DateInterval('P1D'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 28);
     // Interval: 2 hours
     // Duration: 15 minutes
     // Value: 5 * 8
     $valuator = new IntervalValuator(new \DateTime('2016-01-01 11:00'), new \DateTime('2016-01-01 12:59'), $this->u1, $store, new \DateInterval('PT15M'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 40);
     // Interval: 2 hours
     // Duration: 15 minutes
     // Value: 3 * 8
     $valuator = new IntervalValuator(new \DateTime('2016-01-11 11:00'), new \DateTime('2016-01-11 12:59'), $this->u1, $store, new \DateInterval('PT15M'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 24);
     // Interval: 2 hours
     // Duration: 36 minutes
     // Value: 3 * 3.333
     $valuator = new IntervalValuator(new \DateTime('2016-01-11 11:00'), new \DateTime('2016-01-11 12:59'), $this->u1, $store, new \DateInterval('PT36M'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 10);
     // Interval: 15 hours
     // Duration: 3 hours
     // Value: 5 * 1 + 3 * 4
     $valuator = new IntervalValuator(new \DateTime('2016-01-10 21:00'), new \DateTime('2016-01-11 11:59'), $this->u1, $store, new \DateInterval('PT3H'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 17);
     // Interval: 15 hours
     // Duration: 3 hours
     // Value: 5 * 2/3 + 3 * 4.333
     $valuator = new IntervalValuator(new \DateTime('2016-01-10 22:00'), new \DateTime('2016-01-11 12:59'), $this->u1, $store, new \DateInterval('PT3H'));
     $value = $valuator->determineValue();
     $this->assertEquals($value, 16.33);
 }
Exemple #2
0
 /**
  * {@inheritdoc}
  */
 public function determineValue()
 {
     $value = 0;
     // Instantiate a calendar
     $calendar = new Calendar(array($this->unit), $this->store);
     $events = $calendar->getEvents($this->start_date, $this->end_date);
     foreach ($events as $unit => $unit_events) {
         if ($unit == $this->unit->getUnitId()) {
             foreach ($unit_events as $event) {
                 $percentage = EventInterval::divide($event->getStartDate(), $event->getEndDate(), $this->duration_unit);
                 $value = $value + $event->getValue() * $percentage;
             }
         }
     }
     return round($value, 2);
 }
Exemple #3
0
 public function testCalendarLastDayOfMonth()
 {
     $u1 = new Unit(1, 10, array());
     $u2 = new Unit(2, 20, array());
     $units = array($u1, $u2);
     $sd1 = new \DateTime('2016-03-31 12:12');
     $sd2 = new \DateTime('2016-03-31 13:12');
     $e1 = new Event($sd1, $sd2, $u1, 11);
     $e2 = new Event($sd1, $sd2, $u2, 22);
     $store = new SqlLiteDBStore($this->pdo, 'availability_event', SqlDBStore::BAT_STATE);
     $calendar = new Calendar($units, $store);
     $calendar->addEvents(array($e1, $e2), Event::BAT_HOURLY);
     $this->assertEquals($calendar->getStates($sd1, $sd2), array(1 => array(11 => 11), 2 => array(22 => 22)));
 }
Exemple #4
0
 public function testFullHourEvent()
 {
     $u1 = new Unit(1, 10, array());
     $sd = new \DateTime('2016-01-18 12:00');
     $ed = new \DateTime('2016-01-18 15:59');
     $e = new Event($sd, $ed, $u1, 5);
     $store = new SqlLiteDBStore($this->pdo, 'availability_event', SqlDBStore::BAT_STATE);
     $calendar = new Calendar(array($u1), $store);
     // Add the events.
     $calendar->addEvents(array($e), Event::BAT_HOURLY);
     $events = $calendar->getEvents(new \DateTime('2016-01-18 00:00'), new \DateTime('2016-01-19 00:00'));
 }
Exemple #5
0
 /**
  * Test to Constraints Base functionality.
  */
 public function testConstraintsBaseFunctions()
 {
     $u1 = new Unit(1, 10, array());
     $u2 = new Unit(2, 10, array());
     $units = array($u1, $u2);
     $sd = new \DateTime('2016-01-01 15:10');
     $ed = new \DateTime('2016-06-30 18:00');
     $sd1 = new \DateTime('2016-01-07 02:12');
     $ed1 = new \DateTime('2016-01-13 13:12');
     // Create some events for units 1,2
     $e1u1 = new Event($sd1, $ed1, $u1, 11);
     $e1u2 = new Event($sd1, $ed1, $u2, 13);
     $store = new SqlLiteDBStore($this->pdo, 'availability_event', SqlDBStore::BAT_STATE);
     $calendar = new Calendar($units, $store);
     // Add the events.
     $calendar->addEvents(array($e1u1, $e1u2), Event::BAT_HOURLY);
     $response = $calendar->getMatchingUnits($sd, $ed, array(10, 11, 13), array());
     // Add the constraint with Start and End dates.
     $constraint = new MinMaxDaysConstraint(array($u1, $u2), 3, 0, $sd, $ed);
     $constraint->setStartDate($sd1);
     $this->assertEquals($constraint->getStartDate(), $sd1);
     $constraint->setEndDate($ed1);
     $this->assertEquals($constraint->getEndDate(), $ed1);
     $constraint->getAffectedUnits();
 }
Exemple #6
0
 public function testSplitHour()
 {
     $u1 = new Unit(1, 0, array());
     $units = array($u1);
     $store = new SqlLiteDBStore($this->pdo, 'availability_event', SqlDBStore::BAT_STATE);
     $calendar = new Calendar($units, $store);
     $sd1 = new \DateTime('2016-08-01 10:00');
     $ed1 = new \DateTime('2016-08-01 10:59');
     $e1s11 = new Event($sd1, $ed1, $u1, 11);
     $calendar->addEvents(array($e1s11), Event::BAT_HOURLY);
     $itemized = $calendar->getEventsItemized($sd1, $ed1);
     $this->assertEquals($itemized['1']['bat_day']['2016']['8']['d1'], '-1');
     $this->assertEquals($itemized['1']['bat_hour']['2016']['8']['d1']['h10'], '11');
     $sd2 = new \DateTime('2016-08-01 10:00');
     $ed2 = new \DateTime('2016-08-01 10:14');
     $e2s12 = new Event($sd2, $ed2, $u1, 12);
     $calendar->addEvents(array($e2s12), Event::BAT_HOURLY);
     $itemized = $calendar->getEventsItemized($sd1, $ed1);
     $this->assertEquals($itemized['1'][Event::BAT_DAY]['2016']['8']['d1'], '-1');
     $this->assertEquals($itemized['1'][Event::BAT_HOUR]['2016']['8']['d1']['h10'], '-1');
     $this->assertEquals($itemized['1'][Event::BAT_MINUTE]['2016']['8']['d1']['h10']['m10'], '12');
     $this->assertEquals($itemized['1'][Event::BAT_MINUTE]['2016']['8']['d1']['h10']['m14'], '12');
     $this->assertEquals($itemized['1'][Event::BAT_MINUTE]['2016']['8']['d1']['h10']['m15'], '11');
     $this->assertEquals($itemized['1'][Event::BAT_MINUTE]['2016']['8']['d1']['h10']['m59'], '11');
 }