Skip to content

Commit

Permalink
Merge pull request #119 from fenos/issue-118
Browse files Browse the repository at this point in the history
Issue #118
  • Loading branch information
Gummibeer committed May 4, 2016
2 parents e4db5d2 + 8e2bf8e commit aa9e4ce
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 14 deletions.
34 changes: 33 additions & 1 deletion spec/Fenos/Notifynder/Builder/NotifynderBuilderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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]);
}
}
31 changes: 18 additions & 13 deletions src/Notifynder/Builder/NotifynderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Notifynder/Exceptions/EntityNotIterableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace Fenos\Notifynder\Exceptions;

use Exception;

/**
* Class EntityNotSpecifiedException
*
* @package Fenos\Notifynder\Exceptions
*/
class EntityNotIterableException extends Exception
{
}
12 changes: 12 additions & 0 deletions src/Notifynder/Exceptions/IterableIsEmptyException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php namespace Fenos\Notifynder\Exceptions;

use Exception;

/**
* Class EntityNotSpecifiedException
*
* @package Fenos\Notifynder\Exceptions
*/
class IterableIsEmptyException extends Exception
{
}

0 comments on commit aa9e4ce

Please sign in to comment.