DateRecurNonRecurringHelperUnitTest.php in Recurring Dates Field 3.1.x        
                          
                  
                        
  
  
  
  
File
  tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php
  
    View source  
  <?php
namespace Drupal\Tests\date_recur\Unit;
use Drupal\date_recur\DateRecurNonRecurringHelper;
use Drupal\Tests\UnitTestCase;
class DateRecurNonRecurringHelperUnitTest extends UnitTestCase {
  
  public function testOccurrence() {
    $helper = $this
      ->createHelper(new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('1:30am 14 April 2014'));
    $this
      ->assertCount(0, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('4:30am 14 April 2014'), new \DateTime('5am 14 April 2014'));
    $this
      ->assertCount(0, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'));
    $this
      ->assertCount(1, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
    $this
      ->assertCount(1, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('2:30am 14 April 2014'), new \DateTime('3:30am 14 April 2014'));
    $this
      ->assertCount(1, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('3am 14 April 2014'), new \DateTime('5am 14 April 2014'));
    $this
      ->assertCount(1, $occurrences);
    
    $occurrences = $helper
      ->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'), 0);
    $this
      ->assertCount(0, $occurrences);
  }
  
  public function testInvalidLimit() {
    $helper = $this
      ->createHelper(new \DateTime('2am 14 April 2014'), new \DateTime('4am 14 April 2014'));
    $this
      ->expectException(\InvalidArgumentException::class);
    $this
      ->expectExceptionMessage('Invalid count limit.');
    $helper
      ->getOccurrences(new \DateTime('1am 14 April 2014'), new \DateTime('3am 14 April 2014'), -1);
  }
  
  protected function createHelper(?\DateTimeInterface ...$args) {
    return DateRecurNonRecurringHelper::createInstance('', ...$args);
  }
}