@wordpress/style-engine 0.16.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,63 +6,93 @@
6
6
  * @subpackage style-engine
7
7
  */
8
8
 
9
- require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
10
- require __DIR__ . '/../class-wp-style-engine-css-rule.php';
11
- require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
9
+ // Check for the existence of Style Engine classes and methods.
10
+ // Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
11
+ // Testing new features from the Gutenberg package may require
12
+ // testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
13
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
14
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
15
+ }
16
+
17
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
18
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
19
+ }
20
+
21
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
22
+ require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
23
+ }
12
24
 
13
25
  /**
14
- * Tests for registering, storing and retrieving CSS Rules.
26
+ * Tests for registering, storing and retrieving a collection of CSS Rules (a store).
27
+ *
28
+ * @coversDefaultClass WP_Style_Engine_CSS_Rules_Store
15
29
  */
16
30
  class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
17
31
  /**
18
- * Tear down after each test.
32
+ * Cleans up stores after each test.
19
33
  */
20
34
  public function tear_down() {
21
- parent::tear_down();
22
35
  WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
36
+ parent::tear_down();
23
37
  }
38
+
24
39
  /**
25
- * Should create a new store.
40
+ * Tests creating a new store on instantiation.
41
+ *
42
+ * @covers ::__construct
26
43
  */
27
- public function test_create_new_store() {
44
+ public function test_should_create_new_store_on_instantiation() {
28
45
  $new_pancakes_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pancakes-with-strawberries' );
46
+
29
47
  $this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $new_pancakes_store );
30
48
  }
31
49
 
32
50
  /**
33
- * Should not create a new store with invalid $store_name.
51
+ * Tests that a `$store_name` argument is required and no store will be created without one.
52
+ *
53
+ * @covers ::get_store
34
54
  */
35
- public function test_store_name_required() {
55
+ public function test_should_not_create_store_without_a_store_name() {
36
56
  $not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( '' );
37
- $this->assertEmpty( $not_a_store );
57
+
58
+ $this->assertEmpty( $not_a_store, 'get_store() did not return an empty value with empty string as argument.' );
38
59
 
39
60
  $also_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( 123 );
40
- $this->assertEmpty( $also_not_a_store );
61
+
62
+ $this->assertEmpty( $also_not_a_store, 'get_store() did not return an empty value with number as argument.' );
41
63
 
42
64
  $definitely_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( null );
43
- $this->assertEmpty( $definitely_not_a_store );
65
+
66
+ $this->assertEmpty( $definitely_not_a_store, 'get_store() did not return an empty value with `null` as argument.' );
44
67
  }
45
68
 
46
69
  /**
47
- * Should return previously created store when the same selector key is passed.
70
+ * Tests returning a previously created store when the same selector key is passed.
71
+ *
72
+ * @covers ::get_store
48
73
  */
49
- public function test_get_store() {
74
+ public function test_should_return_existing_store() {
50
75
  $new_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
51
76
  $selector = '.haddock';
52
77
 
53
- $new_fish_store->add_rule( $selector )->get_selector();
54
- $this->assertEquals( $selector, $new_fish_store->add_rule( $selector )->get_selector() );
78
+ $new_fish_store->add_rule( $selector );
79
+
80
+ $this->assertSame( $selector, $new_fish_store->add_rule( $selector )->get_selector(), 'Selector string of store rule does not match expected value' );
55
81
 
56
82
  $the_same_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
57
- $this->assertEquals( $selector, $the_same_fish_store->add_rule( $selector )->get_selector() );
83
+
84
+ $this->assertSame( $selector, $the_same_fish_store->add_rule( $selector )->get_selector(), 'Selector string of existing store rule does not match expected value' );
58
85
  }
59
86
 
60
87
  /**
61
- * Should return all previously created stores.
88
+ * Tests returning all previously created stores.
89
+ *
90
+ * @covers ::get_stores
62
91
  */
63
- public function test_get_stores() {
92
+ public function test_should_get_all_existing_stores() {
64
93
  $burrito_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'burrito' );
65
94
  $quesadilla_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'quesadilla' );
95
+
66
96
  $this->assertEquals(
67
97
  array(
68
98
  'burrito' => $burrito_store,
@@ -73,34 +103,43 @@ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
73
103
  }
74
104
 
75
105
  /**
76
- * Should delete all previously created stores.
106
+ * Tests that all previously created stores are deleted.
107
+ *
108
+ * @covers ::remove_all_stores
77
109
  */
78
- public function test_remove_all_stores() {
110
+ public function test_should_remove_all_stores() {
79
111
  $dolmades_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'dolmades' );
80
112
  $tzatziki_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'tzatziki' );
113
+
81
114
  $this->assertEquals(
82
115
  array(
83
116
  'dolmades' => $dolmades_store,
84
117
  'tzatziki' => $tzatziki_store,
85
118
  ),
86
- WP_Style_Engine_CSS_Rules_Store::get_stores()
119
+ WP_Style_Engine_CSS_Rules_Store::get_stores(),
120
+ 'Return value of get_stores() does not match expectation'
87
121
  );
88
122
  WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
123
+
89
124
  $this->assertEquals(
90
125
  array(),
91
- WP_Style_Engine_CSS_Rules_Store::get_stores()
126
+ WP_Style_Engine_CSS_Rules_Store::get_stores(),
127
+ 'Return value of get_stores() is not an empty array after remove_all_stores() called.'
92
128
  );
93
129
  }
94
130
 
95
131
  /**
96
- * Should return a stored rule.
132
+ * Tests adding rules to an existing store.
133
+ *
134
+ * @covers ::add_rule
97
135
  */
98
- public function test_add_rule() {
136
+ public function test_should_add_rule_to_existing_store() {
99
137
  $new_pie_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'meat-pie' );
100
138
  $selector = '.wp-block-sauce a:hover';
101
139
  $store_rule = $new_pie_store->add_rule( $selector );
102
140
  $expected = '';
103
- $this->assertEquals( $expected, $store_rule->get_css() );
141
+
142
+ $this->assertSame( $expected, $store_rule->get_css(), 'Return value of get_css() is not a empty string where a rule has no CSS declarations.' );
104
143
 
105
144
  $pie_declarations = array(
106
145
  'color' => 'brown',
@@ -112,13 +151,16 @@ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
112
151
 
113
152
  $store_rule = $new_pie_store->add_rule( $selector );
114
153
  $expected = "$selector{{$css_declarations->get_declarations_string()}}";
115
- $this->assertEquals( $expected, $store_rule->get_css() );
154
+
155
+ $this->assertSame( $expected, $store_rule->get_css(), 'Return value of get_css() does not match expected CSS from existing store rules.' );
116
156
  }
117
157
 
118
158
  /**
119
- * Should return all stored rules.
159
+ * Tests that all stored rule objects are returned.
160
+ *
161
+ * @covers ::get_all_rules
120
162
  */
121
- public function test_get_all_rules() {
163
+ public function test_should_get_all_rule_objects_for_a_store() {
122
164
  $new_pizza_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pizza-with-mozzarella' );
123
165
  $selector = '.wp-block-anchovies a:hover';
124
166
  $store_rule = $new_pizza_store->add_rule( $selector );
@@ -126,33 +168,7 @@ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
126
168
  $selector => $store_rule,
127
169
  );
128
170
 
129
- $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
130
-
131
- $pizza_declarations = array(
132
- 'color' => 'red',
133
- 'border-color' => 'yellow',
134
- 'border-radius' => '10rem',
135
- );
136
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $pizza_declarations );
137
- $store_rule->add_declarations( array( $css_declarations ) );
138
-
139
- $expected = array(
140
- $selector => $store_rule,
141
- );
142
- $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
143
-
144
- $new_pizza_declarations = array(
145
- 'color' => 'red',
146
- 'border-color' => 'red',
147
- 'font-size' => '10rem',
148
- );
149
- $css_declarations = new WP_Style_Engine_CSS_Declarations( $new_pizza_declarations );
150
- $store_rule->add_declarations( array( $css_declarations ) );
151
-
152
- $expected = array(
153
- $selector => $store_rule,
154
- );
155
- $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
171
+ $this->assertSame( $expected, $new_pizza_store->get_all_rules(), 'Return value for get_all_rules() does not match expectations.' );
156
172
 
157
173
  $new_selector = '.wp-block-mushroom a:hover';
158
174
  $newer_pizza_declarations = array(
@@ -166,6 +182,7 @@ class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
166
182
  $selector => $store_rule,
167
183
  $new_selector => $new_store_rule,
168
184
  );
169
- $this->assertEquals( $expected, $new_pizza_store->get_all_rules() );
185
+
186
+ $this->assertSame( $expected, $new_pizza_store->get_all_rules(), 'Return value for get_all_rules() does not match expectations after adding new rules to store.' );
170
187
  }
171
188
  }
@@ -1,23 +1,44 @@
1
1
  <?php
2
2
  /**
3
- * Tests the Style Engine Renderer class.
3
+ * Tests the Style Engine Processor class.
4
4
  *
5
5
  * @package Gutenberg
6
6
  * @subpackage style-engine
7
7
  */
8
- require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
9
- require __DIR__ . '/../class-wp-style-engine-css-rule.php';
10
- require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
11
- require __DIR__ . '/../class-wp-style-engine-processor.php';
8
+
9
+ // Check for the existence of Style Engine classes and methods.
10
+ // Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
11
+ // Testing new features from the Gutenberg package may require
12
+ // testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
13
+ if ( ! class_exists( 'WP_Style_Engine_Processor' ) ) {
14
+ require __DIR__ . '/../class-wp-style-engine-processor.php';
15
+ }
16
+
17
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
18
+ require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
19
+ }
20
+
21
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
22
+ require __DIR__ . '/../class-wp-style-engine-css-rule.php';
23
+ }
24
+
25
+ if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
26
+ require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
27
+ }
12
28
 
13
29
  /**
14
30
  * Tests for compiling and rendering styles from a store of CSS rules.
31
+ *
32
+ * @coversDefaultClass WP_Style_Engine_Processor
15
33
  */
16
34
  class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
17
35
  /**
18
- * Should compile CSS rules.
36
+ * Tests adding rules and returning compiled CSS rules.
37
+ *
38
+ * @covers ::add_rules
39
+ * @covers ::get_css
19
40
  */
20
- public function test_return_rules_as_css() {
41
+ public function test_should_return_rules_as_compiled_css() {
21
42
  $a_nice_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nice-rule' );
22
43
  $a_nice_css_rule->add_declarations(
23
44
  array(
@@ -35,16 +56,19 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
35
56
  );
36
57
  $a_nice_processor = new WP_Style_Engine_Processor();
37
58
  $a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) );
38
- $this->assertEquals(
59
+
60
+ $this->assertSame(
39
61
  '.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
40
62
  $a_nice_processor->get_css( array( 'prettify' => false ) )
41
63
  );
42
64
  }
43
65
 
44
66
  /**
45
- * Should compile CSS rules.
67
+ * Tests compiling CSS rules and formatting them with new lines and indents.
68
+ *
69
+ * @covers ::get_css
46
70
  */
47
- public function test_return_prettified_rules_as_css() {
71
+ public function test_should_return_prettified_css_rules() {
48
72
  $a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-wonderful-rule' );
49
73
  $a_wonderful_css_rule->add_declarations(
50
74
  array(
@@ -81,16 +105,18 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
81
105
  background-color: orange;
82
106
  }
83
107
  ';
84
- $this->assertEquals(
108
+ $this->assertSame(
85
109
  $expected,
86
110
  $a_wonderful_processor->get_css( array( 'prettify' => true ) )
87
111
  );
88
112
  }
89
113
 
90
114
  /**
91
- * Should compile CSS rules from the store.
115
+ * Tests adding a store and compiling CSS rules from that store.
116
+ *
117
+ * @covers ::add_store
92
118
  */
93
- public function test_return_store_rules_as_css() {
119
+ public function test_should_return_store_rules_as_css() {
94
120
  $a_nice_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'nice' );
95
121
  $a_nice_store->add_rule( '.a-nice-rule' )->add_declarations(
96
122
  array(
@@ -107,16 +133,19 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
107
133
  );
108
134
  $a_nice_renderer = new WP_Style_Engine_Processor();
109
135
  $a_nice_renderer->add_store( $a_nice_store );
110
- $this->assertEquals(
136
+ $this->assertSame(
111
137
  '.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}',
112
138
  $a_nice_renderer->get_css( array( 'prettify' => false ) )
113
139
  );
114
140
  }
115
141
 
116
142
  /**
117
- * Should merge CSS declarations.
143
+ * Tests that CSS declarations are merged and deduped in the final CSS rules output.
144
+ *
145
+ * @covers ::add_rules
146
+ * @covers ::get_css
118
147
  */
119
- public function test_dedupe_and_merge_css_declarations() {
148
+ public function test_should_dedupe_and_merge_css_declarations() {
120
149
  $an_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
121
150
  $an_excellent_processor = new WP_Style_Engine_Processor();
122
151
  $an_excellent_rule->add_declarations(
@@ -136,9 +165,10 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
136
165
  )
137
166
  );
138
167
  $an_excellent_processor->add_rules( $another_excellent_rule );
139
- $this->assertEquals(
168
+ $this->assertSame(
140
169
  '.an-excellent-rule{color:var(--excellent-color);border-style:dotted;border-color:brown;}',
141
- $an_excellent_processor->get_css( array( 'prettify' => false ) )
170
+ $an_excellent_processor->get_css( array( 'prettify' => false ) ),
171
+ 'Return value of get_css() does not match expectations with new, deduped and merged declarations.'
142
172
  );
143
173
 
144
174
  $yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' );
@@ -150,16 +180,19 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
150
180
  )
151
181
  );
152
182
  $an_excellent_processor->add_rules( $yet_another_excellent_rule );
153
- $this->assertEquals(
183
+ $this->assertSame(
154
184
  '.an-excellent-rule{color:var(--excellent-color);border-style:dashed;border-color:brown;border-width:2px;}',
155
- $an_excellent_processor->get_css( array( 'prettify' => false ) )
185
+ $an_excellent_processor->get_css( array( 'prettify' => false ) ),
186
+ 'Return value of get_css() does not match expectations with deduped and merged declarations.'
156
187
  );
157
188
  }
158
189
 
159
190
  /**
160
- * Should print out uncombined selectors duplicate CSS rules.
191
+ * Tests printing out 'unoptimized' CSS, that is, uncombined selectors and duplicate CSS rules.
192
+ *
193
+ * @covers ::get_css
161
194
  */
162
- public function test_output_verbose_css_rules() {
195
+ public function test_should_not_optimize_css_output() {
163
196
  $a_sweet_rule = new WP_Style_Engine_CSS_Rule(
164
197
  '.a-sweet-rule',
165
198
  array(
@@ -187,7 +220,7 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
187
220
  $a_sweet_processor = new WP_Style_Engine_Processor();
188
221
  $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule, $the_sweetest_rule ) );
189
222
 
190
- $this->assertEquals(
223
+ $this->assertSame(
191
224
  '.a-sweet-rule{color:var(--sweet-color);background-color:purple;}#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}.the-sweetest-rule-of-all a{color:var(--sweet-color);background-color:purple;}',
192
225
  $a_sweet_processor->get_css(
193
226
  array(
@@ -199,9 +232,11 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
199
232
  }
200
233
 
201
234
  /**
202
- * Should combine duplicate CSS rules.
235
+ * Tests that 'optimized' CSS is output, that is, that duplicate CSS rules are combined under their corresponding selectors.
236
+ *
237
+ * @covers ::get_css
203
238
  */
204
- public function test_combine_css_rules() {
239
+ public function test_should_optimize_css_output_by_default() {
205
240
  $a_sweet_rule = new WP_Style_Engine_CSS_Rule(
206
241
  '.a-sweet-rule',
207
242
  array(
@@ -221,15 +256,18 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
221
256
  $a_sweet_processor = new WP_Style_Engine_Processor();
222
257
  $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule ) );
223
258
 
224
- $this->assertEquals(
259
+ $this->assertSame(
225
260
  '.a-sweet-rule,#an-even-sweeter-rule > marquee{color:var(--sweet-color);background-color:purple;}',
226
261
  $a_sweet_processor->get_css( array( 'prettify' => false ) )
227
262
  );
228
263
  }
229
- /**
230
- * Should combine and store CSS rules.
231
- */
232
- public function test_combine_previously_added_css_rules() {
264
+
265
+ /**
266
+ * Tests that incoming CSS rules are merged with existing CSS rules.
267
+ *
268
+ * @covers ::add_rules
269
+ */
270
+ public function test_should_combine_previously_added_css_rules() {
233
271
  $a_lovely_processor = new WP_Style_Engine_Processor();
234
272
  $a_lovely_rule = new WP_Style_Engine_CSS_Rule(
235
273
  '.a-lovely-rule',
@@ -245,7 +283,11 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
245
283
  )
246
284
  );
247
285
  $a_lovely_processor->add_rules( $a_lovelier_rule );
248
- $this->assertEquals( '.a-lovely-rule,.a-lovelier-rule{border-color:purple;}', $a_lovely_processor->get_css( array( 'prettify' => false ) ) );
286
+ $this->assertSame(
287
+ '.a-lovely-rule,.a-lovelier-rule{border-color:purple;}',
288
+ $a_lovely_processor->get_css( array( 'prettify' => false ) ),
289
+ 'Return value of get_css() does not match expectations when combining 2 CSS rules'
290
+ );
249
291
 
250
292
  $a_most_lovely_rule = new WP_Style_Engine_CSS_Rule(
251
293
  '.a-most-lovely-rule',
@@ -263,9 +305,10 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase {
263
305
  );
264
306
  $a_lovely_processor->add_rules( $a_perfectly_lovely_rule );
265
307
 
266
- $this->assertEquals(
308
+ $this->assertSame(
267
309
  '.a-lovely-rule,.a-lovelier-rule,.a-most-lovely-rule,.a-perfectly-lovely-rule{border-color:purple;}',
268
- $a_lovely_processor->get_css( array( 'prettify' => false ) )
310
+ $a_lovely_processor->get_css( array( 'prettify' => false ) ),
311
+ 'Return value of get_css() does not match expectations when combining 4 CSS rules'
269
312
  );
270
313
  }
271
314
  }