Skip to content

Commit

Permalink
Add filter to manager so CSS and JS resources are not mixed when outp…
Browse files Browse the repository at this point in the history
…uting by type
  • Loading branch information
ninjapanzer committed Jun 21, 2017
1 parent ddb7576 commit c1e399a
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
14 changes: 13 additions & 1 deletion phalcon/assets/manager.zep
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,18 @@ class Manager
return collection;
}

public function collectionResourcesByType(array resources, string type) -> array
{
var $filtered = [], $resource;
for $resource in resources {
if $resource->getType() == type {
let $filtered[] = $resource;
}
}

return $filtered;
}

/**
* Traverses a collection calling the callback to generate its HTML
*
Expand All @@ -310,7 +322,7 @@ class Manager
/**
* Get the resources as an array
*/
let resources = collection->getResources();
let resources = this->collectionResourcesByType(collection->getResources(), type);

/**
* Get filters in the collection
Expand Down
80 changes: 80 additions & 0 deletions tests/unit/Assets/ManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,44 @@ function () {
);
}

/**
* Tests addCss and addJs
*
* @author Paul Scarrone <[email protected]>
* @since 2017-06-20
*/
public function testAssetsManagerAddingCssAndJs()
{
$this->specify(
"The combination of addCss and addJs on assets manager does add resources correctly",
function () {
$assets = new Manager();

$assets->addCss('/css/style1.css');
$assets->addCss('/css/style2.css');
$assets->addJs('/js/script1.js');
$assets->addJs('/js/script2.js');

$collectionCss = $assets->get('css');
$collectionJs = $assets->get('js');

$CSSnumber = 0;
foreach ($collectionCss as $resource) {
expect('css')->equals($resource->getType());
$CSSnumber++;
}
expect($CSSnumber)->equals(2);

$JSnumber = 0;
foreach ($collectionJs as $resource) {
expect('js')->equals($resource->getType());
$JSnumber++;
}
expect($JSnumber)->equals(2);
}
);
}

/**
* Tests addJs
*
Expand Down Expand Up @@ -374,6 +412,48 @@ function () {
);
}

/**
* Tests collection with mixed resources
*
* @author Paul Scarrone <[email protected]>
* @since 2017-06-20
*/
public function testAssetsManagerOutputJsWithMixedResourceCollection()
{
$this->specify(
"The outputJs using a mixed resource collection returns only JS resources",
function () {
$assets = new Manager();

$assets->collection('header')
->setPrefix('http:://cdn.example.com/')
->setLocal(false)
->addJs('js/script1.js')
->addJs('js/script2.js')
->addCss('css/styles1.css')
->addCss('css/styles2.css');
$assets->useImplicitOutput(false);

$actualJS = $assets->outputJs('header');
$expectedJS = sprintf(
"%s\n%s\n",
'<script type="text/javascript" src="http:://cdn.example.com/js/script1.js"></script>',
'<script type="text/javascript" src="http:://cdn.example.com/js/script2.js"></script>'
);
expect($actualJS)->equals($expectedJS);

$actualCSS = $assets->outputCss('header');
$expectedCSS = sprintf(
"%s\n%s\n",
'<link rel="stylesheet" type="text/css" href="http:://cdn.example.com/css/styles1.css" />',
'<link rel="stylesheet" type="text/css" href="http:://cdn.example.com/css/styles2.css" />'
);
expect($actualCSS)->equals($expectedCSS);
}
);
}


/**
* Tests setting local target
*
Expand Down

0 comments on commit c1e399a

Please sign in to comment.