Exemple #1
0
    public function testAssetsManager()
    {
        $assets = new Phalcon\Assets\Manager();
        $assets->addCss('/css/style1.css');
        $assets->addCss('/css/style2.css');
        $collection = $assets->get('css');
        $number = 0;
        foreach ($collection as $resource) {
            $this->assertEquals($resource->getType(), 'css');
            $number++;
        }
        $this->assertEquals($number, 2);
        $assets->addJs('/js/scripts1.js');
        $assets->addJs('/js/scripts2.js');
        $collection = $assets->get('js');
        $number = 0;
        foreach ($collection as $resource) {
            $this->assertEquals($resource->getType(), 'js');
            $number++;
        }
        $this->assertEquals($number, 2);
        $assets->addResource(new Phalcon\Assets\Resource\Css('/css/style.css', false));
        $this->assertEquals(count($assets->get('css')), 3);
        Phalcon\DI::reset();
        //Create a default DI
        $di = new Phalcon\DI();
        $di->set('url', function () {
            $url = new Phalcon\Mvc\Url();
            $url->setBaseUri('/');
            return $url;
        });
        $di->set('escaper', function () {
            return new \Phalcon\Escaper();
        });
        //With implicit output
        ob_start();
        $assets->outputCss();
        $html = ob_get_clean();
        $this->assertEquals($html, '<link rel="stylesheet" href="//css/style1.css" type="text/css" />
<link rel="stylesheet" href="//css/style2.css" type="text/css" />
<link rel="stylesheet" href="/css/style.css" type="text/css" />' . PHP_EOL);
        ob_start();
        $assets->outputJs();
        $html = ob_get_clean();
        $this->assertEquals($html, '<script src="//js/scripts1.js" type="text/javascript"></script>
<script src="//js/scripts2.js" type="text/javascript"></script>' . PHP_EOL);
        //Without implicit output
        $assets->useImplicitOutput(false);
        $html = $assets->outputCss();
        $this->assertEquals($html, '<link rel="stylesheet" href="//css/style1.css" type="text/css" />
<link rel="stylesheet" href="//css/style2.css" type="text/css" />
<link rel="stylesheet" href="/css/style.css" type="text/css" />' . PHP_EOL);
        $html = $assets->outputJs();
        $this->assertEquals($html, '<script src="//js/scripts1.js" type="text/javascript"></script>
<script src="//js/scripts2.js" type="text/javascript"></script>' . PHP_EOL);
        //Test collections
        $assets->collection('footer')->addCss('css/style-1.css');
        $footer = $assets->collection('footer');
        $footer->addCss('css/style-2.css');
        $html = $assets->outputCss('footer');
        $this->assertEquals($html, '<link rel="stylesheet" href="/css/style-1.css" type="text/css" />
<link rel="stylesheet" href="/css/style-2.css" type="text/css" />' . PHP_EOL);
        $scripts = $assets->collection('header')->setPrefix('http:://cdn.example.com/')->setLocal(false)->addJs('js/jquery.js')->addJs('js/bootstrap.min.js');
        $html = $assets->outputJs('header');
        $this->assertEquals($html, '<script src="http:://cdn.example.com/js/jquery.js" type="text/javascript"></script>
<script src="http:://cdn.example.com/js/bootstrap.min.js" type="text/javascript"></script>' . PHP_EOL);
    }