Passed
Push — master ( f02495...4136df )
by Alexander
01:44
created

TestCase::removeAssets()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 5
nop 1
dl 0
loc 18
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap4\Tests;
6
7
use Yiisoft\Composer\Config\Builder;
8
use PHPUnit\Framework\TestCase as BaseTestCase;
9
use Psr\Container\ContainerInterface;
10
use Psr\EventDispatcher\EventDispatcherInterface;
11
use Psr\EventDispatcher\ListenerProviderInterface;
12
use Psr\Log\LoggerInterface;
13
use Yiisoft\Aliases\Aliases;
14
use Yiisoft\Assets\AssetManager;
15
use Yiisoft\Files\FileHelper;
16
use Yiisoft\Di\Container;
17
use Yiisoft\Widget\Widget;
18
use Yiisoft\Widget\WidgetFactory;
19
20
abstract class TestCase extends BaseTestCase
21
{
22
    /**
23
     * @var Aliases $aliases
24
     */
25
    protected $aliases;
26
27
    /**
28
     * @var AssetManager $assetManager
29
     */
30
    protected $assetManager;
31
32
    /**
33
     * @var ContainerInterface $container
34
     */
35
    private $container;
36
37
    /**
38
     * @var WebView $webView
39
     */
40
    protected $webView;
41
42
    protected function setUp(): void
43
    {
44
        parent::setUp();
45
        $config = require Builder::path('tests');
46
        $this->container = new Container($config);
47
        $this->aliases = $this->container->get(Aliases::class);
48
49
        WidgetFactory::initialize($this->container, []);
50
    }
51
52
    protected function tearDown(): void
53
    {
54
        $this->container = null;
55
        $this->removeAssets('@basePath');
56
        parent::tearDown();
57
    }
58
59
    /**
60
     * Asserting two strings equality ignoring line endings.
61
     * @param string $expected
62
     * @param string $actual
63
     * @param string $message
64
     *
65
     * @return void
66
     */
67
    protected function assertEqualsWithoutLE(string $expected, string $actual, string $message = ''): void
68
    {
69
        $expected = str_replace("\r\n", "\n", $expected);
70
        $actual = str_replace("\r\n", "\n", $actual);
71
        $this->assertEquals($expected, $actual, $message);
72
    }
73
74
    /**
75
     * Asserting same ignoring slash.
76
     *
77
     * @param string $expected
78
     * @param string $actual
79
     *
80
     * @return void
81
     */
82
    protected function assertSameIgnoringSlash(string $expected, string $actual): void
83
    {
84
        $expected = str_replace(['/', '\\'], '/', $expected);
85
        $actual = str_replace(['/', '\\'], '/', $actual);
86
        $this->assertSame($expected, $actual);
87
    }
88
89
    protected function removeAssets(string $basePath): void
90
    {
91
        $handle = opendir($dir = $this->aliases->get($basePath));
92
        if ($handle === false) {
93
            throw new \Exception("Unable to open directory: $dir");
94
        }
95
        while (($file = readdir($handle)) !== false) {
96
            if ($file === '.' || $file === '..' || $file === '.gitignore') {
97
                continue;
98
            }
99
            $path = $dir . DIRECTORY_SEPARATOR . $file;
100
            if (is_dir($path)) {
101
                FileHelper::removeDirectory($path);
102
            } else {
103
                FileHelper::unlink($path);
104
            }
105
        }
106
        closedir($handle);
107
    }
108
}
109