Completed
Push — snake-case-tests ( 55729f )
by Kamil
87:29 queued 51:35
created

AssetTest::testAssetsWhenReinstalled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
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 Sylius\Bundle\ThemeBundle\Tests\Functional;
13
14
use Sylius\Bundle\ThemeBundle\Asset\Installer\AssetsInstallerInterface;
15
16
/**
17
 * @author Kamil Kokot <[email protected]>
18
 */
19
final class AssetTest extends ThemeBundleTestCase
20
{
21
    protected function tearDown()
22
    {
23
        parent::tearDown();
24
25
        file_put_contents(__DIR__.'/../Fixtures/themes/FirstTestTheme/TestBundle/public/theme_asset.txt', 'Theme asset'.PHP_EOL);
26
    }
27
28
    /**
29
     * @test
30
     * @dataProvider getSymlinkMasks
31
     *
32
     * @param int $symlinkMask
33
     */
34
    public function it_dumps_assets($symlinkMask)
35
    {
36
        $webDirectory = $this->createWebDirectory();
37
38
        $client = $this->getClient();
39
40
        $client->getContainer()->get('sylius.theme.asset.assets_installer')->installAssets($webDirectory, $symlinkMask);
41
42
        $crawler = $client->request('GET', '/template/:Asset:assetsTest.txt.twig');
43
        $lines = explode("\n", $crawler->text());
44
45
        $this->assertFileContent($lines, $webDirectory);
46
    }
47
48
    /**
49
     * @test
50
     * @dataProvider getSymlinkMasks
51
     *
52
     * @param int $symlinkMask
53
     */
54
    public function it_updates_dumped_assets_if_they_are_modified($symlinkMask)
55
    {
56
        $webDirectory = $this->createWebDirectory();
57
58
        $client = $this->getClient();
59
60
        $client->getContainer()->get('sylius.theme.asset.assets_installer')->installAssets($webDirectory, $symlinkMask);
61
62
        sleep(1);
63
        file_put_contents(__DIR__.'/../Fixtures/themes/FirstTestTheme/TestBundle/public/theme_asset.txt', 'Theme asset modified');
64
        clearstatcache();
65
66
        $client->getContainer()->get('sylius.theme.asset.assets_installer')->installAssets($webDirectory, $symlinkMask);
67
68
        $crawler = $client->request('GET', '/template/:Asset:modifiedAssetsTest.txt.twig');
69
        $lines = explode("\n", $crawler->text());
70
71
        $this->assertFileContent($lines, $webDirectory);
72
    }
73
74
    /**
75
     * @test
76
     * @dataProvider getSymlinkMasks
77
     *
78
     * @param int $symlinkMask
79
     */
80
    public function it_dumps_assets_correctly_even_if_nothing_has_changed($symlinkMask)
81
    {
82
        $webDirectory = $this->createWebDirectory();
83
84
        $client = $this->getClient();
85
86
        $client->getContainer()->get('sylius.theme.asset.assets_installer')->installAssets($webDirectory, $symlinkMask);
87
        $client->getContainer()->get('sylius.theme.asset.assets_installer')->installAssets($webDirectory, $symlinkMask);
88
89
        $crawler = $client->request('GET', '/template/:Asset:assetsTest.txt.twig');
90
        $lines = explode("\n", $crawler->text());
91
92
        $this->assertFileContent($lines, $webDirectory);
93
    }
94
95
    private function createWebDirectory()
96
    {
97
        $webDirectory = $this->getTmpDirPath(self::TEST_CASE).'/web';
98
        if (!is_dir($webDirectory)) {
99
            mkdir($webDirectory, 0777, true);
100
        }
101
102
        chdir($webDirectory);
103
104
        return $webDirectory;
105
    }
106
107
    private function assertFileContent($lines, $webDirectory)
108
    {
109
        foreach ($lines as $line) {
110
            if (empty($line)) {
111
                continue;
112
            }
113
114
            list($expectedText, $assetFile) = explode(': ', $line);
115
116
            $contents = file_get_contents($webDirectory.$assetFile);
117
118
            $this->assertEquals($expectedText, trim($contents));
119
        }
120
    }
121
122
    /**
123
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
124
     */
125
    public function getSymlinkMasks()
126
    {
127
        return [
128
            [AssetsInstallerInterface::RELATIVE_SYMLINK],
129
            [AssetsInstallerInterface::SYMLINK],
130
            [AssetsInstallerInterface::HARD_COPY],
131
        ];
132
    }
133
}
134