From aecd99ef41f05d07d03179d463779763d1f312b1 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 3 May 2016 20:09:17 +0200 Subject: [PATCH 1/3] Issue #118 throw exception in the `loop()` method if the iterable is empty. --- src/Notifynder/Builder/NotifynderBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Notifynder/Builder/NotifynderBuilder.php b/src/Notifynder/Builder/NotifynderBuilder.php index 6e24961..e4a71fe 100755 --- a/src/Notifynder/Builder/NotifynderBuilder.php +++ b/src/Notifynder/Builder/NotifynderBuilder.php @@ -254,7 +254,7 @@ public function refresh() */ protected function isIterable($var) { - return (is_array($var) || $var instanceof Traversable); + return (is_array($var) || $var instanceof Traversable) && count($var) > 0; } /** From 68e7b701d534984cac06cba0627e9acafdc043ac Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 3 May 2016 20:22:43 +0200 Subject: [PATCH 2/3] add phpspec tests for empty array & collection --- .../Builder/NotifynderBuilderSpec.php | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php index 3c609ea..08fdf69 100755 --- a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php +++ b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php @@ -6,6 +6,7 @@ use Fenos\Notifynder\Builder\NotifynderBuilder; use Fenos\Notifynder\Categories\CategoryManager; use Fenos\Notifynder\Models\NotificationCategory; +use Illuminate\Support\Collection; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -92,6 +93,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 +167,27 @@ 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('InvalidArgumentException')->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('InvalidArgumentException')->during('loop', [new Collection([]), $closure]); } } From 8e2bf8e7ed8c160060319d360c9104127c2c07f8 Mon Sep 17 00:00:00 2001 From: Tom Witkowski Date: Tue, 3 May 2016 22:01:38 +0200 Subject: [PATCH 3/3] add two exceptions for the empty or not iterable loop data --- .../Builder/NotifynderBuilderSpec.php | 15 +++++++-- src/Notifynder/Builder/NotifynderBuilder.php | 33 +++++++++++-------- .../Exceptions/EntityNotIterableException.php | 12 +++++++ .../Exceptions/IterableIsEmptyException.php | 12 +++++++ 4 files changed, 56 insertions(+), 16 deletions(-) create mode 100644 src/Notifynder/Exceptions/EntityNotIterableException.php create mode 100644 src/Notifynder/Exceptions/IterableIsEmptyException.php diff --git a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php index 08fdf69..ec59a80 100755 --- a/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php +++ b/spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php @@ -5,6 +5,8 @@ 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; @@ -179,7 +181,7 @@ public function it_create_empty_array_loop_builder() { return $builder->to(1)->from(2)->url('notifynder.io')->category(1); }; - $this->shouldThrow('InvalidArgumentException')->during('loop', [[], $closure]); + $this->shouldThrow(IterableIsEmptyException::class)->during('loop', [[], $closure]); } public function it_create_empty_collection_loop_builder() @@ -188,6 +190,15 @@ public function it_create_empty_collection_loop_builder() { return $builder->to(1)->from(2)->url('notifynder.io')->category(1); }; - $this->shouldThrow('InvalidArgumentException')->during('loop', [new Collection([]), $closure]); + $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 e4a71fe..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); } /** @@ -254,7 +259,7 @@ public function refresh() */ protected function isIterable($var) { - return (is_array($var) || $var instanceof Traversable) && count($var) > 0; + return (is_array($var) || $var instanceof Traversable); } /** 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 @@ +