Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #118 #119

Merged
merged 3 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
{
}