Skip to content

Commit 1bcc6f7

Browse files
committed
Add Service Assets Tests
1 parent e26c897 commit 1bcc6f7

File tree

1 file changed

+178
-2
lines changed

1 file changed

+178
-2
lines changed

tests/Services/AssetsTest.php

+178-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22

33
namespace Services;
44

5+
use BEA\Theme\Framework\Framework;
56
use BEA\Theme\Framework\Service_Container;
67
use BEA\Theme\Framework\Services\Acf;
78
use BEA\Theme\Framework\Services\Assets;
89
use BEA\Theme\Framework\Services\Svg;
910
use InvalidArgumentException;
11+
use stdclass;
1012
use WP_Mock;
1113
use WP_Mock\Tools\TestCase;
14+
use function define;
1215

1316
class AssetsTest extends TestCase {
17+
/**
18+
* Since we have CONSTANTS, run all tests in separate processes
19+
*/
20+
protected $preserveGlobalState = false;
21+
protected $runTestInSeparateProcess = true;
22+
1423
public function setUp(): void {
1524
WP_Mock::setUp();
1625
}
@@ -24,12 +33,179 @@ public function testName() {
2433
$this->assertEquals( 'assets', $assets->get_service_name() );
2534
}
2635

27-
public function testStyleSheetURIWithScriptDebug() {
36+
public function testStyleSheetURIWithScriptDebugAndNoFiles() {
2837
$assets = new Assets();
2938

30-
WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 1 ] );
39+
define( 'SCRIPT_DEBUG', true );
40+
41+
WP_Mock::userFunction( 'get_theme_file_path', [
42+
'times' => 1,
43+
'return' => 'ok.css',
44+
'args' => [ '/dist/app.css' ],
45+
] );
46+
47+
$this->assertSame( 'ok.css', $assets->stylesheet_uri( 'ok.css' ) );
48+
}
49+
50+
public function testStyleSheetURIWithScriptDebugAndFiles() {
51+
$assets = new Assets();
52+
53+
define( 'SCRIPT_DEBUG', true );
54+
55+
$assets_file = __DIR__ . '/../data/assets/app.css';
56+
57+
WP_Mock::userFunction(
58+
'get_theme_file_path',
59+
[
60+
'times' => 1,
61+
'return' => $assets_file,
62+
'args' => [
63+
'/dist/app.css',
64+
],
65+
]
66+
);
67+
WP_Mock::userFunction(
68+
'get_theme_file_uri',
69+
[
70+
'times' => 1,
71+
'args' => [
72+
'/dist/app.css',
73+
],
74+
'return' => 'https://localhost.dev/data/assets/app.css',
75+
]
76+
);
77+
78+
$this->assertSame( 'https://localhost.dev/data/assets/app.css', $assets->stylesheet_uri( 'ok.css' ) );
79+
}
80+
81+
public function testStyleSheetURIWithOutScriptDebugAndNoFiles() {
82+
$assets = new Assets();
83+
84+
define( 'SCRIPT_DEBUG', false );
85+
86+
WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 2 ] );
3187

3288
$this->assertSame( 'ok.css', $assets->stylesheet_uri( 'ok.css' ) );
3389
}
3490

