Exemplo n.º 1
0
namespace Chaos\Database\Spec\Suite;

use Lead\Set\Set;
use Chaos\ChaosException;
use Chaos\Database\DatabaseException;
use Chaos\Model;
use Chaos\Finders;
use Chaos\Database\Query;
use Chaos\Database\Schema;
use Chaos\Database\Spec\Fixture\Fixtures;
$box = box('chaos.spec');
$connections = ["MySQL" => $box->has('source.database.mysql') ? $box->get('source.database.mysql') : null, "PgSql" => $box->has('source.database.postgresql') ? $box->get('source.database.postgresql') : null];
foreach ($connections as $db => $connection) {
    describe("Query[{$db}]", function () use($connection) {
        beforeAll(function () use($connection) {
            skipIf(!$connection);
        });
        beforeEach(function () use($connection) {
            $this->connection = $connection;
            $this->fixtures = new Fixtures(['connection' => $connection, 'fixtures' => ['gallery' => 'Chaos\\Database\\Spec\\Fixture\\Schema\\Gallery', 'gallery_detail' => 'Chaos\\Database\\Spec\\Fixture\\Schema\\GalleryDetail', 'image' => 'Chaos\\Database\\Spec\\Fixture\\Schema\\Image', 'image_tag' => 'Chaos\\Database\\Spec\\Fixture\\Schema\\ImageTag', 'tag' => 'Chaos\\Database\\Spec\\Fixture\\Schema\\Tag']]);
            $this->fixtures->populate('gallery', ['create']);
            $this->fixtures->populate('gallery_detail', ['create']);
            $this->fixtures->populate('image', ['create']);
            $this->fixtures->populate('image_tag', ['create']);
            $this->fixtures->populate('tag', ['create']);
            $this->gallery = $this->fixtures->get('gallery')->model();
            $this->galleryDetail = $this->fixtures->get('gallery_detail')->model();
            $this->image = $this->fixtures->get('image')->model();
            $this->image_tag = $this->fixtures->get('image_tag')->model();
            $this->tag = $this->fixtures->get('tag')->model();
            $this->query = new Query(['model' => $this->gallery]);
Exemplo n.º 2
0
namespace Kahlan\Spec\Suite\Plugin;

use Kahlan\Jit\Interceptor;
use Kahlan\QuitException;
use Kahlan\Plugin\Quit;
use Kahlan\Jit\Patcher\Quit as QuitPatcher;
use Kahlan\Spec\Fixture\Plugin\Quit\Foo;
describe("Quit", function () {
    /**
     * Save current & reinitialize the Interceptor class.
     */
    beforeAll(function () {
        $this->previous = Interceptor::instance();
        Interceptor::unpatch();
        $cachePath = rtrim(sys_get_temp_dir(), DS) . DS . 'kahlan';
        $include = ['Kahlan\\Spec\\'];
        $interceptor = Interceptor::patch(compact('include', 'cachePath'));
        $interceptor->patchers()->add('quit', new QuitPatcher());
    });
    /**
     * Restore Interceptor class.
     */
    afterAll(function () {
        Interceptor::load($this->previous);
    });
    describe("::enable()", function () {
        it("enables quit statements", function () {
            Quit::disable();
            expect(Quit::enabled())->toBe(false);
            Quit::enable();
            expect(Quit::enabled())->toBe(true);
Exemplo n.º 3
0
<?php

namespace Chaos\Spec\Suite;

use stdClass;
use DateTime;
use InvalidArgumentException;
use Chaos\Model;
use Chaos\Schema;
use Chaos\Collection\Collection;
use Kahlan\Plugin\Double;
describe("Model", function () {
    beforeAll(function () {
        $model = $this->model = Double::classname(['extends' => Model::class]);
        $model::definition()->locked(false);
    });
    afterEach(function () {
        $model = $this->model;
        $model::reset();
        $model::definition()->locked(false);
    });
    describe("::conventions()", function () {
        it("gets/sets a conventions", function () {
            $conventions = Double::instance();
            $model = $this->model;
            $model::conventions($conventions);
            expect($model::conventions())->toBe($conventions);
        });
    });
    describe("::connection()", function () {
        it("gets/sets a connection", function () {
Exemplo n.º 4
0
<?php

namespace Kahlan\Spec\Suite\Cli;

use Kahlan\Cli\Cli;
describe("Cli", function () {
    describe('->color()', function () {
        beforeAll(function () {
            $this->check = function ($actual, $expected) {
                expect(strlen($actual))->toBe(strlen($expected));
                for ($i = 0; $i < strlen($actual); $i++) {
                    $check = ord($actual[$i]) == ord($expected[$i]) ? true : false;
                    if ($check) {
                        break;
                    }
                }
                expect($check)->toBe(true);
            };
        });
        it("leaves string unchanged whith no options", function () {
            expect(Cli::color("String"))->toBe("String");
        });
        it("applies a color using a string as options", function () {
            $this->check(Cli::color("String", "yellow"), "Srting");
        });
        it("applies a complex color using a semicolon separated string as options", function () {
            $this->check(Cli::color("String", "n;yellow;100"), "Srting");
            $this->check(Cli::color("String", "4;red;100"), "Srting");
            $this->check(Cli::color("String", "n;100;100"), "Srting");
        });
        it("applies the 39 default color with unknown color name", function () {
Exemplo n.º 5
0
<?php

namespace Chaos\Database\Spec\Suite\Adapter;

use DateTime;
use Chaos\Database\DatabaseException;
use Chaos\Database\Adapter\Sqlite;
use Chaos\Database\Schema;
describe("Sqlite", function () {
    beforeAll(function () {
        $box = box('chaos.spec');
        skipIf(!$box->has('source.database.sqlite'));
    });
    beforeEach(function () {
        $box = box('chaos.spec');
        $this->adapter = $box->get('source.database.sqlite');
    });
    describe("::enabled()", function () {
        it("returns `true` for enabled features, false otherwise.", function () {
            expect(Sqlite::enabled())->toEqual(['arrays' => false, 'transactions' => false, 'booleans' => true, 'default' => false]);
            expect(Sqlite::enabled('arrays'))->toBe(false);
            expect(Sqlite::enabled('transactions'))->toBe(false);
            expect(Sqlite::enabled('booleans'))->toBe(true);
            expect(Sqlite::enabled('default'))->toBe(false);
        });
        it("throws an exception if the extension is not loaded.", function () {
            allow('extension_loaded')->toBeCalled()->andReturn(false);
            expect(function () {
                Sqlite::enabled();
            })->toThrow(new DatabaseException("The PDO SQLite extension is not installed."));
        });
Exemplo n.º 6
0
<?php

namespace Kahlan\Spec\Suite;

use stdClass;
use Exception;
use Kahlan\Suite;
use Kahlan\Specification;
use Kahlan\Matcher;
use Kahlan\Plugin\Double;
describe("Specification", function () {
    beforeAll(function () {
        Suite::$PHP = 5;
    });
    afterAll(function () {
        Suite::$PHP = PHP_MAJOR_VERSION;
    });
    beforeEach(function () {
        $this->spec = new Specification(['closure' => function () {
        }]);
    });
    describe("->passed()", function () {
        it("returns the closure return value", function () {
            $this->spec = new Specification(['closure' => function () {
                return 'hello world';
            }]);
            $return = null;
            $this->spec->passed($return);
            expect($return)->toBe('hello world');
        });
        it("fails when an expectation is not verified", function () {
Exemplo n.º 7
0
namespace Lead\Jit\Spec\Suite;

use Lead\Jit\JitException;
use Lead\Jit\Patchers;
use Lead\Jit\Interceptor;
use Kahlan\Dir\Dir;
use Kahlan\Plugin\Double;
use Lead\Jit\Spec\Proxy\Autoloader;
use Lead\Jit\Spec\Mock\Patcher;
describe("Interceptor", function () {
    beforeAll(function () {
        $this->composer = Interceptor::composer();
        skipIf(!$this->composer);
        $composer = clone $this->composer[0];
        $this->autoloader = new Autoloader($composer);
        spl_autoload_register([$this->autoloader, 'loadClass']);
        spl_autoload_unregister($this->composer);
        $this->cachePath = Dir::tempnam(null, 'cache');
    });
    afterEach(function () {
        Interceptor::unpatch();
    });
    afterAll(function () {
        spl_autoload_register($this->composer);
        spl_autoload_unregister([$this->autoloader, 'loadClass']);
        Dir::remove($this->cachePath);
    });
    describe("::patch()", function () {
        it("patches the composer autoloader by default", function () {
            $interceptor = Interceptor::patch(['cachePath' => $this->cachePath]);
Exemplo n.º 8
0
         });
         $this->suite->run();
         expect($describe->exectuted)->toEqual(['it' => 2]);
         expect($this->suite->total())->toBe(3);
         expect($this->suite->enabled())->toBe(2);
         expect($describe->children()[1]->excluded())->toBe(true);
         expect($this->suite->status())->toBe(0);
         expect($this->suite->passed())->toBe(true);
     });
 });
 describe("skipIf", function () {
     it("skips specs in a before", function () {
         $describe = $this->suite->describe("skip suite", function () {
             $this->exectuted = ['it' => 0];
             beforeAll(function () {
                 skipIf(true);
             });
             $this->it("an it", function () {
                 $this->exectuted['it']++;
             });
             $this->it("an it", function () {
                 $this->exectuted['it']++;
             });
         });
         $reporters = Double::instance();
         expect($reporters)->toReceive('dispatch')->with('start', ['total' => 2])->ordered;
         expect($reporters)->toReceive('dispatch')->with('suiteStart', $describe)->ordered;
         expect($reporters)->toReceive('dispatch')->with('specStart', Arg::toBeAnInstanceOf('Kahlan\\Specification'))->ordered;
         expect($reporters)->toReceive('dispatch')->with('specEnd', Arg::toBeAnInstanceOf('Kahlan\\Log'))->ordered;
         expect($reporters)->toReceive('dispatch')->with('specStart', Arg::toBeAnInstanceOf('Kahlan\\Specification'))->ordered;
         expect($reporters)->toReceive('dispatch')->with('specEnd', Arg::toBeAnInstanceOf('Kahlan\\Log'))->ordered;
Exemplo n.º 9
0
use Kahlan\Plugin\Monkey;
function defineGlobals($config = [])
{
    $defaults = ['SERVER' => ['REQUEST_URI' => '/base/path/webroot/index.php/app?get=value', 'SCRIPT_NAME' => '/base/path/webroot/index.php'], 'GET' => ['get' => 'value'], 'POST' => ['post' => 'value'], 'FILES' => ['file' => ['name' => 'file.jpg', 'type' => 'image/jpeg', 'tmp_name' => '/private/var/tmp/phpows38J', 'error' => 0, 'size' => 418]]];
    $config = Set::merge($defaults, $config);
    $_SERVER = $config['SERVER'];
    $_GET = $config['GET'];
    $_POST = $config['POST'];
    $_FILES = $config['FILES'];
    return $config;
}
describe("Request", function () {
    beforeAll(function () {
        $this->globalNames = ['_GET', '_POST', '_SERVER'];
        $this->oldEnv = [];
        foreach ($this->globalNames as $varname) {
            $this->oldEnv[$varname] = $GLOBALS[$varname];
            unset($GLOBALS[$varname]);
        }
    });
    afterEach(function () {
        foreach ($this->globalNames as $varname) {
            $GLOBALS[$varname] = $this->oldEnv[$varname];
        }
    });
    describe("__construct", function () {
        it("sets default values", function () {
            $request = new Request();
            expect($request->export())->toEqual(['basePath' => '', 'locale' => null, 'data' => [], 'params' => [], 'env' => $request->env, 'method' => 'GET', 'scheme' => 'http', 'version' => '1.1', 'host' => 'localhost', 'port' => 80, 'path' => '/', 'query' => '', 'fragment' => '', 'username' => null, 'password' => null, 'url' => 'http://localhost/', 'stream' => $request->stream()]);
            $expected = <<<EOD
Host: localhost
Connection: Close
Exemplo n.º 10
0
             });
             $double::generateAbstractMethods('some\\unexisting\\Class');
         })->toThrow();
     });
 });
 describe("::create()", function () {
     beforeAll(function () {
         $this->is_method_exists = function ($instance, $method, $type = "public") {
             if (!method_exists($instance, $method)) {
                 return false;
             }
             $refl = new ReflectionMethod($instance, $method);
             switch ($type) {
                 case "static":
                     return $refl->isStatic();
                     break;
                 case "public":
                     return $refl->isPublic();
                     break;
                 case "private":
                     return $refl->isPrivate();
                     break;
             }
             return false;
         };
     });
     it("stubs an instance", function () {
         $double = Double::instance();
         expect(is_object($double))->toBe(true);
         expect(get_class($double))->toMatch("/^Kahlan\\\\Spec\\\\Plugin\\\\Double\\\\Double\\d+\$/");
     });
     it("names a stub instance", function () {
Exemplo n.º 11
0
namespace Kahlan\Spec\Suite\Cli;

use Exception;
use Kahlan\Jit\Interceptor;
use Kahlan\Filter\Filter;
use Kahlan\Suite;
use Kahlan\Matcher;
use Kahlan\Cli\Kahlan;
use Kahlan\Plugin\Quit;
describe("Kahlan", function () {
    /**
     * Save current & reinitialize the Interceptor class.
     */
    beforeAll(function () {
        $this->previous = Interceptor::instance();
        Interceptor::unpatch();
    });
    /**
     * Restore Interceptor class.
     */
    afterAll(function () {
        Interceptor::load($this->previous);
    });
    beforeEach(function () {
        $this->specs = new Kahlan(['autoloader' => Interceptor::composer()[0], 'suite' => new Suite(['matcher' => new Matcher()])]);
        $this->console = $this->specs->terminal();
    });
    describe("->loadConfig()", function () {
        it("sets passed arguments to specs", function () {
            $argv = ['--src=src', '--spec=spec/Fixture/Kahlan/Spec', '--pattern=*MySpec.php', '--reporter=verbose', '--coverage=3', '--config=spec/Fixture/Kahlan/kahlan-config.php', '--ff=5', '--cc', '--no-colors', '--no-header', '--include=*', '--exclude=Kahlan\\', '--persistent=false', '--autoclear=Kahlan\\Plugin\\Monkey', '--autoclear=Kahlan\\Plugin\\Call', '--autoclear=Kahlan\\Plugin\\Stub', '--autoclear=Kahlan\\Plugin\\Quit'];
            $this->specs->loadConfig($argv);