| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Yiisoft\View\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Yiisoft\Files\FileHelper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Yiisoft\Tests\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Yiisoft\View\WebView; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | final class WebViewTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |     private string $dataDir; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     private string $layoutPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var string path for the test files. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private string $testViewPath = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected function setUp(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->dataDir = dirname(__DIR__) . '/public/view'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->layoutPath = $this->dataDir . '/layout.php'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->testViewPath = sys_get_temp_dir() . '/' . str_replace('\\', '_', get_class($this)) . uniqid('', false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         FileHelper::createDirectory($this->testViewPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected function tearDown(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         FileHelper::removeDirectory($this->testViewPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function testRegisterJsVar(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->webView->registerJsVar('username', 'samdark'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         $html = $this->webView->render('//layout.php', ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         $this-> assertStringContainsString('<script>var username = "samdark";</script></head>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->webView->registerJsVar('objectTest', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'number' => 42, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'question' => 'Unknown', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $html = $this->webView->render('//layout.php', ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->assertStringContainsString('<script>var objectTest = {"number":42,"question":"Unknown"};</script></head>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     public function testRegisterJsFileWithAlias(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_HEAD]); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></head>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_BEGIN]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->assertStringContainsString('<body>' . PHP_EOL . '<script src="/baseUrl/js/somefile.js"></script>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         $this->webView->registerJsFile($this->aliases->get('@web/js/somefile.js'), ['position' => WebView::POSITION_END]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertStringContainsString('<script src="/baseUrl/js/somefile.js"></script></body>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     public function testRegisterCssFileWithAlias(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->webView->registerCssFile($this->aliases->get('@web/css/somefile.css')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $html = $this->webView->renderFile($this->layoutPath, ['content' => 'content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $this->assertStringContainsString('<link href="/baseUrl/css/somefile.css" rel="stylesheet"></head>', $html); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  |