91+
public function testGetMinFileEmpty() {
92+
$assets = new Assets();
93+
94+
$this->assertSame( '', $assets->get_min_file( '' ) );
95+
}
96+
97+
public function testGetMinFileAssetsExistsAndEmpty() {
98+
$assets = new Assets();
99+
$assets_file = __DIR__ . '/../data/assets/assets-empty.json';
100+
101+
WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] );
102+
103+
$this->assertSame( '', $assets->get_min_file( 'css' ) );
104+
}
105+
106+
public function testGetMinFileAssetsExistsNotEmptyNonExistingType() {
107+
$assets = new Assets();
108+
$assets_file = __DIR__ . '/../data/assets/assets.json';
109+
110+
WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] );
111+
112+
$this->assertSame( '', $assets->get_min_file( 'non-existing' ) );
113+
}
114+
115+
public function testGetMinFileAssetsExistsNotEmptyExistingType() {
116+
$assets = new Assets();
117+
$assets_file = __DIR__ . '/../data/assets/assets.json';
118+
119+
WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] );
120+
121+
// Existing
122+
$this->assertSame( 'app.min.css', $assets->get_min_file( 'css' ) );
123+
$this->assertSame( 'app.min.js', $assets->get_min_file( 'js' ) );
124+
$this->assertSame( 'editor.min.css', $assets->get_min_file( 'editor.css' ) );
125+
$this->assertSame( 'editor.min.js', $assets->get_min_file( 'editor.js' ) );
126+
$this->assertSame( 'login.min.css', $assets->get_min_file( 'login' ) );
127+
128+
// Custom
129+
$this->assertSame( 'custom.min.css', $assets->get_min_file( 'custom.css' ) );
130+
131+
// Non existing
132+
$this->assertSame( '', $assets->get_min_file( 'custom.min.css' ) );
133+
}
134+
135+
public function testGetLoginStyleSheet() {
136+
$assets = new Assets();
137+
$assets_file = __DIR__ . '/../data/assets/assets.json';
138+
139+
WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] );
140+
141+
// Existing
142+
$this->assertSame( 'dist/login.min.css', $assets->login_stylesheet_uri() );
143+
}
144+
145+
public function testGetLoginStyleSheetDebug() {
146+
$assets = new Assets();
147+
$assets_file = __DIR__ . '/../data/assets/assets.json';
148+
define( 'SCRIPT_DEBUG', true );
149+
150+
WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] );
151+
152+
// Existing
153+
$this->assertSame( 'dist/login.css', $assets->login_stylesheet_uri() );
154+
}
155+
156+
public function testRegisterFilesAdmin() {
157+
$assets = new Assets();
158+
159+
WP_Mock::userFunction( 'is_admin', [ 'return' => true, 'times' => 1 ] );
160+
WP_Mock::passthruFunction( 'wp_get_theme', [ 'times' => 0 ] );
161+
162+
$this->assertNull( $assets->register_assets() );
163+
}
164+
165+
public function testRegisterFiles() {
166+
$assets = new Assets();
167+
$container = Framework::get_container();
168+
$theme_mock = $this->getMockBuilder( stdclass::class )->addMethods( [ 'get' ] )->getMock();
169+
$assets->register( $container );
170+
171+
WP_Mock::userFunction(
172+
'is_admin',
173+
[
174+
'return' => false,
175+
'times' => 1,
176+
]
177+
);
178+
WP_Mock::userFunction(
179+
'wp_get_theme',
180+
[
181+
'times' => 1,
182+
'return' => $theme_mock,
183+
]
184+
);
185+
WP_Mock::passthruFunction( 'wp_register_script', [ 'times' => 2 ] );
186+
WP_Mock::passthruFunction( 'wp_register_style', [ 'times' => 1 ] );
187+
WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 1 ] );
188+
WP_Mock::passthruFunction( 'get_theme_file_uri' );
189+
WP_Mock::userFunction( 'get_stylesheet_uri', [ 'return' => false ] );
190+
191+
// Check the version is called
192+
$theme_mock->expects( $this->atLeastOnce() )->method( 'get' );
193+
194+
$this->assertNull( $assets->register_assets() );
195+
}
196+
197+
public function testCheckFiltersAdded() {
198+
$assets = new Assets();
199+
200+
// Check filters
201+
WP_Mock::expectFilterAdded( 'stylesheet_uri', [ $assets, 'stylesheet_uri' ] );
202+
WP_Mock::expectFilterAdded( 'wp_login_page_theme_css', [ $assets, 'login_stylesheet_uri' ] );
203+
204+
// Check Action
205+
WP_Mock::expectActionAdded( 'wp', [ $assets, 'register_assets' ] );
206+
WP_Mock::expectActionAdded( 'wp_enqueue_scripts', [ $assets, 'enqueue_scripts' ] );
207+
WP_Mock::expectActionAdded( 'wp_print_styles', [ $assets, 'enqueue_styles' ] );
208+
209+
$assets->boot( Framework::get_container() );
210+
}
35211
}

0 commit comments

Comments
 (0)