/** * 创建一个新实例 * * @param array $filters * <p> * <pre> * array('username'=>array('StripTags')) * </pre> * </p> * @param array $validate * <p> * <pre> * array('username'=>array('Required'=>'can not be empty')) * </pre> * </p> * @param array $data */ public function __construct($filters, $validate, $data) { $this->filters = array(); $this->validators = array(); //添加过滤器 foreach ($filters as $field => $filter_name) { $filter_tmp = new Filter(); foreach ($filter_name as $name) { $filter_name = 'filter\\' . $filter_name; if (class_exists($filter_name)) { $filter_tmp->addFilter(new $filter_name()); } } $this->filters[$field] = $filter_tmp; } //添加验证器 foreach ($validate as $field => $rules) { $validator_tmp = new Validator(); foreach ($rules as $rule => $params) { $rule_name = 'validator\\' . $rule; if (class_exists($rule_name)) { $obj = new $rule_name($params); $validator_tmp->addValidator($obj); } } $this->validators[$field] = $validator_tmp; } $this->filter($data); }
/** * Boot the service * Registers a singleton Filter instance */ public function boot() { $this->app->singleton('filter', function ($app) { $filter = new Filter(); $filter->registerDefaultFilters(); return $filter; }); }
public function testIpv6() { $expect = array(null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, '2001:0db8:0000:0000:0000:ff00:0042:8329'); foreach ($this->tests as $index => $value) { $this->assertSame($expect[$index], Filter::ipv6($value), "Test index: {$index}"); } $this->assertSame($expect, call_user_func_array('\\Filter\\Filter::ipv6', $this->tests)); }
public function closure() { \filter\Filter::on(isset($this) ? $this : get_called_class(), __FUNCTION__, func_get_args(), function ($chain) { return function () { $rand = rand(2, 5); $rand++; return $rand; }; }); }
public function testUser() { $filter = Filter::map(["[user]" => ["login,password,confirm" => "Trim,\r\n\t\v", "password" => "Compare,===,,confirm"], "[profile]" => ["first_name,last_name,middle_name" => "Trim;Lower;FirstUpper"]]); $context = $filter->run(["user" => ["login" => "\tlogin\r\n", "password" => "pass\r\n", "confirm" => "pass"], "profile" => ["first_name" => "иван", "last_name" => "иванов ", "middle_name" => " иванович "]]); $this->assertCount(0, $context->errors); $this->assertEquals("Иван", $context->data["profile"]["first_name"]); $this->assertEquals("Иванов", $context->data["profile"]["last_name"]); $this->assertEquals("Иванович", $context->data["profile"]["middle_name"]); $this->assertEquals("login", $context->data["user"]["login"]); $this->assertEquals("pass", $context->data["user"]["password"]); }
public function testToBooleanFilter() { $filter = Filter::map(["to bool"]); $this->assertSame(true, $filter->run("yes")->data[0]); $this->assertSame(true, $filter->run("true")->data[0]); $this->assertSame(true, $filter->run("1")->data[0]); $this->assertSame(true, $filter->run("on")->data[0]); $this->assertSame(false, $filter->run("no")->data[0]); $this->assertSame(false, $filter->run("false")->data[0]); $this->assertSame(false, $filter->run("0")->data[0]); $this->assertSame(false, $filter->run("off")->data[0]); $this->assertSame(false, $filter->run("")->data[0]); $this->assertSame(false, $filter->run("oops")->data[0]); }
public function testBoolean() { $filter = Filter::map([Boolean::create()]); $this->assertCount(0, $filter->run("yes")->errors); $this->assertCount(0, $filter->run("true")->errors); $this->assertCount(0, $filter->run("1")->errors); $this->assertCount(0, $filter->run("on")->errors); $this->assertCount(0, $filter->run("no")->errors); $this->assertCount(0, $filter->run("false")->errors); $this->assertCount(0, $filter->run("0")->errors); $this->assertCount(0, $filter->run("off")->errors); $this->assertCount(0, $filter->run("")->errors); $this->assertCount(1, $filter->run("oops")->errors); }
/** * * @param Closure $op * @param mixed $expected * @param string $with * @param mixed $filter */ public function __construct($op = "?", $expected = null, $with = null, $filter = null) { if ($op instanceof Closure) { $this->op = $op->bindTo($this); } else { $this->op = $op; } $this->expected = $expected; $this->with = $with; if ($filter instanceof Filter) { $this->filter = $filter; } elseif (is_string($filter)) { $this->filter = Filter::map($filter); } elseif (is_array($filter)) { $this->filter = Filter::map($filter); } else { $this->filter = null; } }
public function testRegisterFilterByClassName() { $this->filter->registerFilter('by_class_name', 'Filter\\TestFilter'); $this->assertEquals('TestFilter', $this->filter->filterOne('by_class_name', 'test')); }
<?php use filter\Filter; use kahlan\reporter\Coverage; use kahlan\reporter\coverage\exporter\CodeClimate; $args = $this->args(); $args->argument('coverage', 'default', 3); Filter::register('kahlan.coverage-exporter', function ($chain) { $reporter = $this->reporters()->get('coverage'); if (!$reporter || !getenv('CODECLIMATE_REPO_TOKEN')) { return $chain->next(); } CodeClimate::write(['collector' => $reporter, 'file' => 'codeclimate.json', 'branch' => getenv('TRAVIS_BRANCH') ?: null, 'repo_token' => getenv('CODECLIMATE_REPO_TOKEN')]); return $chain->next(); }); Filter::apply($this, 'reporting', 'kahlan.coverage-exporter'); require_once __DIR__ . '/spec/init.php';
<?php use Doctrine\Common\Annotations\AnnotationRegistry; use filter\Filter; use jit\Interceptor; Filter::register('doctrine.exclude.annotations', function ($chain) { $extra = ['Doctrine\\Common\\Annotations\\Annotation']; $exclude = $this->args()->get('exclude'); $exclude = is_array($exclude) ? $exclude + $extra : $extra; $this->args()->set('exclude', $exclude); return $chain->next(); }); Filter::register('doctrine.annotations.autoloader', function ($chain) { AnnotationRegistry::registerLoader(Interceptor::instance()->loader()); return $chain->next(); }); Filter::apply($this, 'interceptor', 'doctrine.exclude.annotations'); Filter::apply($this, 'run', 'doctrine.annotations.autoloader');
Filter::apply($this->specs, 'load', 'spec.load'); Filter::register('spec.reporters', function ($chain) use(&$order) { $order[] = 'reporters'; }); Filter::apply($this->specs, 'reporters', 'spec.reporters'); Filter::register('spec.matchers', function ($chain) use(&$order) { $order[] = 'matchers'; }); Filter::apply($this->specs, 'matchers', 'spec.matchers'); Filter::register('spec.run', function ($chain) use(&$order) { $order[] = 'run'; }); Filter::apply($this->specs, 'run', 'spec.run'); Filter::register('spec.reporting', function ($chain) use(&$order) { $order[] = 'reporting'; }); Filter::apply($this->specs, 'reporting', 'spec.reporting'); Filter::register('spec.stop', function ($chain) use(&$order) { $order[] = 'stop'; }); Filter::apply($this->specs, 'stop', 'spec.stop'); Filter::register('spec.quit', function ($chain) use(&$order) { $order[] = 'quit'; }); Filter::apply($this->specs, 'quit', 'spec.quit'); $this->specs->run(); expect($order)->toBe(['bootstrap', 'interceptor', 'namespaces', 'patchers', 'load', 'reporters', 'matchers', 'run', 'reporting', 'stop', 'quit']); Interceptor::unpatch(); }); }); });
protected function dump() { \filter\Filter::on(isset($this) ? $this : get_called_class(), __FUNCTION__, func_get_args(), function ($chain) { return String::dump('Hello'); }); }
$chrome_location = getenv('SELENIUM_CHROME_HOST') !== false ? getenv('SELENIUM_CHROME_HOST') : 'localhost'; $firefox_selenium_host = "http://{$firefox_location}:4444/wd/hub"; $chrome_selenium_host = "http://{$chrome_location}:4444/wd/hub"; $mink = new Mink(['firefox' => new Session(new Selenium2Driver('firefox', ['browserName' => 'firefox'], $firefox_selenium_host)), 'chrome' => new Session(new Selenium2Driver('chrome', ['browserName' => 'chrome'], $chrome_selenium_host))]); $mink->setDefaultSessionName($default_browser); /** @var Suite $root */ $root = $this->suite(); $root->mink = $mink; $root->beforeEach(function () use($mink) { $mink->resetSessions(); }); $root->after(function () use($mink) { $mink->stopSessions(); }); return $chain->next(); }); Filter::register('specs.prepare', function ($chain) { $callback = (require_once __DIR__ . '/prepare.php'); /** @var Suite $root */ $root = $this->suite(); $root->before($callback); return $chain->next(); }); Filter::apply($this, 'run', 'mockery.register.close'); Filter::apply($this, 'run', 'mink.register'); Filter::apply($this, 'run', 'symfony.register'); Filter::apply($this, 'interceptor', 'doctrine.exclude.annotations'); Filter::apply($this, 'load', 'specs.default.path'); Filter::apply($this, 'run', 'doctrine.annotations.autoloader'); Filter::apply($this, 'load', 'specs.prepare');
/** * The default `'quit'` filter. */ protected function _quit() { return Filter::on($this, 'quit', [$this->suite()->passed()], function ($chain, $success) { }); }
return $chain->next($message); }); expect(Filter::registered('spec.newclosure'))->toBe(true); }); it("registers a closure with no name", function () { $name = Filter::register(function ($chain, $message) { $message = "My {$message}"; return $chain->next($message); }); expect(Filter::registered($name))->toBe(true); }); }); describe("::unregister()", function () { it("unregisters a closure", function () { Filter::register('spec.newclosure', function ($chain, $message) { $message = "My {$message}"; return $chain->next($message); }); Filter::unregister('spec.newclosure'); expect(Filter::registered('spec.newclosure'))->toBe(false); }); }); describe("::resets()", function () { it("clears all the filters", function () { Filter::reset(); expect(Filter::registered('spec.my_prefix'))->toBe(false); expect(Filter::registered('spec.be_prefix'))->toBe(false); expect(Filter::registered('spec.no_chain'))->toBe(false); }); }); });
<?php use filter\Filter; use kahlan\reporter\coverage\exporter\Coveralls; $args = $this->args(); $args->argument('coverage', 'default', 3); Filter::register('phalcon.namespace', function ($chain) { $this->_autoloader->addPsr4('Spec\\Models\\', __DIR__ . '/spec/models/'); }); Filter::register('phalcon.coverage-exporter', function ($chain) { $reporter = $this->reporters()->get('coverage'); if (!$reporter) { return; } Coveralls::write(['collector' => $reporter, 'file' => 'coveralls.json', 'service_name' => 'travis-ci', 'service_job_id' => getenv('TRAVIS_JOB_ID') ?: null]); return $chain->next(); }); Filter::apply($this, 'namespaces', 'phalcon.namespace'); Filter::apply($this, 'reporting', 'phalcon.coverage-exporter'); $di = new \Phalcon\DI\FactoryDefault(); $di->setShared('db', function () { return new \Phalcon\Db\Adapter\Pdo\Sqlite(['dbname' => __DIR__ . '/spec/db.sqlite']); }); \Phalcon\DI::setDefault($di);