-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathclass-wp-style-engine-css-rules-store-test.php
189 lines (158 loc) · 6.45 KB
/
class-wp-style-engine-css-rules-store-test.php
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* Tests the Style Engine CSS Rules Store class.
*
* @package Gutenberg
* @subpackage style-engine
*/
/**
* Tests for registering, storing and retrieving a collection of CSS Rules (a store).
*
* @group style-engine
* @coversDefaultClass WP_Style_Engine_CSS_Rules_Store_Gutenberg
*/
class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
/**
* Cleans up stores after each test.
*/
public function tear_down() {
WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores();
parent::tear_down();
}
/**
* Tests creating a new store on instantiation.
*
* @covers ::__construct
*/
public function test_should_create_new_store_on_instantiation() {
$new_pancakes_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'pancakes-with-strawberries' );
$this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store_Gutenberg', $new_pancakes_store );
}
/**
* Tests that a `$store_name` argument is required and no store will be created without one.
*
* @covers ::get_store
*/
public function test_should_not_create_store_without_a_store_name() {
$not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( '' );
$this->assertEmpty( $not_a_store, 'get_store() did not return an empty value with empty string as argument.' );
$also_not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 123 );
$this->assertEmpty( $also_not_a_store, 'get_store() did not return an empty value with number as argument.' );
$definitely_not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( null );
$this->assertEmpty( $definitely_not_a_store, 'get_store() did not return an empty value with `null` as argument.' );
}
/**
* Tests returning a previously created store when the same selector key is passed.
*
* @covers ::get_store
*/
public function test_should_return_existing_store() {
$new_fish_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'fish-n-chips' );
$selector = '.haddock';
$new_fish_store->add_rule( $selector );
$this->assertSame( $selector, $new_fish_store->add_rule( $selector )->get_selector(), 'Selector string of store rule does not match expected value' );
$the_same_fish_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'fish-n-chips' );
$this->assertSame( $selector, $the_same_fish_store->add_rule( $selector )->get_selector(), 'Selector string of existing store rule does not match expected value' );
}
/**
* Tests returning all previously created stores.
*
* @covers ::get_stores
*/
public function test_should_get_all_existing_stores() {
$burrito_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'burrito' );
$quesadilla_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'quesadilla' );
$this->assertEquals(
array(
'burrito' => $burrito_store,
'quesadilla' => $quesadilla_store,
),
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores()
);
}
/**
* Tests that all previously created stores are deleted.
*
* @covers ::remove_all_stores
*/
public function test_should_remove_all_stores() {
$dolmades_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'dolmades' );
$tzatziki_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'tzatziki' );
$this->assertEquals(
array(
'dolmades' => $dolmades_store,
'tzatziki' => $tzatziki_store,
),
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(),
'Return value of get_stores() does not match expectation'
);
WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores();
$this->assertEquals(
array(),
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(),
'Return value of get_stores() is not an empty array after remove_all_stores() called.'
);
}
/**
* Tests adding rules to an existing store.
*
* @covers ::add_rule
*/
public function test_should_add_rule_to_existing_store() {
$new_pie_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'meat-pie' );
$selector = '.wp-block-sauce a:hover';
$store_rule = $new_pie_store->add_rule( $selector );
$expected = '';
$this->assertSame( $expected, $store_rule->get_css(), 'Return value of get_css() is not a empty string where a rule has no CSS declarations.' );
$pie_declarations = array(
'color' => 'brown',
'border-color' => 'yellow',
'border-radius' => '10rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $pie_declarations );
$store_rule->add_declarations( $css_declarations );
$store_rule = $new_pie_store->add_rule( $selector );
$expected = "$selector{{$css_declarations->get_declarations_string()}}";
$this->assertSame( $expected, $store_rule->get_css(), 'Return value of get_css() does not match expected CSS from existing store rules.' );
}
/**
* Tests that all stored rule objects are returned.
*
* @covers ::get_all_rules
*/
public function test_should_get_all_rule_objects_for_a_store() {
$new_pizza_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'pizza-with-mozzarella' );
$selector = '.wp-block-anchovies a:hover';
$store_rule = $new_pizza_store->add_rule( $selector );
$expected = array(
$selector => $store_rule,
);
$this->assertSame( $expected, $new_pizza_store->get_all_rules(), 'Return value for get_all_rules() does not match expectations.' );
$new_selector = '.wp-block-mushroom a:hover';
$newer_pizza_declarations = array(
'padding' => '100px',
);
$new_store_rule = $new_pizza_store->add_rule( $new_selector );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $newer_pizza_declarations );
$new_store_rule->add_declarations( array( $css_declarations ) );
$expected = array(
$selector => $store_rule,
$new_selector => $new_store_rule,
);
$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.' );
}
/**
* Tests adding rules group keys to store.
*
* @covers ::add_rule
*/
public function test_should_store_as_concatenated_rules_groups_and_selector() {
$store_one = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'one' );
$store_one_rule = $store_one->add_rule( '.tony', '.one' );
$this->assertSame(
'.one .tony',
"{$store_one_rule->get_rules_group()} {$store_one_rule->get_selector()}",
'add_rule() does not concatenate rules group and selector.'
);
}
}