Skip to content

Instantly share code, notes, and snippets.

@gabrieljmj
Created February 17, 2015 23:20
Show Gist options
  • Save gabrieljmj/26fed8fff56e65cd8b4f to your computer and use it in GitHub Desktop.
Save gabrieljmj/26fed8fff56e65cd8b4f to your computer and use it in GitHub Desktop.
Simple Should port for PHP - complete framework https://github.com/GabrielJMJ/ShouldPHP
<?php
class Environment
{
private $shouldState;
private $verbs = array('be', 'have');
private $toTest;
public function __construct($toTest, $shouldState = false, $verb = null)
{
$this->toTest = $toTest;
$this->shouldState = $shouldState;
$this->verb = $verb;
}
public function __get($property)
{
if ($property === 'should') {
return new Environment($this->toTest, true);
} elseif (in_array($property, $this->verbs)) {
return new Environment($this->toTest, true, $property);
}
}
public function __call($method, $params)
{
if ($this->shouldState) {
$method = $this->verb . ucfirst($method);
return $this->{$method}($params);
}
}
private function beEquals(array $params)
{
return $this->toTest === $params[0];
}
private function beAnInstanceOf(array $params)
{
return $this->toTest instanceof $params[0];
}
}
<?php
$e = new Environment(new stdClass);
var_dump($e->should->be->anInstanceOf('stdClass'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment