Skip to content

Commit

Permalink
Re-factor dispatcher asset test to use a data provider.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Nov 10, 2011
1 parent 767d1af commit bc40a55
Showing 1 changed file with 102 additions and 98 deletions.
200 changes: 102 additions & 98 deletions lib/Cake/Test/Case/Routing/DispatcherTest.php
Expand Up @@ -1191,113 +1191,117 @@ public function testAssets() {
} catch (MissingControllerException $e) {
$this->assertEquals('Controller class ThemeController could not be found.', $e->getMessage());
}
}

ob_start();
$Dispatcher->dispatch(new CakeRequest('theme/test_theme/flash/theme_test.swf'), $response);
$result = ob_get_clean();

$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'webroot' . DS . 'flash' . DS . 'theme_test.swf');
$this->assertEqual($file, $result);
$this->assertEqual('this is just a test to load swf file from the theme.', $result);

ob_start();
$Dispatcher->dispatch(new CakeRequest('theme/test_theme/pdfs/theme_test.pdf'), $response);
$result = ob_get_clean();
$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'webroot' . DS . 'pdfs' . DS . 'theme_test.pdf');
$this->assertEqual($file, $result);
$this->assertEqual('this is just a test to load pdf file from the theme.', $result);

ob_start();
$Dispatcher->dispatch(new CakeRequest('theme/test_theme/img/test.jpg'), $response);
$result = ob_get_clean();
$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'View' . DS . 'Themed' . DS . 'TestTheme' . DS . 'webroot' . DS . 'img' . DS . 'test.jpg');
$this->assertEqual($file, $result);

ob_start();
$Dispatcher->asset('theme/test_theme/css/test_asset.css', $response);
$result = ob_get_clean();
$this->assertEqual('this is the test asset css file', $result);

ob_start();
$Dispatcher->asset('theme/test_theme/js/theme.js', $response);
$result = ob_get_clean();
$this->assertEqual('root theme js file', $result);

ob_start();
$Dispatcher->asset('theme/test_theme/js/one/theme_one.js', $response);
$result = ob_get_clean();
$this->assertEqual('nested theme js file', $result);

ob_start();
$Dispatcher->asset('test_plugin/root.js', $response);
$result = ob_get_clean();
$expected = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot' . DS . 'root.js');
$this->assertEqual($expected, $result);

ob_start();
$Dispatcher->dispatch(new CakeRequest('test_plugin/flash/plugin_test.swf'), $response);
$result = ob_get_clean();
$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot' . DS . 'flash' . DS . 'plugin_test.swf');
$this->assertEqual($file, $result);
$this->assertEqual('this is just a test to load swf file from the plugin.', $result);

ob_start();
$Dispatcher->dispatch(new CakeRequest('test_plugin/pdfs/plugin_test.pdf'), $response);
$result = ob_get_clean();
$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS . 'webroot' . DS . 'pdfs' . DS . 'plugin_test.pdf');
$this->assertEqual($file, $result);
$this->assertEqual('this is just a test to load pdf file from the plugin.', $result);

ob_start();
$Dispatcher->asset('test_plugin/js/test_plugin/test.js', $response);
$result = ob_get_clean();
$this->assertEqual('alert("Test App");', $result);

ob_start();
$Dispatcher->asset('test_plugin/js/test_plugin/test.js', $response);
$result = ob_get_clean();
$this->assertEqual('alert("Test App");', $result);

ob_start();
$Dispatcher->asset('test_plugin/css/test_plugin_asset.css', $response);
$result = ob_get_clean();
$this->assertEqual('this is the test plugin asset css file', $result);
/**
* Data provider for asset()
*
* - theme assets.
* - plugin assets.
* - plugin assets in sub directories.
* - unknown plugin assets.
*
* @return array
*/
public static function assetProvider() {
return array(
array(
'theme/test_theme/flash/theme_test.swf',
'View/Themed/TestTheme/webroot/flash/theme_test.swf'
),
array(
'theme/test_theme/pdfs/theme_test.pdf',
'View/Themed/TestTheme/webroot/pdfs/theme_test.pdf'
),
array(
'theme/test_theme/img/test.jpg',
'View/Themed/TestTheme/webroot/img/test.jpg'
),
array(
'theme/test_theme/css/test_asset.css',
'View/Themed/TestTheme/webroot/css/test_asset.css'
),
array(
'theme/test_theme/js/theme.js',
'View/Themed/TestTheme/webroot/js/theme.js'
),
array(
'theme/test_theme/js/one/theme_one.js',
'View/Themed/TestTheme/webroot/js/one/theme_one.js'
),
array(
'test_plugin/root.js',
'Plugin/TestPlugin/webroot/root.js'
),
array(
'test_plugin/flash/plugin_test.swf',
'Plugin/TestPlugin/webroot/flash/plugin_test.swf'
),
array(
'test_plugin/pdfs/plugin_test.pdf',
'Plugin/TestPlugin/webroot/pdfs/plugin_test.pdf'
),
array(
'test_plugin/js/test_plugin/test.js',
'Plugin/TestPlugin/webroot/js/test_plugin/test.js'
),
array(
'test_plugin/css/test_plugin_asset.css',
'Plugin/TestPlugin/webroot/css/test_plugin_asset.css'
),
array(
'test_plugin/img/cake.icon.gif',
'Plugin/TestPlugin/webroot/img/cake.icon.gif'
),
array(
'plugin_js/js/plugin_js.js',
'Plugin/PluginJs/webroot/js/plugin_js.js'
),
array(
'plugin_js/js/one/plugin_one.js',
'Plugin/PluginJs/webroot/js/one/plugin_one.js'
),
array(
'test_plugin/css/unknown.extension',
'Plugin/TestPlugin/webroot/css/unknown.extension'
),
array(
'test_plugin/css/theme_one.htc',
'Plugin/TestPlugin/webroot/css/theme_one.htc'
),
);
}

ob_start();
$Dispatcher->asset('test_plugin/img/cake.icon.gif', $response);
$result = ob_get_clean();
$file = file_get_contents(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' .DS . 'webroot' . DS . 'img' . DS . 'cake.icon.gif');
$this->assertEqual($file, $result);
/**
* Test assets
*
* @dataProvider assetProvider
* @outputBuffering enabled
* @return void
*/
public function testAsset($url, $file) {
Router::reload();

ob_start();
$Dispatcher->asset('plugin_js/js/plugin_js.js', $response);
$result = ob_get_clean();
$expected = "alert('win sauce');";
$this->assertEqual($expected, $result);
App::build(array(
'Plugin' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Plugin' . DS),
'Vendor' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'Vendor'. DS),
'View' => array(CAKE . 'Test' . DS . 'test_app' . DS . 'View'. DS)
));
CakePlugin::loadAll();

ob_start();
$Dispatcher->asset('plugin_js/js/one/plugin_one.js', $response);
$result = ob_get_clean();
$expected = "alert('plugin one nested js file');";
$this->assertEqual($expected, $result);
$Dispatcher = new TestDispatcher();
$response = $this->getMock('CakeResponse', array('_sendHeader'));

ob_start();
$Dispatcher->asset('test_plugin/css/unknown.extension', $response);
$Dispatcher->dispatch(new CakeRequest($url), $response);
$result = ob_get_clean();
$this->assertEqual('Testing a file with unknown extension to mime mapping.', $result);

ob_start();
$Dispatcher->asset('test_plugin/css/theme_one.htc', $response);
$result = ob_get_clean();
$this->assertEqual('htc file', $result);
$path = CAKE. 'Test' . DS . 'test_app' . DS . str_replace('/', DS, $file);
$file = file_get_contents($path);
$this->assertEquals($file, $result);

$response = $this->getMock('CakeResponse', array('_sendHeader'));
ob_start();
$Dispatcher->asset('test_plugin/css/unknown.extension', $response);
ob_end_clean();
$expected = filesize(CakePlugin::path('TestPlugin') . 'webroot' . DS . 'css' . DS . 'unknown.extension');
$expected = filesize($path);
$headers = $response->header();
$this->assertEqual($expected, $headers['Content-Length']);
$this->assertEquals($expected, $headers['Content-Length']);
}

/**
Expand Down

0 comments on commit bc40a55

Please sign in to comment.