From b49c6d387c2354885f5df975be538d2a6d08a980 Mon Sep 17 00:00:00 2001
From: dpi <danph@81431.no-reply.drupal.org>
Date: Sat, 29 Jan 2022 14:10:32 +0000
Subject: [PATCH] Issue #3153976 by AndyF: Can't iterate
 DateRecurNonRecurringHelper

---
 src/DateRecurNonRecurringHelper.php           |  2 +-
 .../DateRecurNonRecurringHelperUnitTest.php   | 26 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/src/DateRecurNonRecurringHelper.php b/src/DateRecurNonRecurringHelper.php
index 53e3c1c..d614e47 100644
--- a/src/DateRecurNonRecurringHelper.php
+++ b/src/DateRecurNonRecurringHelper.php
@@ -126,7 +126,7 @@ class DateRecurNonRecurringHelper implements DateRecurHelperInterface {
    * {@inheritdoc}
    */
   public function valid(): bool {
-    return TRUE;
+    return !is_null(key($this->occurrences));
   }
 
   /**
diff --git a/tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php b/tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php
index 30b48d1..26f8995 100644
--- a/tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php
+++ b/tests/src/Unit/DateRecurNonRecurringHelperUnitTest.php
@@ -94,6 +94,32 @@ class DateRecurNonRecurringHelperUnitTest extends UnitTestCase {
     );
   }
 
+  /**
+   * Test the helper works as an iterator.
+   */
+  public function testIteration() {
+    $format = 'ga j F Y';
+    $start = '2am 14 April 2014';
+    $end = '4am 14 April 2014';
+    $helper = $this->createHelper(
+      new \DateTime($start),
+      new \DateTime($end)
+    );
+    $occurrences = [];
+
+    // Iterate the occurences.
+    foreach ($helper as $date_range) {
+      $occurrences[] = $date_range;
+    }
+    // There should be only one result.
+    /** @var \Drupal\date_recur\DateRange $occurrence */
+    $occurrence = reset($occurrences);
+
+    $this->assertCount(1, $occurrences);
+    $this->assertSame($occurrence->getStart()->format($format), $start);
+    $this->assertSame($occurrence->getEnd()->format($format), $end);
+  }
+
   /**
    * Creates a new helper.
    *
-- 
GitLab