-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCssUpdaterTest.php
More file actions
143 lines (123 loc) · 4.87 KB
/
CssUpdaterTest.php
File metadata and controls
143 lines (123 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php declare(strict_types=1);
namespace Tests\CssManager;
use PHPUnit\Framework\TestCase;
/**
* 测试 php-css-updater\CssManager\CssUpdater
*
* @author fdipzone
*/
final class CssUpdaterTest extends TestCase
{
/**
* @covers \CssManager\CssUpdater::__construct
*/
public function testConstruct()
{
$css_tmpl_path = '/tmp';
$css_path = '/tmp/css';
$replace_tags = ['jpg', 'gif'];
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
$this->assertEquals('CssManager\CssUpdater', get_class($css_updater));
}
/**
* @covers \CssManager\CssUpdater::__construct
*/
public function testConstructCssTmplPathException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('css updater: css tmpl path not exists');
$css_tmpl_path = '/tmp/not_exists';
$css_path = '/tmp/css';
$replace_tags = ['jpg', 'gif'];
new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
}
/**
* @covers \CssManager\CssUpdater::__construct
*/
public function testConstructCssPathException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('css updater: css path is empty');
$css_tmpl_path = '/tmp';
$css_path = '';
$replace_tags = ['jpg', 'gif'];
new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
}
/**
* @covers \CssManager\CssUpdater::__construct
*/
public function testConstructReplaceTagsException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('css updater: replace tags is empty');
$css_tmpl_path = '/tmp';
$css_path = '/tmp/css';
$replace_tags = [];
new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
}
/**
* @covers \CssManager\CssUpdater::setLogFile
* @covers \CssManager\CssUpdater::logFile
*/
public function testSetAndGetLogFile()
{
$css_tmpl_path = '/tmp';
$css_path = '/tmp/css';
$replace_tags = ['jpg', 'gif'];
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
$log_file = '/tmp/update.log';
$css_updater->setLogFile($log_file);
$this->assertEquals($log_file, $css_updater->logFile());
}
/**
* @covers \CssManager\CssUpdater::setLogFile
*/
public function testSetLogFileException()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('css updater: log file is empty');
$css_tmpl_path = '/tmp';
$css_path = '/tmp/css';
$replace_tags = ['jpg', 'gif'];
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
$css_updater->setLogFile('');
}
/**
* @covers \CssManager\CssUpdater::update
*/
public function testUpdate()
{
$css_tmpl_path = dirname(__FILE__).'/test_data';
$css_path = sprintf('/tmp/ut-%s-%s-%d/css', md5(__CLASS__), date('YmdHis'), \Tests\Utils\PHPUnitExtension::sequenceId());
$replace_tags = ['jpg', 'png', 'gif', 'woff2', 'woff'];
$log_file = sprintf('/tmp/ut-%s-%s-%d/update.log', md5(__CLASS__), date('YmdHis'), \Tests\Utils\PHPUnitExtension::sequenceId());
// 不遍历子目录
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
$success_num = $css_updater->update();
$this->assertSame(1, $success_num);
// 遍历子目录
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags, true);
$css_updater->setLogFile($log_file);
$success_num = $css_updater->update();
$this->assertSame(3, $success_num);
}
/**
* @covers \CssManager\CssUpdater::create
*/
public function testCreate()
{
$css_tmpl_path = dirname(__FILE__).'/test_data';
$css_path = sprintf('/tmp/ut-%s-%s-%d/css', md5(__CLASS__), date('YmdHis'), \Tests\Utils\PHPUnitExtension::sequenceId());
$replace_tags = ['jpg', 'png', 'gif'];
$source_file = $css_tmpl_path.'/main.css';
$dest_file = $css_path.'/main_update.css';
$css_updater = new \CssManager\CssUpdater($css_tmpl_path, $css_path, $replace_tags);
$ret = \Tests\Utils\PHPUnitExtension::callMethod($css_updater, 'create', [$source_file, $dest_file]);
$this->assertTrue($ret);
$this->assertTrue(file_exists($dest_file));
$css_content = file_get_contents($dest_file);
$this->assertTrue(strstr($css_content, 'images/background.jpg?'.date('Ymd'))!==false);
$this->assertTrue(strstr($css_content, 'images/header.png?'.date('Ymd'))!==false);
$this->assertTrue(strstr($css_content, 'images/logo.gif?'.date('Ymd'))!==false);
}
}