/** * Test that factory returns the correct object for the supplied namespace. */ public function testFactory() { $this->assertInstanceOf('Titon\\Common\\Base', Registry::factory('Titon\\Common\\Base', [], false)); $this->assertInstanceOf('Titon\\Common\\Base', Registry::factory('Titon/Common/Base', [], false)); $this->assertInstanceOf('Titon\\Common\\Base', Registry::factory('Titon\\Common\\Base', [], false)); $this->assertInstanceOf('Titon\\Common\\Base', Registry::factory('/Titon/Common/Base', [], false)); }
/** * Test that isMatch() returns a valid response. * Test that getParam() returns a single value, or all values. * Test that url() returns the current URL. */ public function testIsMatchAndParamAndUrl() { $g11n = Registry::factory('Titon\\G11n\\G11n'); $g11n->addLocale(new Locale('en_US')); $g11n->useLocale('en_US'); $url = '/en-us/blog/2012/02/26'; $route = new LocaleRoute('/blog/[year]/[month]/[day]', ['module' => 'blog', 'controller' => 'api', 'action' => 'archives', 'custom' => 'value']); $this->assertTrue($route->isMatch($url)); $this->assertEquals(['ext' => '', 'module' => 'blog', 'controller' => 'api', 'action' => 'archives', 'query' => [], 'args' => [], 'year' => 2012, 'month' => 2, 'day' => 26, 'custom' => 'value', 'locale' => 'en-us'], $route->getParams()); $this->assertEquals('blog', $route->getParam('module')); $this->assertEquals('archives', $route->getParam('action')); $this->assertEquals(2012, $route->getParam('year')); $this->assertEquals(2, $route->getParam('month')); $this->assertEquals(26, $route->getParam('day')); $this->assertEquals('value', $route->getParam('custom')); $this->assertEquals(null, $route->getParam('foobar')); $this->assertEquals('en-us', $route->getParam('locale')); $this->assertEquals($url, $route->url()); // module, controller, action $url = '/en/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertTrue($route->isMatch($url)); $this->assertEquals(['ext' => '', 'module' => 'forum', 'controller' => 'topic', 'action' => 'view', 'query' => [], 'args' => [123], 'locale' => 'en'], $route->getParams()); // invalid locale $url = '/foo-bar/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertFalse($route->isMatch($url)); // no locale $url = '/forum/topic/view/123'; $route = new LocaleRoute('/{module}/{controller}/{action}'); $this->assertFalse($route->isMatch($url)); }
/** * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); $this->object = Registry::factory('Titon\\G11n\\G11n'); $this->object->addLocale(new Locale('en')); $this->object->useLocale('en'); }
/** * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); $this->object = Registry::factory('Titon\\G11n\\G11n'); $_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'ex-no,ex;q=0.5'; foreach (['ex', 'en'] as $code) { $this->object->addLocale(new Locale($code)); } }
/** * This method is called before a test is executed. */ protected function setUp() { parent::setUp(); $translator = new MessageTranslator(); $translator->setReader(new PhpReader()); $this->object = Registry::factory('Titon\\G11n\\G11n'); $this->object->addLocale(new Locale('en')); $this->object->useLocale('en'); $this->object->setTranslator($translator); }
/** * Return a new instance of the current class and store it in registry. * * @return $this */ public static function registry() { return Registry::factory(get_called_class(), func_get_args()); }
/** * Primary method to detect if the object being called can be returned; based on restrictions and instantiation. * If an object is not instantiated, it will create it based off the Closure (if applicable) or the options namespace. * * @uses Titon\Common\Registry * * @param string $class * @return object * @throws \Titon\Common\Exception\MissingObjectException * @throws \Titon\Common\Exception\UnsupportedInterfaceException */ public function &getObject($class) { if (isset($this->_attached[$class])) { return $this->_attached[$class]; } else { if (!isset($this->_classes[$class])) { throw new MissingObjectException(sprintf('No object attachment could be found for %s', $class)); } } // Gather options $options = $this->_classes[$class]; // Lazy-load the object if (isset($this->__objectMap[$class])) { $object = $this->__objectMap[$class](); $this->_classes[$class]['class'] = get_class($object); // Create manually } else { if ($options['register']) { $object = Registry::factory($options['class']); } else { $object = new $options['class'](); } } if ($options['interface'] && !$object instanceof $options['interface']) { throw new UnsupportedInterfaceException(sprintf('%s does not implement the %s interface', get_class($object), $options['interface'])); } $this->_attached[$class] =& $object; return $this->_attached[$class]; }
function factory($key) { return Registry::factory($key); }