This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updates a fair amount of handling to allow for some saner code. The big takeaway is that we now have a dynamic comparisons system which allows extensions to configure assertions per their needs. For example-- ```php <?php use Rawebone\Tapped\{Extension, Comparison, Comparator}; class MyExtension extends Extension { public function comparisons(Comparator $c) { $c->register(new class implements Comparison { compare($a, $b) { return $a instanceof $b; } name() { return 'toBeAnInstanceOf'; } }); } } ``` We can then call `$expect(new A)->toBeAnInstanceOf(A::class)`. In total this change might not seem very remarkable. However, it allows users to easily and cleanly extend the assertion functionality without needing to bloat the core system. Additionally this allows for domain specific comparisons to be exposed with domain specific extensions.
- Loading branch information
Showing
18 changed files
with
363 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
/vendor/ | ||
/.vscode/ | ||
/tests/ | ||
/.vscode/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the default comparisons that are shipped with the | ||
* framework. | ||
* | ||
* This file will be loaded when the framework boots. | ||
*/ | ||
|
||
use Rawebone\Tapped\Comparisons; | ||
|
||
return [ | ||
new Comparisons\Equals, | ||
new Comparisons\DoesNotEqual, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
/** | ||
* This file contains the default extensions that are shipped with the | ||
* framework. | ||
* | ||
* This file will be loaded when the framework boots. | ||
*/ | ||
|
||
return [ | ||
|
||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
namespace Rawebone\Tapped; | ||
|
||
use ReflectionClass; | ||
|
||
/** | ||
* Comparator acts as the method by which we can make comparisons between values. | ||
* | ||
* To allow for flexible integration with third-party tooling which requires | ||
* bespoke comparisons, we define this as a dynamic handler. | ||
* | ||
* Third parties can then register their comparisons at framework startup for | ||
* use inside the users tests. | ||
*/ | ||
class Comparator | ||
{ | ||
/** | ||
* The Comparisons that can be made. | ||
* | ||
* @var Comparison[] | ||
*/ | ||
protected $comparisons = []; | ||
|
||
/** | ||
* Finds the required handler and delegates to it to make the comparison. | ||
*/ | ||
public function compare(string $comparison, $subject, $expectation): bool | ||
{ | ||
if (!$this->has($comparison)) { | ||
return false; | ||
} | ||
|
||
return $this->comparisons[$comparison]->compare($subject, $expectation); | ||
} | ||
|
||
/** | ||
* Returns whether the given comparison has been registered. | ||
*/ | ||
public function has(string $comparison): bool | ||
{ | ||
return array_key_exists($comparison, $this->comparisons); | ||
} | ||
|
||
/** | ||
* Registers a comparison for use. | ||
* | ||
* The key by which the Comparison can be accessed will be determined by | ||
* one of two methods-- | ||
* | ||
* 1. If the comparison has a `name()` method, then that will be expected | ||
* to return the name in string form. | ||
* 2. Otherwise the short class name will be used, with the first character | ||
* lowercased. | ||
*/ | ||
public function register(Comparison $comparison) | ||
{ | ||
$name = $this->getNameFor($comparison); | ||
|
||
$this->comparisons[$name] = $comparison; | ||
} | ||
|
||
/** | ||
* Registers many comparisons for use. | ||
*/ | ||
public function registerMany(array $comparisons) | ||
{ | ||
foreach ($comparisons as $comparison) { | ||
if (!$comparison instanceof Comparison) { | ||
throw new BailOutError('Invalid comparison provided.'); | ||
} | ||
|
||
$this->register($comparison); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the real name of the comparison. | ||
*/ | ||
protected function getNameFor(Comparison $comparison): string | ||
{ | ||
if (method_exists($comparison, 'name')) { | ||
return $comparison->name(); | ||
} | ||
|
||
$reflection = new ReflectionClass($comparison); | ||
|
||
return lcfirst($reflection->getShortName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Rawebone\Tapped; | ||
|
||
/** | ||
* A Comparison provides a simple interface for checking whether two values | ||
* are "equal". | ||
*/ | ||
interface Comparison | ||
{ | ||
public function compare($subject, $expectation): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rawebone\Tapped\Comparisons; | ||
|
||
class DoesNotEqual extends Equals | ||
{ | ||
public function compare($subject, $expectation): bool | ||
{ | ||
return ! parent::compare($subject, $expectation); | ||
} | ||
|
||
public function name() | ||
{ | ||
return 'toNotEqual'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace Rawebone\Tapped\Comparisons; | ||
|
||
use Rawebone\Tapped\Comparison; | ||
|
||
class Equals implements Comparison | ||
{ | ||
public function compare($subject, $expectation): bool | ||
{ | ||
return $subject === $expectation; | ||
} | ||
|
||
public function name() | ||
{ | ||
return 'toEqual'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.