Permalink
Browse files

New unit tests

  • Loading branch information...
1 parent 3dbfd24 commit 68a4de7cf8a4579b1e47b64bd966670f5eb874a6 @kmontag kmontag committed Sep 27, 2011
@@ -47,6 +47,7 @@ public function getConfigTreeBuilder()
->defaultValue('js')
->end()
->scalarNode('base_directory')
+ ->cannotBeEmpty()
->defaultValue('@HearsayRequireJSBundle/Resources/scripts')
->end()
->arrayNode('namespaces')
@@ -57,7 +57,7 @@ public function load(array $configs, ContainerBuilder $container)
$value = $settings['value'];
$container->getDefinition('hearsay_require_js.optimizer_filter')->addMethodCall('setOption', array($name, $value));
}
-
+
// Add the configured namespaces
foreach ($config['namespaces'] as $namespace => $settings) {
$this->addNamespaceMapping($settings['path'], $namespace, $container);
@@ -80,9 +80,11 @@ protected function addNamespaceMapping($path, $namespace, ContainerBuilder $cont
// Register the namespace with the configuration
$mapping = $container->getDefinition('hearsay_require_js.namespace_mapping');
$mapping->addMethodCall('registerNamespace', array($path, $namespace));
-
+
// And with the optimizer filter
- $container->getDefinition('hearsay_require_js.optimizer_filter')->addMethodCall('setOption', array('paths.' . $namespace, $path));
+ if ($namespace) {
+ $container->getDefinition('hearsay_require_js.optimizer_filter')->addMethodCall('setOption', array('paths.' . $namespace, $path));
+ }
// Create the assetic resource
$resource = new DefinitionDecorator('hearsay_require_js.directory_filename_resource');
@@ -101,11 +103,9 @@ protected function addNamespaceMapping($path, $namespace, ContainerBuilder $cont
*/
private function getRealPath($path, ContainerBuilder $container)
{
- // Get bundles so we can match up bundle notation
- $bundles = $container->getParameter('kernel.bundles');
-
// Expand bundle notation (snagged from the Assetic bundle)
if ($path[0] == '@' && strpos($path, '/') !== false) {
+
// Extract the bundle name and the directory within the bundle
$bundle = substr($path, 1);
$directory = '';
@@ -114,6 +114,9 @@ private function getRealPath($path, ContainerBuilder $container)
$bundle = substr($bundle, 0, $pos);
}
+ // Get loaded bundles
+ $bundles = $container->getParameter('kernel.bundles');
+
// Reconstruct the path
if (isset($bundles[$bundle])) {
$rc = new \ReflectionClass($bundles[$bundle]);
@@ -125,5 +128,5 @@ private function getRealPath($path, ContainerBuilder $container)
return $path;
}
-
+
}
@@ -52,13 +52,7 @@ class RequireJSOptimizerFilter implements FilterInterface
*/
protected $baseUrl = null;
/**
- * Plugin to apply when processing content.
- * @var string
- */
- protected $plugin = null;
- /**
- * Extension to add to temporary files for processing (generally dependent
- * on the plugin being used).
+ * Extension to add to temporary files for processing.
* @var string
*/
protected $extension = 'js';
@@ -86,18 +80,6 @@ public function setOption($name, $value)
}
/**
- * Set the plugin to apply to content before processing.
- * @param string $plugin The plugin module name, or null for no plugin.
- * @param string $extension The file extension to add to temporary files, to
- * enable them to be loaded by this plugin.
- */
- public function setPlugin($plugin, $extension)
- {
- $this->plugin = $plugin;
- $this->extension = $extension;
- }
-
- /**
* {@inheritdoc}
*/
public function filterLoad(AssetInterface $asset)
@@ -111,7 +93,8 @@ public function filterLoad(AssetInterface $asset)
public function filterDump(AssetInterface $asset)
{
$input = tempnam(sys_get_temp_dir(), 'assetic_requirejs');
- file_put_contents($input . '.' . $this->extension, $asset->getContent());
+ $inputFilename = $input . ($this->extension ? '.' . $this->extension : '');
+ file_put_contents($inputFilename, $asset->getContent());
$output = tempnam(sys_get_temp_dir(), 'assetic_requirejs');
@@ -124,7 +107,7 @@ public function filterDump(AssetInterface $asset)
->add('-o') // Optimize
// Configure the primary input
->add('paths.' . $name . '=' . $input)
- ->add('name=' . ($this->plugin ? $this->plugin . '!' : '') . $name)
+ ->add('name=' . $name)
// Configure the output
->add('out=' . $output)
View
@@ -1,9 +1,36 @@
<?php
+/**
+ * Copyright (c) 2011 Hearsay News Products, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
namespace Hearsay\RequireJSBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
+/**
+ * Bundle providing RequireJS integration.
+ * @author Kevin Montag <kevin@hearsay.it>
+ * @codeCoverageIgnore
+ */
class HearsayRequireJSBundle extends Bundle
{
}
@@ -25,9 +25,8 @@
namespace Hearsay\RequireJSBundle\Templating\Helper;
use Hearsay\RequireJSBundle\Configuration\ConfigurationBuilder;
-use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
+use Symfony\Component\Templating\EngineInterface;
use Symfony\Component\Templating\Helper\Helper;
-use Symfony\Component\Translation\TranslatorInterface;
/**
* Templating helper for RequireJS inclusion.
@@ -0,0 +1,50 @@
+<?php
+
+/**
+ * Copyright (c) 2011 Hearsay News Products, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+namespace Hearsay\RequireJSBundle\Tests\Configuration;
+
+use Hearsay\RequireJSBundle\Configuration\ConfigurationBuilder;
+
+/**
+ * Unit tests for the helper to generate RequireJS configuration.
+ * @author Kevin Montag <kevin@hearsay.it>
+ */
+class ConfigurationBuilderTest extends \PHPUnit_Framework_TestCase
+{
+ public function testConfigurationGenerated()
+ {
+ $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
+ $translator->expects($this->any())
+ ->method('getLocale')
+ ->will($this->returnValue('fr_FR'));
+ $builder = new ConfigurationBuilder($translator, 'js', array('option' => 'value'));
+
+ $expected = array(
+ 'locale' => 'fr_FR',
+ 'baseUrl' => 'js',
+ 'option' => 'value',
+ );
+ $this->assertEquals($expected, $builder->getConfiguration(), 'Unexpected configuration generated');
+ }
+}
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * Copyright (c) 2011 Hearsay News Products, Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+namespace Hearsay\RequireJSBundle\Tests\Configuration;
+
+use Hearsay\RequireJSBundle\Configuration\NamespaceMapping;
+
+/**
+ * Unit tests for the namespace mapping container.
+ * @author Kevin Montag <kevin@hearsay.it>
+ */
+class NamespaceMappingTest extends \PHPUnit_Framework_TestCase
+{
+
+ public function testFilesConvertedToModules()
+ {
+ $mapping = new NamespaceMapping('js');
+ $mapping->registerNamespace(__DIR__ . '/dir', 'modules');
+
+ $this->assertEquals('js/modules/file.js', $mapping->getModulePath(__DIR__ . '/dir/file.js'), 'Incorrect file-to-module conversion');
+ }
+
+ public function testExtraSlashesIgnored()
+ {
+ $mapping = new NamespaceMapping('js//');
+ $mapping->registerNamespace(__DIR__ . '/dir', '/modules/');
+
+ $this->assertEquals('js/modules/file.js', $mapping->getModulePath(__DIR__ . '/dir/file.js'), 'Incorrect file-to-module conversion');
+ }
+
+ public function testRelativePathsReduced()
+ {
+ $mapping = new NamespaceMapping('js');
+ $mapping->registerNamespace(__DIR__ . '/dir/../dir', 'modules');
+
+ $this->assertEquals('js/modules/file.js', $mapping->getModulePath(__DIR__ . '/../Configuration/dir/file.js'), 'Incorrect file-to-module conversion');
+ }
+
+ public function testNonexistentNamespaceReturnsFalse()
+ {
+ $mapping = new NamespaceMapping('js');
+
+ $this->assertFalse($mapping->getModulePath(__DIR__ . '/dir/file.js'), 'Non-existent module expected not to be converted');
+ }
+
+}
No changes.
Oops, something went wrong.

0 comments on commit 68a4de7

Please sign in to comment.