fixture() public static method

I wish we didn't have to do this. In PHP 5.4 we may just be able to bind the tests to an object and access fixtures via $this
See also: FUnit::setup()
public static fixture ( string $key, mixed $val = null ) : mixed
$key string the key to set or retrieve
$val mixed the value to assign to the key. OPTIONAL
return mixed the value of the $key passed.
<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Zend\Stdlib\Hydrator\ObjectProperty;
use Knlv\Zf2\Hydrator\Strategy\MultilineTextStrategy;
fu::setup(function () {
    $hydrator = new ObjectProperty();
    $strategy = new MultilineTextStrategy();
    $hydrator->addStrategy('items', $strategy);
    fu::fixture('strategy', $strategy);
    fu::fixture('hydrator', $hydrator);
});
fu::test('Testing hydrate/extract methods', function () {
    $hydrator = fu::fixture('hydrator');
    $object = new stdClass();
    $object->name = 'test';
    $object->items = array('item1', 'item2', 'item3', 'item4', 'item5');
    $expected = array('name' => 'test', 'items' => <<<EOL
item1
item2
item3
item4
item5
EOL
);
    fu::equal($expected, $hydrator->extract($object), 'Assert extract works');
    fu::equal($object, $hydrator->hydrate($expected, new stdClass()), 'Assert hydrate works');
});
<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Knlv\Zf2\Validator\NotIdentical;
fu::setup(function () {
    $validator = new NotIdentical();
    fu::fixture('validator', $validator);
});
fu::test('Test not_identical validates correctly', function () {
    $validator = fu::fixture('validator');
    $validator->setToken('token');
    fu::not_ok($validator->isValid('value', array('token' => 'value')), 'Assert validator returns false on same');
    fu::ok($validator->isValid('value', array('token' => 'other')), 'Assert validator retuns true on different');
    fu::ok($validator->isValid('value'), 'Assert validator returns true if no context provided');
    fu::ok($validator->isValid('value', array('other_token' => 'value')), 'Assert validator returns true if token not found in context');
    $validator->setToken(null);
    fu::not_ok($validator->isValid('value', array('token' => 'value')), 'Assert validator return false if no token is set');
});
fu::test('Test not_identical messages', function () {
    $validator = fu::fixture('validator');
    $validator->setToken('token');
    $validator->isValid('value', array('token' => 'value'));
    fu::has($validator::SAME, $validator->getMessages(), 'Assert same message');
    $validator->isValid('value', array('token' => 'other'));
    $messages = $validator->getMessages();
    fu::ok(empty($messages), 'Assert empty messages if validator validates');
    $validator->setToken(null);
    $validator->isValid('value', array('token' => 'value'));
    fu::has($validator::MISSING_TOKEN, $validator->getMessages(), 'Assert missing token message');
});
    $hydrator = new ObjectProperty();
    $strategy = new DateTimeStrategy();
    $hydrator->addStrategy('created', $strategy);
    fu::fixture('strategy', $strategy);
    fu::fixture('hydrator', $hydrator);
});
fu::test('Testing hydrate/extract methods', function () {
    $hydrator = fu::fixture('hydrator');
    $object = new stdClass();
    $object->name = 'test';
    $object->data = array('data1', 'data2');
    $object->created = new DateTime('2015-01-02 10:10:10');
    $expected = array('name' => 'test', 'data' => array('data1', 'data2'), 'created' => '2015-01-02 10:10:10');
    fu::equal($expected, $hydrator->extract($object), 'Assert extract works');
    fu::equal($object, $hydrator->hydrate($expected, new stdClass()), 'Assert hydrate works');
});
fu::test('Test set/get format methods', function () {
    $strategy = fu::fixture('strategy');
    fu::equal($strategy::DEFAULT_FORMAT, $strategy->getFormat(), 'Assert default format if no other set');
    $strategy->setFormat(DateTime::ATOM);
    fu::equal(DateTime::ATOM, $strategy->getFormat(), 'Assert format changed');
});
fu::test('Test set/get timzone methods', function () {
    $strategy = fu::fixture('strategy');
    fu::equal(new DateTimeZone(date_default_timezone_get()), $strategy->getTimezone(), 'Assert default timezone if no other set');
    $timezone = new DateTimeZone('Europe/London');
    $strategy->setTimezone($timezone);
    fu::equal($timezone, $strategy->getTimezone(), 'Assert timezone set as object');
    $strategy->setTimezone('Europe/Athens');
    fu::equal($timezone, $strategy->getTimezone(), 'Assert timezone set as string');
});
Example #4
0
    fu::strict_equal('Fixture Suite', $ts->getName());
});
fu::test("Check suite run state", function () {
    $ts = fu::fixture('ts');
    fu::strict_equal(false, $ts->run);
    $ts->run();
    fu::strict_equal(true, $ts->run);
});
fu::test("Check suite exit code 1", function () {
    $ts = fu::fixture('ts');
    fu::strict_equal(0, $ts->getExitCode());
    $ts->addTest('known to fail for suite', function () use($ts) {
        // this forces the result of this assertion to be recorded in
        // the `$ts` TestSuite instance
        fu::fail($ts, 'this always fails');
    });
    $ts->run();
    fu::strict_equal(1, $ts->getExitCode());
});
fu::test("Check suite exit code 0", function () {
    $ts = fu::fixture('ts');
    fu::strict_equal(0, $ts->getExitCode());
    $ts->addTest('known to fail for suite', function () use($ts) {
        // this forces the result of this assertion to be recorded in
        // the `$ts` TestSuite instance
        fu::pass($ts, 'this always fails');
    });
    $ts->run();
    fu::strict_equal(0, $ts->getExitCode());
});
fu::run();
Example #5
0
<?php

use FUnit as fu;
fu::setup(function () {
    // set a fixture to use in tests
    fu::fixture('foobar', array('foo' => 'bar'));
});
fu::teardown(function () {
    // this resets the fu::$fixtures array. May not provide clean shutdown
    fu::reset_fixtures();
});
fu::test('Checking for exceptions', function () {
    $callback = function () {
        throw new RuntimeException();
    };
    fu::throws($callback, 'RuntimeException', 'Correct exception');
    $callback = function ($foo) {
        throw new RuntimeException($foo);
    };
    fu::throws($callback, array('bar'), 'LogicException', 'Not the correct exception');
});
fu::test('Forced failure', function () {
    fu::fail('This is a forced fail');
});
fu::test('Expected failure', function () {
    fu::expect_fail('This is a good place to describe a missing test');
});
fu::test('Forced Errors/Exception', function () {
    trigger_error('This was triggered inside a test', E_USER_ERROR);
    trigger_error('This was triggered inside a test', E_USER_NOTICE);
    throw new Exception('This was thrown inside a test');
    $data = array(array('foo' => ' bazbat ', 'bar' => ' 12345 ', 'baz' => ''), array('foo' => ' bazbar ', 'bar' => ' 12345 ', 'baz' => ''));
    $expected = array(array('foo' => 'bazbat', 'bar' => '12345', 'baz' => ''), array('foo' => 'bazbar', 'bar' => '12345', 'baz' => ''));
    $collectionFilter = fu::fixture('collectionFilter');
    $collectionFilter->setData($data);
    $collectionFilter->setUniqueFields(array('foo'));
    fu::ok($collectionFilter->isValid(), 'Assert inputfilter is valid');
    $messages = $collectionFilter->getMessages();
    fu::ok(empty($messages), 'Assert inputfilter has no error messages');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
    $collectionFilter->setUniqueFields(array('bar'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[1]['bar'][$collectionFilter::NOT_UNIQUE]), 'Assert message for not unique isset');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
});
fu::test('Test inputfilter validate for unique values in fields on invalid data', function () {
    $data = array(array('foo' => ' bazbatfoo ', 'bar' => ' 12345 ', 'baz' => ''), array('foo' => ' bazbatfoo ', 'bar' => ' 54321 ', 'baz' => ''));
    $expected = array(array('foo' => 'bazbatfoo', 'bar' => '12345', 'baz' => ''), array('foo' => 'bazbatfoo', 'bar' => '54321', 'baz' => ''));
    $collectionFilter = fu::fixture('collectionFilter');
    $collectionFilter->setData($data);
    $collectionFilter->setUniqueFields(array('bar'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[0]['foo'][Validator\StringLength::TOO_LONG]), 'Assert correct error messages');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
    $collectionFilter->setUniqueFields(array('foo'));
    fu::not_ok($collectionFilter->isValid(), 'Assert inputfilter is invalid');
    $messages = $collectionFilter->getMessages();
    fu::ok(isset($messages[0]['foo'][Validator\StringLength::TOO_LONG]) && isset($messages[1]['foo'][Validator\StringLength::TOO_LONG]) && isset($messages[1]['foo'][$collectionFilter::NOT_UNIQUE]), 'Assert message for not unique isset along with other');
    fu::equal($expected, $collectionFilter->getValues(), 'Assert inputfilter returns expected values');
});
use Zend\Permissions\Acl\Acl;
use Knlv\Zf2\Permissions\Acl\Assertion\Callback as AclCallback;
fu::setup(function () {
    $acl = new Acl();
    // add roles
    $acl->addRole('guest');
    $acl->addRole('member', 'guest');
    // add resources
    $acl->addResource('article');
    // add rules
    $acl->allow('guest', 'article', array('read'));
    $acl->allow('member', 'article', array('write', 'delete'));
    fu::fixture('acl', $acl);
});
fu::test('Test acl callback assertion', function () {
    $acl = fu::fixture('acl');
    $test = $acl->isAllowed('guest', 'article', 'read') && $acl->isAllowed('member', 'article', 'read') && $acl->isAllowed('member', 'article', 'write') && !$acl->isAllowed('guest', 'article', 'write');
    fu::ok($test, 'Test acl without assertions');
    $assertTrue = new AclCallback(function () {
        return true;
    });
    $assertFalse = new AclCallback(function () {
        return false;
    });
    $acl->removeAllow('member', 'article', 'write');
    $acl->allow('member', 'article', 'write', $assertFalse);
    fu::not_ok($acl->isAllowed('member', 'article', 'write'), 'Assert not allowed when callback returns false');
    $acl->removeAllow('member', 'article', 'write');
    $acl->allow('member', 'article', 'write', $assertTrue);
    fu::ok($acl->isAllowed('member', 'article', 'write'), 'Assert allowed when callback returns true');
});
Example #8
0
    fu::fixture('foobar', array('foo' => 'bar'));
});
fu::teardown(function () {
    // this resets the fu::$fixtures array. May not provide clean shutdown
    fu::reset_fixtures();
});
fu::test("this is a test", function () {
    fu::ok(1, "the integer '1' is okay");
    fu::ok(0, "the integer '0' is not okay");
    // this will fail!
});
fu::test("another test", function () {
    fu::equal(true, 1, "the integer '1' is truthy");
    fu::not_strict_equal(true, 1, "the integer '1' is NOT true");
    // access a fixture
    $foobar = fu::fixture('foobar');
    fu::equal($foobar['foo'], 'bar', "the fixture 'foobar' should have a key 'foo' equal to 'baz'");
    $fooarr = array('blam' => 'blaz');
    fu::has('blam', $fooarr, "\$fooarr has a key named 'blam'");
    $fooobj = new \StdClass();
    $fooobj->blam = 'blaz';
    fu::has('blam', $fooobj, "\$fooobj has a property named 'blam'");
});
fu::test('Checking for exceptions', function () {
    $callback = function () {
        throw new RuntimeException();
    };
    fu::throws($callback, 'RuntimeException', 'Correct exception');
    $callback = function ($foo) {
        throw new RuntimeException($foo);
    };
<?php

require __DIR__ . '/../vendor/autoload.php';
use FUnit as fu;
use Zend\Permissions\Rbac\Rbac;
use Knlv\Zf2\Permissions\Rbac\Assertion\Callback as RbacCallback;
fu::setup(function () {
    $rbac = new Rbac();
    $rbac->addRole('member');
    $rbac->addRole('guest', 'member');
    $rbac->getRole('guest')->addPermission('read');
    $rbac->getRole('member')->addPermission('write');
    fu::fixture('rbac', $rbac);
});
fu::test('Test rbac callback assertion', function () {
    $rbac = fu::fixture('rbac');
    $test = $rbac->isGranted('guest', 'read') && $rbac->isGranted('member', 'read') && !$rbac->isGranted('guest', 'write') && $rbac->isGranted('member', 'write');
    fu::ok($test, 'Test rbac without assertions');
    $assertTrue = new RbacCallback(function () {
        return true;
    });
    $assertFalse = new RbacCallback(function () {
        return false;
    });
    fu::not_ok($rbac->isGranted('member', 'read', $assertFalse), 'Assert permission not granted when callback returns false');
    fu::ok($rbac->isGranted('member', 'write', $assertTrue), 'Assert permission granted when callback returns true');
});
Example #10
0
<?php

use FUnit as fu;
require_once __DIR__ . '/../src/FUnit.php';
fu::suite('Fixture test suite');
fu::test('Test adding fixtures', function () {
    fu::fixture('a', array(1, 2, 3));
    $a = fu::fixture('a');
    fu::strict_equal(array(1, 2, 3), $a);
});
fu::test('Test resetting fixtures', function () {
    fu::fixture('a', array(1, 2, 3));
    $a = fu::fixture('a');
    fu::reset_fixtures();
    fu::ok(is_null(fu::fixture('a')));
});
fu::run();