Skip to content

Commit

Permalink
Merge pull request #5 from m1x0n/feature/invariant-messages
Browse files Browse the repository at this point in the history
Invariant messages
  • Loading branch information
Barry O Sullivan committed Apr 21, 2017
2 parents 4ec40c4 + 86fcf15 commit 4fbef08
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 23 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

vendor/
.idea/tasks.xml
.idea/workspace.xml
.idea/
16 changes: 9 additions & 7 deletions src/Business/Invariant/AbstractInvariant.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ abstract class AbstractInvariant implements Invariant
{
protected $queryable;
protected $assumptions;
protected $error_message;
protected $error_message_positive;
protected $error_message_negative;

private $is_invariant;

Expand Down Expand Up @@ -56,12 +57,13 @@ public function is_satisfied()

public function asserts()
{
if(
(!$this->satisfier($this->queryable) && $this->is_invariant) ||
($this->satisfier($this->queryable) && !$this->is_invariant)
)
{
$error_message = $this->error_message ?: "Invariant broken: ".get_called_class();
if (!$this->satisfier($this->queryable) && $this->is_invariant) {
$error_message = $this->error_message_negative ?: "Invariant broken: ".get_called_class();
throw new Exception($error_message);
}

if ($this->satisfier($this->queryable) && !$this->is_invariant) {
$error_message = $this->error_message_positive ?: "Invariant broken: ".get_called_class();
throw new Exception($error_message);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Collection/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function key()

public function has_next()
{
return isset($this->items[$this->key]);
return isset($this->items[$this->key + 1]);
}

public function next()
Expand Down
18 changes: 18 additions & 0 deletions tests/CollectableItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

use BoundedContext\Contracts\Core\Collectable;

class CollectableItem implements Collectable
{
protected $secret;

public function __construct($value)
{
$this->secret = $value;
}

public function value()
{
return $this->secret;
}
}
28 changes: 15 additions & 13 deletions tests/CollectionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class CollectionTests extends PHPUnit_Framework_TestCase
{

/**
* @var Collection
*/
private $collection;

public function setup()
Expand All @@ -18,40 +20,40 @@ public function setup()

public function test_count()
{
$this->assertEquals($this->collection->count(), 3);
$this->assertEquals($this->collection->count()->value(), 3);
}

public function test_reset()
{
$this->assertEquals($this->collection->count(), 3);
$this->assertEquals($this->collection->count()->value(), 3);
$this->collection->reset();
$this->assertEquals($this->collection->count(), 0);
$this->assertEquals($this->collection->count()->value(), 0);
}

public function test_rewind()
{
$this->collection->next();
$this->collection->next();

$this->assertEquals($this->collection->current(), 'C');
$this->assertEquals($this->collection->current()->value(), 'C');

$this->collection->rewind();

$this->assertEquals($this->collection->current(), 'A');
$this->assertEquals($this->collection->current()->value(), 'A');
}

public function test_ordering()
{
$this->assertEquals($this->collection->current(), 'A');
$this->assertEquals($this->collection->current()->value(), 'A');

$this->assertTrue($this->collection->has_next());
$this->collection->next();
$this->assertEquals($this->collection->current(), 'B');
$this->assertEquals($this->collection->current()->value(), 'B');

$this->assertTrue($this->collection->has_next());
$this->collection->next();
$this->assertEquals($this->collection->current(), 'C');

$this->assertEquals($this->collection->current()->value(), 'C');
$this->assertFalse($this->collection->has_next());
}

Expand All @@ -67,7 +69,7 @@ public function test_append()
$this->assertTrue($this->collection->has_next());
$this->collection->next();

$this->assertEquals($this->collection->current(), 'D');
$this->assertEquals($this->collection->current()->value(), 'D');
$this->assertFalse($this->collection->has_next());
}

Expand All @@ -82,7 +84,7 @@ public function test_append_after_reset()
);

$this->assertFalse($this->collection->has_next());
$this->assertEquals($this->collection->current(), 'D');
$this->assertEquals($this->collection->current()->value(), 'D');
}

public function test_append_after_playthrough()
Expand All @@ -100,6 +102,6 @@ public function test_append_after_playthrough()
$this->collection->next();

$this->assertFalse($this->collection->has_next());
$this->assertEquals($this->collection->current(), 'D');
$this->assertEquals($this->collection->current()->value(), 'D');
}
}
8 changes: 8 additions & 0 deletions tests/Invariant/FakeQueryable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use BoundedContext\Contracts\Projection\Queryable;

class FakeQueryable implements Queryable
{

}
44 changes: 44 additions & 0 deletions tests/Invariant/InvariantMessagesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use BoundedContext\Contracts\Business\Invariant;

class InvariantMessagesTest extends PHPUnit_Framework_TestCase
{
public function test_valid_positive_assumption()
{
$exists = true;
$invariant = new LogicInvariant(new FakeQueryable());
$invariant->assuming([$exists])->asserts();
}

public function test_invalid_positive_assumption()
{
$this->setExpectedException(
Invariant\Exception::class,
'Universe does exist'
);

$exists = true;
$invariant = new LogicInvariant(new FakeQueryable());
$invariant->assuming([$exists])->not()->asserts();
}

public function test_valid_negative_assumption()
{
$exists = false;
$invariant = new LogicInvariant(new FakeQueryable());
$invariant->assuming([$exists])->not()->asserts();
}

public function test_invalid_negative_assumption()
{
$this->setExpectedException(
Invariant\Exception::class,
"Universe does not exist"
);

$exists = false;
$invariant = new LogicInvariant(new FakeQueryable());
$invariant->assuming([$exists])->asserts();
}
}
22 changes: 22 additions & 0 deletions tests/Invariant/LogicInvariant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use BoundedContext\Business\Invariant\AbstractInvariant;
use BoundedContext\Contracts\Business\Invariant\Invariant;

class LogicInvariant extends AbstractInvariant implements Invariant
{
protected $error_message_positive = "Universe does exist";
protected $error_message_negative = "Universe does not exist";

protected $flag;

protected function assumptions($flag)
{
$this->flag = $flag;
}

protected function satisfier()
{
return !!$this->flag;
}
}

0 comments on commit 4fbef08

Please sign in to comment.