Exemplo n.º 1
0
        expect($response)->to_be_a('pipes\\Response');
        expect(pipes\response())->to_be($response);
    });
    it("should allow you to override the current instance", function () {
        $oldResponse = pipes\response();
        $newResponse = new pipes\Request();
        expect(pipes\response($newResponse))->to_be($newResponse);
        expect(pipes\response())->to_be($newResponse);
        expect(pipes\response($oldResponse))->to_be($oldResponse);
        expect(pipes\response())->to_be($oldResponse);
    });
});
describe("Response", function () {
    before_each(function ($context) {
        pipes\headers(array());
        $context['response'] = new pipes\Response();
        return $context;
    });
    it("should be empty by default", function ($context) {
        extract($context);
        expect($response->body)->to_be_type('array')->and_to_be_empty();
        expect($response->headers)->to_be_a('pipes\\Hash')->to_have_count(0);
        expect($response->length)->to_be(0);
    });
    it("should allow you to specify headers", function ($context) {
        extract($context);
        $response->headers['Content-Type'] = 'text/html';
        $response->headers['Set-Cookie'] = 'username=skeletor';
        expect($response->headers)->to_have_count(2);
        expect($response->headers['Content-Type'])->to_be('text/html');
        expect($response->headers['Set-Cookie'])->to_be('username=skeletor');
Exemplo n.º 2
0
     it("should return the module", function ($scope) {
         extract($scope);
         expect($loader->load('cakes.php'))->to_be($cakes);
         expect($loader->load('pies.php'))->to_be($pies);
     });
     it("should return null if the module doesn't exist", function ($scope) {
         extract($scope);
         expect($loader->load('tarts.php'))->to_be_null();
     });
 });
 describe("loadClass()", function () {
     before_each(function ($scope) {
         // recreating these here because we want real modules
         // instead of the mock modules
         $cakes = new modules\Module('cakes.php', __DIR__ . '/../cakes.php', 'Cake*');
         $pies = new modules\Module('pies.php', __DIR__ . '/../pies.php', 'Pie*');
         $loader = new modules\Loader();
         $loader->addModule($cakes);
         $loader->addModule($pies);
         return array_merge($scope, compact('loader', 'cakes', 'pies'));
     });
     it("should call load() on the matching module", function ($scope) {
         extract($scope);
         $loader->loadClass('Cake');
         expect($cakes->loaded)->to_be_true();
     });
     it("should return the matched module", function ($scope) {
         extract($scope);
         expect($loader->loadClass('Cake'))->to_be($cakes);
     });
     it("should return null if there wasn't a matching module", function ($scope) {
         extract($scope);
<?php

namespace Preview\DSL\BDD;

require_once __DIR__ . '/../ok.php';
shared_example("to share test", function () {
    it("will use the caller's context", function () {
        ok($this->name == "wenjun.yan");
    });
    describe("create a test suite here", function () {
        it("and still have access to vars defined caller", function () {
            ok($this->name == "wenjun.yan");
        });
    });
});
describe("it_behaves_like", function () {
    before_each(function () {
        $this->name = "wenjun.yan";
    });
    /*
     * the following line will be replaced by
     * code defined in shared_exmaple "to share test";
     */
    it_behaves_like("to share test");
});
Exemplo n.º 4
0
<?php

namespace Preview\DSL\BDD;

require_once 'ok.php';
shared_example("stack", function ($group) {
    describe("#pop", function () {
        it("should return the last item", function () {
            $end = end($this->subject);
            ok(array_pop($this->subject) == $end);
        });
        it("remove the last item", function () {
        });
    })->group($group);
});
describe("world", function () {
    before_each(function () {
        $this->subject = array(1, 2, 3);
    });
    it_behaves_like("stack", "hello world");
});
Exemplo n.º 5
0
<?php

use pecs\Spec;
use Fu\Traffic as t;
describe("traffic", function () {
    before_each('reset_request');
    describe("handling different types of callback", function () {
        it("should run callback on a static class", function () {
            mimick_request('/', 'GET');
            $gather = gather_info(function () {
                t::get('/', array('CallbackTestClass', 'index'));
            });
            expect($gather)->to_be('index.');
        });
        it("should run callback on an instantiated object", function () {
            mimick_request('/', 'GET');
            $gather = gather_info(function () {
                t::get('/', array(new CallbackTestClass(), 'index'));
            });
            expect($gather)->to_be('index.');
        });
        it("should run callback with hooks if available on an instantiated object", function () {
            mimick_request('/', 'GET');
            $gather = gather_info(function () {
                t::get('/', array(new CallbackTestClassWithHooks(), 'index'));
            });
            expect($gather)->to_be('before.index.after.');
        });
        it("should not run hooks on a static class", function () {
            mimick_request('/', 'GET');
            $gather = gather_info(function () {
Exemplo n.º 6
0
<?php

namespace Preview\DSL\BDD;

require_once 'stack.php';
require_once __DIR__ . '/../ok.php';
describe("Stack", function () {
    before_each(function () {
        $this->stack = new \Stack(array(1, 2, 3));
    });
    describe("#size", function () {
        it("returns the size of stack", function () {
            ok($this->stack->size() == 3);
        });
    });
    describe("#peek", function () {
        it("returns the last element", function () {
            ok($this->stack->peek() == 3);
        });
    });
    describe("#push", function () {
        it("pushes an element to stack", function () {
            $this->stack->push(4);
            ok($this->stack->peek() == 4);
            ok($this->stack->size() == 4);
        });
    });
    describe("#pop", function () {
        it("pops out the last element", function () {
            ok($this->stack->pop() == 3);
            ok($this->stack->size() == 2);
Exemplo n.º 7
0
<?php

namespace Preview\DSL\BDD;

require_once 'ok.php';
describe("parent", function () {
    it("should be ok", function () {
        ok(true);
    });
    describe("here we go", function () {
        before(function () {
            $this->x;
        });
        before_each(function () {
        });
        it("failed", function () {
            $a->xuser;
        });
        it("ok", function () {
        });
    });
});
Exemplo n.º 8
0
describe("before_each", function () {
    before_each(function () {
        $this->usage = "run before each test case";
    });
    before_each(function () {
        $this->note_1 = "before_each hooks are run in order";
    });
    before_each(function () {
        $this->ref = new \stdClass();
        $this->ref->name = "wenjun.yan";
        $this->value = "string";
    });
    it("can access the variable set in before_each hooks", function () {
        ok($this->note_1);
        ok($this->note_2);
        ok($this->value);
        ok($this->ref->name);
        $this->value = null;
        $this->ref->name = null;
    });
    it("before_each hooks are run before each test case", function () {
        // $this-value and $this->ref are reassigned.
        ok($this->value);
        // string is passed by value
        ok($this->ref->name);
        // object is passed by "ref".
    });
    before_each(function () {
        $this->note_2 = "wherever you put the before each hook, " . "it will run before each test case";
    });
});
Exemplo n.º 9
0
     $hash[] = 'foo';
     expect($hash)->to_have_count(1);
     $hash[] = 'bar';
     expect($hash)->to_have_count(2);
 });
 it("should return null for values that are not set", function () {
     $hash = new pipes\Hash();
     expect($hash[0])->to_be_null();
     $hash[0] = 'foo';
     expect($hash[0])->to_be('foo');
     unset($hash[0]);
     expect($hash[0])->to_be_null();
 });
 describe("get()", function () {
     before_each(function ($context) {
         $context['hash'] = new pipes\Hash(array('foo' => 1));
         return $context;
     });
     it("should return the value of the matching key", function ($context) {
         extract($context);
         expect($hash->get('foo'))->to_be(1);
         expect($hash->get('foo', 2))->to_be(1);
     });
     it("should fallback to null for missing values by default", function ($context) {
         extract($context);
         expect($hash->get('bar'))->to_be_null();
     });
     it("should fallback to a custom value if specified", function ($context) {
         extract($context);
         expect($hash->get('bar', 2))->to_equal(2);
         $hash['bar'] = 3;
         expect($hash->get('bar'))->to_equal(3);
Exemplo n.º 10
0
    });
    it("should allow you to override the current instance", function () {
        $oldRequest = pipes\request();
        $newRequest = new pipes\Request();
        expect(pipes\request($newRequest))->to_be($newRequest);
        expect(pipes\request())->to_be($newRequest);
        expect(pipes\request($oldRequest))->to_be($oldRequest);
        expect(pipes\request())->to_be($oldRequest);
    });
});
describe("Request", function () {
    before_each(function ($context) {
        $oldServer = $_SERVER;
        $oldRequest = $_REQUEST;
        $_SERVER['REQUEST_URI'] = '/foo/bar/baz.biff';
        $_SERVER['REQUEST_METHOD'] = 'PUT';
        $_REQUEST['a'] = 1;
        $_REQUEST['b'] = 2;
        $request = new pipes\Request();
        return array_merge($context, compact('oldServer', 'oldRequest', 'request'));
    });
    after_each(function ($context) {
        $_SERVER = $context['oldServer'];
        $_REQUEST = $context['oldRequest'];
    });
    it("should copy \$_SERVER['REQUEST_URI'] to \$request->uri", function ($context) {
        extract($context);
        expect($request->uri)->to_be($_SERVER['REQUEST_URI']);
    });
    it("should copy \$_SERVER['REQUEST_METHOD'] to \$request->method", function ($context) {
        extract($context);
        expect($request->method)->to_be($_SERVER['REQUEST_METHOD']);
Exemplo n.º 11
0
<?php

describe("run", function () {
    before_each(function ($context) {
        $oldServer = $_SERVER;
        $oldRequest = $_REQUEST;
        $_SERVER['REQUEST_URI'] = '/foo';
        $_SERVER['REQUEST_METHOD'] = 'GET';
        $_REQUEST['a'] = 1;
        $_REQUEST['b'] = 2;
        pipes\routes(array());
        $options = pipes\options(new pipes\Hash());
        $request = pipes\request(new pipes\Request());
        $response = pipes\response(new pipes\Response());
        return array_merge($context, compact('oldServer', 'oldRequest', 'request'));
    });
    after_each(function ($context) {
        $_SERVER = $context['oldServer'];
        $_REQUEST = $context['oldRequest'];
    });
    it("should run and return the first matching route", function () {
        $route1 = pipes\get('/foo', function () {
            return 'bar';
        });
        $route2 = pipes\get('/foo', function () {
            return 'baz';
        });
        ob_start();
        expect(pipes\run())->to_be_type('object')->and_to_be($route1);
        expect(ob_get_clean())->to_be('bar');
    });
Exemplo n.º 12
0
         $args = array();
         $route = new pipes\Route('/', function () use(&$count, &$args) {
             $count += 1;
             $args[] = func_get_args();
         });
         $route->runCallback(array(1, 2, 3));
         $route->runCallback(array(4, 5, 6));
         expect($count)->to_be(2);
         expect($args)->to_have_length(2);
         expect($args[0])->to_be(array(1, 2, 3));
         expect($args[1])->to_be(array(4, 5, 6));
     });
 });
 describe("runPaths()", function () {
     before_each(function ($context) {
         $context['route'] = new pipes\Route('/(?<path>.*)', array('paths' => array(__DIR__ . '/mock/path1', __DIR__ . '/mock/path2')));
         return $context;
     });
     it("should include matching files in the paths in order", function ($context) {
         extract($context);
         ob_start();
         expect($route->runPaths('/foo'))->to_be('foo1foo2');
         expect(ob_get_clean())->to_be('');
     });
     it("should stop iterating over the path if \$route->bubble is set to false", function ($context) {
         extract($context);
         ob_start();
         expect($route->runPaths('/bar'))->to_be('bar1');
         expect(ob_get_clean())->to_be('');
     });
     it("should not fail if at least one matching file exists", function ($context) {
         extract($context);
Exemplo n.º 13
0
     });
     it('should use an existent probe by config', function ($scope) {
         $po = $scope->p->addProbe('p1', 'number');
         $scope->p->update(array('name' => 'p1'), 123);
         expect($scope->p->probes->p1->args)->to_be(array(123));
         expect($scope->p->probes->p1->id)->to_be($po->id);
     });
     it('should add a new probe by config if not exist', function ($scope) {
         $scope->p->update(array('name' => 'p1'), 123);
         expect($scope->p->probes->p1->args)->to_be(array(123));
     });
 });
 describe('#increment', function () {
     before_each(function ($scope) {
         $scope = (object) $scope;
         $scope->p = new Provider(array('name' => 'p'));
         return $scope;
     });
     after_each(function ($scope) {
         expect(get_object_vars($scope->p->probes))->to_have_count(2);
         $count = 0;
         $scope->p->on('sample', function () use(&$count) {
             $count++;
         });
         $scope->p->probes->p1->sample('c1');
         expect($count)->to_be(1);
     });
     it('should use an existent probe by name', function ($scope) {
         $po = $scope->p->addProbe('p1', 'number');
         $scope->p->probes->p1->update(5);
         $scope->p->increment('p1');
Exemplo n.º 14
0
<?php

require_once __DIR__ . '/../mocks.php';
describe("Module", function () {
    before_each(function ($scope) {
        $cakes = new modules\Module('cakes.php', __DIR__ . '/../cakes.php', array('Cake*', 'Candle*', 'Lie'));
        $pies = new modules\Module('pies.php', __DIR__ . '/../pies.php', 'Pie*');
        return array_merge($scope, compact('cakes', 'pies'));
    });
    it("should set the passed parameters as attributes", function ($scope) {
        extract($scope);
        expect($cakes->name)->to_be('cakes.php');
        expect($cakes->filename)->to_be(__DIR__ . '/../cakes.php');
        expect($cakes->patterns)->to_be_type('array')->and_to_have_count(3);
        expect($cakes->patterns[0])->to_be('Cake*');
    });
    it("should convert the \$patterns argument into an array", function ($scope) {
        extract($scope);
        expect($pies->patterns)->to_be_type('array')->and_to_have_count(1);
        expect($pies->patterns[0])->to_be('Pie*');
    });
    it("should not be loaded by default", function ($scope) {
        extract($scope);
        expect($cakes->loaded)->to_be_false();
    });
    describe("load()", function () {
        it("should include the referenced file", function ($scope) {
            extract($scope);
            expect(class_exists('Cake', false))->to_be_false();
            expect($cakes->load())->to_be_true();
            expect($cakes->loaded)->to_be_true();