Skip to content

Instantly share code, notes, and snippets.

@nexik
Created April 19, 2014 23:31
Show Gist options
  • Save nexik/11100704 to your computer and use it in GitHub Desktop.
Save nexik/11100704 to your computer and use it in GitHub Desktop.
SpecBDD in Codeception
<?php
namespace Codeception\Module;
use Codeception\Module;
use Codeception\PHPUnit\AssertWrapper;
use Closure;
class DevHelper extends Module
{
private $testSubject;
/**
* @param callable $callable
* @param array $params
* @return \Codeception\Module\Result
*/
public function specify(Closure $callable, $params = [])
{
$test = $callable->bindTo($this->getTestSubject());
return new Result(call_user_func_array($test, $params));
}
public function getTestSubject()
{
return $this->testSubject;
}
public function setTestSubject($subject)
{
$this->testSubject = $subject;
return $this;
}
}
class Result extends AssertWrapper
{
public function __construct($result)
{
$this->result = $result;
}
public function shouldReturn($value)
{
$this->assert(array('Equals', $this->result, $value));
}
}
<?php
/**
* This file is part Phalcon Nest (Phalcon SOLID bootstrap project for RAD)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT
*/
namespace Nest;
/**
* Registry
*
* @author Tomasz Ślązok <tomek@landingi.com>
*/
class Registry
{
/**
* @var array
*/
private $registry = [];
/**
* @param $key
* @param null $default
* @return null
*/
public function get($key, $default = null)
{
if (isset($this->registry[$key])) {
return $this->registry[$key];
}
return $default;
}
/**
* @param $key
* @param $value
*/
public function set($key, $value)
{
$this->registry[$key] = $value;
}
}
<?php
namespace unit;
use Nest\Registry;
/**
* unit/RegistryCest
*
* @mixin \Nest\Registry
* @author Tomasz Ślązok <tomek@landingi.com>
*/
class RegistryCest
{
public function testRegistry(\Dev $developer)
{
$developer->setTestSubject(new Registry());
$developer->expect('Registry return null for non existed key');
$developer->specify(function () {
return $this->get('non_existed');
})->shouldReturn(null);
}
}
@nexik
Copy link
Author

nexik commented Apr 20, 2014

Developer is my codeGuy i use codeception 2 so i generate my actor as dev.

This is only proof of concept but writing new type od tests looks very nice. I will try to write PR after some tweaks

@DavertMik
Copy link

Yep, I understood this, so I deleted my comment. To hide my stupidity )

Basically, what I propose is to think on the most clear way to implement the specs. If you don't need a CodeGuy create a new format. I would start with writing a spec syntax example, without thinking on implementation details.

The only problem I see is to not reinvent the PHPSpec )

@nexik
Copy link
Author

nexik commented Apr 20, 2014

I see two possibilities:

  • use CodeGuy to write simple specifications (using Cest)
  • just write bridge to PhpSpec for Codeception (using new format Spec)

Of course we can invented new syntax for Specifications but I think this is out of scope for me now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment