protected function _before() { if (!extension_loaded('MongoDB')) { $this->markTestSkipped("MongoDB extension not loaded, test skipped"); return; } Di::reset(); $di = new DI(); $di->set('mongo', function () { $mongo = new MongoClient('mongodb://' . TEST_MONGODB_HOST . ':' . TEST_MONGODB_PORT); return $mongo->selectDatabase('phalcon_test'); }); $di->set('collectionManager', function () { return new Manager(); }); }
/** * @expectedException \Phalcon\Mvc\Model\Exception * @expectedExceptionMessage Field name must be a string */ public function testValidateIncorrectFieldType() { $di = new DI(); $di->set('modelsManager', new Manager()); require_once __DIR__ . '/resources/TestCardNumberIncorrectField.php'; $obj = new \TestCardNumberIncorrectField(); $obj->validation(); }
public function testHasKey() { $di = new DI(); $di->setShared('date', function (DI $di) { return uniqid(); }); $di->set('time', function (DI $di) { return time(); }); $this->assertTrue($di->hasKey('date')); $this->assertTrue($di->hasKey('time')); $this->assertFalse($di->hasKey('a_object')); }
/** * Tests the standard set/get methods of the DI. * @dataProvider setAndGetServiceProvider */ public function testSetAndGetService($key, $element, $expected) { $di = new DI(); $di->set($key, $element); // check that we get back what we expect $this->assertEquals($expected, $di->get($key, false)); // check we get the same value if we use the cache $this->assertEquals($expected, $di->get($key, true)); // and again if we force a "no cache" hit $this->assertEquals($expected, $di->get($key, false)); $this->assertTrue($di->hasElement($key)); $this->assertEquals(array($key), $di->allRegisteredElements()); }
* ArrayAccess接口,以$di[$name]=$value方式注册服务,非共享 * @param [type] $offset [description] * @param [type] $value [description] * @return [type] [description] */ public function offsetSet($offset, $value) { return self::set($offset, $value); } /** * ArrayAccess接口,以unset($di[$name])方式卸载服务 * @param [type] $offset [description] * @return [type] [description] */ public function offsetUnset($offset) { return self::remove($offset); } } class A { public function abc() { echo 'a'; } } $di = new DI(); $di->set('B', function () { return new A(); }); var_dump($di->get('B'));
<?php use Phalcon\Loader; use Phalcon\Mvc\View; use Phalcon\Db\Adapter\Pdo\Mysql; /** * Very simple MVC structure */ $loader = new Loader(); $loader->registerDirs(array('../apps/controllers/', '../apps/models/')); $loader->register(); $di = new DI(); //Registering a router $di->set('router', 'Phalcon\\Mvc\\Router'); //Registering a dispatcher $di->set('dispatcher', 'Phalcon\\Mvc\\Dispatcher'); //Registering a Http\Response $di->set('response', 'Phalcon\\Http\\Response'); //Registering a Http\Request $di->set('request', 'Phalcon\\Http\\Request'); //Registering the view component $di->set('view', function () { $view = new View(); $view->setViewsDir('../apps/views/'); return $view; }); $di->set('db', function () { return new Database(array("host" => "localhost", "username" => "root", "password" => "", "dbname" => "invo")); }); //Registering the Models-Metadata $di->set('modelsMetadata', 'Phalcon\\Mvc\\Model\\Metadata\\Memory');