Skip to content

Instantly share code, notes, and snippets.

@svenax
Created November 18, 2015 15:44
Show Gist options
  • Save svenax/08aec30330134c1e0fe9 to your computer and use it in GitHub Desktop.
Save svenax/08aec30330134c1e0fe9 to your computer and use it in GitHub Desktop.
<?php
class FakeStuff
{
private $faker = null;
public function __construct()
{
$this->faker = Faker\Factory::create('sv_SE');
}
public function nameList($num)
{
return array_map(function() {
return $this->faker->name;
}, range(1, $num));
}
public function dateList($num)
{
return array_map(function() {
return $this->faker->date;
}, range(1, $num));
}
}
<?php
namespace spec;
use PhpSpec\Exception\Example\FailureException;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
function myTitleCase($str)
{
return mb_strtoupper(mb_substr($str, 0, 1, 'UTF-8'), 'UTF-8') .
mb_strtolower(mb_substr($str, 1, null, 'UTF-8'), 'UTF-8');
}
class FakeStuffSpec extends ObjectBehavior
{
// Test methods -----------------------------------------------------------
public function it_is_initializable()
{
$this->shouldHaveType('FakeStuff');
}
public function it_lists_names()
{
$res = $this->nameList(10);
$res->shouldHaveCount(10);
$res->shouldHaveWords(2, 3);
$res->shouldHaveTitleCase();
}
public function it_lists_dates()
{
$res = $this->dateList(10);
$res->shouldHaveCount(10);
}
// Test helpers -----------------------------------------------------------
public function getMatchers()
{
return [
// Check that each element in the $subject array has between $from and $to words.
'haveWords' => function(array $subject, $from, $to) {
return array_reduce($subject, function($res, $item) use ($from, $to) {
$wc = count(explode(' ', $item));
$res = $res && $from <= $wc && $wc <= $to;
if (!$res) throw new FailureException("'$item' does not have between $from and $to words");
return $res;
}, true);
},
// Check that each element in the $subject array is UTF-8 title case.
'haveTitleCase' => function(array $subject) {
return array_reduce($subject, function($res, $item) {
return $res && array_reduce(preg_split('/[- ]/', $item), function($res, $item) {
$myItem = myTitleCase($item);
$res = $res && $item === $myItem;
if (!$res) throw new FailureException("'$item' is not '$myItem'");
return $res;
}, true);
}, true);
}
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment