| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Assetic package, an OpenSky project. |
| 5 |
* |
| 6 |
* (c) 2010-2014 OpenSky Project Inc |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Assetic\Test; |
| 13 |
|
| 14 |
use Assetic\Asset\FileAsset; |
| 15 |
use Assetic\AssetWriter; |
| 16 |
|
| 17 |
class AssetWriterTest extends \PHPUnit_Framework_TestCase |
| 18 |
{ |
| 19 |
private $dir; |
| 20 |
/** @var AssetWriter */ |
| 21 |
private $writer; |
| 22 |
|
| 23 |
protected function setUp() |
| 24 |
{ |
| 25 |
$this->dir = sys_get_temp_dir().'/assetic_tests_'.rand(11111, 99999); |
| 26 |
mkdir($this->dir); |
| 27 |
$this->writer = new AssetWriter($this->dir, array( |
| 28 |
'locale' => array('en', 'de', 'fr'), |
| 29 |
'browser' => array('ie', 'firefox', 'other'), |
| 30 |
'gzip' => array('gzip', ''), |
| 31 |
)); |
| 32 |
} |
| 33 |
|
| 34 |
protected function tearDown() |
| 35 |
{ |
| 36 |
array_map('unlink', glob($this->dir.'/*')); |
| 37 |
rmdir($this->dir); |
| 38 |
} |
| 39 |
|
| 40 |
public function testWriteManagerAssets() |
| 41 |
{ |
| 42 |
$asset = $this->getMockBuilder('Assetic\\Asset\\AssetInterface')->getMock(); |
| 43 |
$am = $this->getMockBuilder('Assetic\\AssetManager')->getMock(); |
| 44 |
|
| 45 |
$am->expects($this->once()) |
| 46 |
->method('getNames') |
| 47 |
->will($this->returnValue(array('foo'))); |
| 48 |
$am->expects($this->once()) |
| 49 |
->method('get') |
| 50 |
->with('foo') |
| 51 |
->will($this->returnValue($asset)); |
| 52 |
$asset->expects($this->atLeastOnce()) |
| 53 |
->method('getTargetPath') |
| 54 |
->will($this->returnValue('target_url')); |
| 55 |
$asset->expects($this->once()) |
| 56 |
->method('dump') |
| 57 |
->will($this->returnValue('content')); |
| 58 |
$asset->expects($this->atLeastOnce()) |
| 59 |
->method('getVars') |
| 60 |
->will($this->returnValue(array())); |
| 61 |
$asset->expects($this->atLeastOnce()) |
| 62 |
->method('getValues') |
| 63 |
->will($this->returnValue(array())); |
| 64 |
|
| 65 |
$this->writer->writeManagerAssets($am); |
| 66 |
|
| 67 |
$this->assertFileExists($this->dir.'/target_url'); |
| 68 |
$this->assertEquals('content', file_get_contents($this->dir.'/target_url')); |
| 69 |
} |
| 70 |
|
| 71 |
public function testWriteAssetWithVars() |
| 72 |
{ |
| 73 |
$asset = $this->getMockBuilder('Assetic\Asset\AssetInterface')->getMock(); |
| 74 |
$asset->expects($this->atLeastOnce()) |
| 75 |
->method('getVars') |
| 76 |
->will($this->returnValue(array('locale'))); |
| 77 |
|
| 78 |
$self = $this; |
| 79 |
$expectedValues = array( |
| 80 |
array('locale' => 'en'), |
| 81 |
array('locale' => 'de'), |
| 82 |
array('locale' => 'fr'), |
| 83 |
); |
| 84 |
$asset->expects($this->exactly(3)) |
| 85 |
->method('setValues') |
| 86 |
->will($this->returnCallback(function ($values) use ($self, $expectedValues) { |
| 87 |
static $counter = 0; |
| 88 |
$self->assertEquals($expectedValues[$counter++], $values); |
| 89 |
})); |
| 90 |
$asset->expects($this->exactly(3)) |
| 91 |
->method('getValues') |
| 92 |
->will($this->returnCallback(function () use ($expectedValues) { |
| 93 |
static $counter = 0; |
| 94 |
|
| 95 |
return $expectedValues[$counter++]; |
| 96 |
})); |
| 97 |
|
| 98 |
$asset->expects($this->exactly(3)) |
| 99 |
->method('dump') |
| 100 |
->will($this->onConsecutiveCalls('en', 'de', 'fr')); |
| 101 |
|
| 102 |
$asset->expects($this->atLeastOnce()) |
| 103 |
->method('getTargetPath') |
| 104 |
->will($this->returnValue('target.{locale}')); |
| 105 |
|
| 106 |
$this->writer->writeAsset($asset); |
| 107 |
|
| 108 |
$this->assertFileExists($this->dir.'/target.en'); |
| 109 |
$this->assertFileExists($this->dir.'/target.de'); |
| 110 |
$this->assertFileExists($this->dir.'/target.fr'); |
| 111 |
$this->assertEquals('en', file_get_contents($this->dir.'/target.en')); |
| 112 |
$this->assertEquals('de', file_get_contents($this->dir.'/target.de')); |
| 113 |
$this->assertEquals('fr', file_get_contents($this->dir.'/target.fr')); |
| 114 |
} |
| 115 |
|
| 116 |
public function testAssetWithInputVars() |
| 117 |
{ |
| 118 |
$asset = new FileAsset(__DIR__.'/Fixture/messages.{locale}.js', |
| 119 |
array(), null, null, array('locale')); |
| 120 |
$asset->setTargetPath('messages.{locale}.js'); |
| 121 |
|
| 122 |
$this->writer->writeAsset($asset); |
| 123 |
|
| 124 |
$this->assertFileExists($this->dir.'/messages.en.js'); |
| 125 |
$this->assertFileExists($this->dir.'/messages.de.js'); |
| 126 |
$this->assertFileExists($this->dir.'/messages.fr.js'); |
| 127 |
$this->assertEquals('var messages = {"text.greeting": "Hello %name%!"};', |
| 128 |
file_get_contents($this->dir.'/messages.en.js')); |
| 129 |
$this->assertEquals('var messages = {"text.greeting": "Hallo %name%!"};', |
| 130 |
file_get_contents($this->dir.'/messages.de.js')); |
| 131 |
$this->assertEquals('var messages = {"text.greet": "All\u00f4 %name%!"};', |
| 132 |
file_get_contents($this->dir.'/messages.fr.js')); |
| 133 |
} |
| 134 |
} |
| 135 |
|