diff --git a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php index 3c609ea..ec59a80 100755 --- a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php +++ b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php @@ -5,7 +5,10 @@ use Carbon\Carbon; use Fenos\Notifynder\Builder\NotifynderBuilder; use Fenos\Notifynder\Categories\CategoryManager; +use Fenos\Notifynder\Exceptions\EntityNotIterableException; +use Fenos\Notifynder\Exceptions\IterableIsEmptyException; use Fenos\Notifynder\Models\NotificationCategory; +use Illuminate\Support\Collection; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -92,6 +95,8 @@ function it_allow_only_string_as_url() /** @test */ function it_add_the_expire_parameter_to_the_builder() { + date_default_timezone_set('UTC'); + $datetime = new Carbon; $this->expire($datetime)->shouldReturnAnInstanceOf(NotifynderBuilder::class); @@ -164,9 +169,36 @@ function it_create_a_builder_array_using_a_raw_closure() public function it_create_multi_notification_in_a_loop() { - $cloure = function(NotifynderBuilder $builder,$data,$key) + $closure = function(NotifynderBuilder $builder,$data,$key) + { + return $builder->to(1)->from(2)->url('notifynder.io')->category(1); + }; + } + + public function it_create_empty_array_loop_builder() + { + $closure = function(NotifynderBuilder $builder,$data,$key) + { + return $builder->to(1)->from(2)->url('notifynder.io')->category(1); + }; + $this->shouldThrow(IterableIsEmptyException::class)->during('loop', [[], $closure]); + } + + public function it_create_empty_collection_loop_builder() + { + $closure = function(NotifynderBuilder $builder,$data,$key) + { + return $builder->to(1)->from(2)->url('notifynder.io')->category(1); + }; + $this->shouldThrow(IterableIsEmptyException::class)->during('loop', [new Collection([]), $closure]); + } + + public function it_create_not_iterable_loop_builder() + { + $closure = function(NotifynderBuilder $builder,$data,$key) { return $builder->to(1)->from(2)->url('notifynder.io')->category(1); }; + $this->shouldThrow(EntityNotIterableException::class)->during('loop', ['hello world', $closure]); } } diff --git a/src/Notifynder/Builder/NotifynderBuilder.php b/src/Notifynder/Builder/NotifynderBuilder.php index 6e24961..8ffa213 100755 --- a/src/Notifynder/Builder/NotifynderBuilder.php +++ b/src/Notifynder/Builder/NotifynderBuilder.php @@ -3,10 +3,11 @@ use ArrayAccess; use Carbon\Carbon; use Fenos\Notifynder\Contracts\NotifynderCategory; +use Fenos\Notifynder\Exceptions\EntityNotIterableException; +use Fenos\Notifynder\Exceptions\IterableIsEmptyException; use Fenos\Notifynder\Exceptions\NotificationBuilderException; use Illuminate\Contracts\Config\Repository; use Illuminate\Database\Eloquent\Model; -use InvalidArgumentException; use Traversable; use Closure; @@ -175,26 +176,30 @@ public function raw(Closure $closure) * @param $dataToIterate * @param Closure $builder * @return $this - * @throws NotificationBuilderException + * @throws \Fenos\Notifynder\Exceptions\IterableIsEmptyException + * @throws \Fenos\Notifynder\Exceptions\EntityNotIterableException */ public function loop($dataToIterate, Closure $builder) { if ($this->isIterable($dataToIterate)) { - $notifications = []; + if(count($dataToIterate) > 0) { + $notifications = []; - $newBuilder = new self($this->notifynderCategory); + $newBuilder = new self($this->notifynderCategory); - foreach ($dataToIterate as $key => $data) { - $builder($newBuilder, $data, $key); - $notifications[] = $newBuilder->toArray(); - } + foreach ($dataToIterate as $key => $data) { + $builder($newBuilder, $data, $key); + $notifications[] = $newBuilder->toArray(); + } - $this->notifications = $notifications; - return $this; + $this->notifications = $notifications; + return $this; + } else { + throw new IterableIsEmptyException('The Iterable passed must contain at least one element'); + } + } else { + throw new EntityNotIterableException('The data passed must be itarable'); } - - $error = "The data passed must be itarable"; - throw new InvalidArgumentException($error); } /** diff --git a/src/Notifynder/Exceptions/EntityNotIterableException.php b/src/Notifynder/Exceptions/EntityNotIterableException.php new file mode 100644 index 0000000..40a5893 --- /dev/null +++ b/src/Notifynder/Exceptions/EntityNotIterableException.php @@ -0,0 +1,12 @@ +