Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
ExtendedClientScriptTest.php
1 <?php
2 /**
3  *
4  */
5 class ExtendedClientScriptTest extends CTestCase
6 {
7 
8  protected $cssFiles = array(
9  'css/core.css',
10  'css/default.css',
11  'css/styles.css'
12  );
13 
14  protected $jsFiles = array(
15  'js/jquery.json.js',
16  'js/jquery.yiigridview.js',
17  );
18 
19  protected $assetsFolder;
20 
21  /**
22  * @var ExtendedClientScript
23  */
24  public $cs;
25 
26  public function setUp()
27  {
28  Yii::setPathOfAlias('webroot', Yii::getPathOfAlias('site.tests.runtime.www'));
29  $this->assetsFolder = '/assets';
30 
31  $this->cs = Yii::createComponent(array(
32  'class' => 'common.extensions.ExtendedClientScript.ExtendedClientScript',
33  'disableRegisterFiles' => false,
34  'fileUrl' => $this->assetsFolder,
35  'basePath' => Yii::getPathOfAlias('webroot'),
36  //combine css files to single file
37  'combineCss' => false,
38  //compress css files
39  'compressCombinedCss' => false,
40  //combine js files to single file
41  'combineJs' => false,
42  //compress js files
43  'compressCombinedJs' => false,
44  //javascript minificator class location
45  'jsMinPath' => 'common.extensions.ExtendedClientScript.jsmin.*',
46  //CSS minificator class location
47  'cssMinPath' => 'common.extensions.ExtendedClientScript.cssmin.*'
48  ));
49  }
50 
51  public function tearDown()
52  {
53  $fsIter = new FilesystemIterator(Yii::getPathOfAlias('webroot') . $this->assetsFolder);
54  foreach ($fsIter as $f) {
55  if (preg_match('/.*.(css|js)/', $f->getFileName())) {
56  unlink($f->getPathname());
57  }
58  }
59  }
60 
61  public function testPublishCss()
62  {
63  foreach ($this->cssFiles as $f) {
64  $this->cs->registerCssFile($this->assetsFolder . '/' . $f);
65  }
66 
67  $output = '';
68  $this->cs->renderHead($output);
69 
70  preg_match_all('/href="(?P<url>.*)"/', $output, $matches);
71  $this->assertEquals(count($this->cssFiles), count($matches['url']));
72  }
73 
74  public function testCombineCss()
75  {
76  $this->cs->combineCss = true;
77 
78  foreach ($this->cssFiles as $f) {
79  $this->cs->registerCssFile($this->assetsFolder . '/' . $f);
80  }
81 
82  $output = '';
83  $this->cs->renderHead($output);
84 
85  preg_match('/href="(?P<url>.*)"/', $output, $matches);
86  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
87  }
88 
89  public function testCombineCompressCss()
90  {
91  $this->cs->combineCss = true;
92  $this->cs->compressCombinedCss = true;
93 
94  foreach ($this->cssFiles as $f) {
95  $this->cs->registerCssFile($this->assetsFolder . '/' . $f);
96  }
97 
98  $output = '';
99  $this->cs->renderHead($output);
100 
101  preg_match('/href="(?P<url>.*)"/', $output, $matches);
102  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
103  }
104 
105  public function testPublishJs()
106  {
107  foreach ($this->jsFiles as $f) {
108  $this->cs->registerScriptFile($this->assetsFolder . '/' . $f);
109  }
110 
111  $output = '';
112  $this->cs->renderHead($output);
113 
114 
115  preg_match_all('/src="(?P<url>.*)"/', $output, $matches);
116  $this->assertEquals(count($this->jsFiles), count($matches['url']));
117  }
118 
119  public function testCombineJs()
120  {
121  $this->cs->combineJs = true;
122 
123  foreach ($this->jsFiles as $f) {
124  $this->cs->registerScriptFile($this->assetsFolder . '/' . $f);
125  }
126 
127  $output = '';
128  $this->cs->renderHead($output);
129 
130 
131  preg_match('/src="(?P<url>.*)"/', $output, $matches);
132  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
133  }
134 
135  public function testCombineCompressJs()
136  {
137  $this->cs->combineJs = true;
138  $this->cs->compressCombinedJs = true;
139 
140  foreach ($this->jsFiles as $f) {
141  $this->cs->registerScriptFile($this->assetsFolder . '/' . $f);
142  }
143 
144  $output = '';
145  $this->cs->renderHead($output);
146 
147  preg_match('/src="(?P<url>.*)"/', $output, $matches);
148  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
149  }
150 
151  public function testCombineCompressJsCss()
152  {
153  $this->cs->combineJs = true;
154  $this->cs->compressCombinedJs = true;
155  $this->cs->combineCss = true;
156  $this->cs->compressCombinedCss = true;
157 
158 
159  foreach ($this->cssFiles as $f) {
160  $this->cs->registerCssFile($this->assetsFolder . '/' . $f);
161  }
162  foreach ($this->jsFiles as $f) {
163  $this->cs->registerScriptFile($this->assetsFolder . '/' . $f);
164  }
165 
166  $output = '';
167  $this->cs->renderHead($output);
168 
169  preg_match('/href="(?P<url>.*)"/', $output, $matches);
170  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
171 
172  preg_match('/src="(?P<url>.*)"/', $output, $matches);
173  $this->assertFileExists(Yii::getPathOfAlias('webroot') . $matches['url']);
174 
175  }
176 
177  public function testDisableRegisterFiles()
178  {
179  $this->cs->disableRegisterFiles = true;
180 
181  foreach ($this->cssFiles as $f) {
182  $this->cs->registerCssFile($this->assetsFolder . '/' . $f);
183  }
184  foreach ($this->jsFiles as $f) {
185  $this->cs->registerScriptFile($this->assetsFolder . '/' . $f);
186  }
187 
188  $output = '';
189  $this->cs->renderHead($output);
190 
191  preg_match('/href="(?P<url>.*)"/', $output, $matches);
192  $this->assertArrayNotHasKey('url', $matches);
193 
194  preg_match('/src="(?P<url>.*)"/', $output, $matches);
195  $this->assertArrayNotHasKey('url', $matches);
196  }
197 
198 
199 }