AssetConverterTest.php 2.17 KB
Newer Older
1 2 3 4 5 6
<?php
/**
 * @author Carsten Brandt <mail@cebe.cc>
 */

namespace yiiunit\framework\web;
AlexGx committed
7

8
use yii\helpers\FileHelper;
9 10 11 12 13 14 15
use yii\web\AssetConverter;

/**
 * @group web
 */
class AssetConverterTest extends \yiiunit\TestCase
{
16 17 18 19 20
    /**
     * @var string temporary files path
     */
    protected $tmpPath;

21 22 23 24
    protected function setUp()
    {
        parent::setUp();
        $this->mockApplication();
25 26 27 28
        $this->tmpPath = \Yii::$app->runtimePath . '/assetConverterTest_' . getmypid();
        if (!is_dir($this->tmpPath)) {
            mkdir($this->tmpPath, 0777, true);
        }
29 30
    }

31
    protected function tearDown()
32
    {
33 34
        if (is_dir($this->tmpPath)) {
            FileHelper::removeDirectory($this->tmpPath);
35
        }
36 37 38 39 40 41 42 43
        parent::tearDown();
    }

    // Tests :

    public function testConvert()
    {
        $tmpPath = $this->tmpPath;
44
        file_put_contents($tmpPath . '/test.php', <<<EOF
45 46 47 48 49
<?php

echo "Hello World!\n";
echo "Hello Yii!";
EOF
50
        );
51

52 53 54
        $converter = new AssetConverter();
        $converter->commands['php'] = ['txt', 'php {from} > {to}'];
        $this->assertEquals('test.txt', $converter->convert('test.php', $tmpPath));
55

56 57 58
        $this->assertTrue(file_exists($tmpPath . '/test.txt'), 'Failed asserting that asset output file exists.');
        $this->assertEquals("Hello World!\nHello Yii!", file_get_contents($tmpPath . '/test.txt'));
    }
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86

    /**
     * @depends testConvert
     */
    public function testForceConvert()
    {
        $tmpPath = $this->tmpPath;
        file_put_contents($tmpPath . '/test.php', <<<EOF
<?php

echo microtime();
EOF
        );

        $converter = new AssetConverter();
        $converter->commands['php'] = ['txt', 'php {from} > {to}'];

        $converter->convert('test.php', $tmpPath);
        $initialConvertTime = file_get_contents($tmpPath . '/test.txt');

        usleep(1);
        $converter->convert('test.php', $tmpPath);
        $this->assertEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));

        $converter->forceConvert = true;
        $converter->convert('test.php', $tmpPath);
        $this->assertNotEquals($initialConvertTime, file_get_contents($tmpPath . '/test.txt'));
    }
AlexGx committed
87
}