-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
transformer-service.php
160 lines (146 loc) · 5.76 KB
/
transformer-service.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
<?php
/**
* TransformerService tests.
*
* @package WooCommerce\Admin\Tests\RemoteInboxNotifications
*/
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers\ArrayKeys;
use Automattic\WooCommerce\Admin\RemoteSpecs\RuleProcessors\Transformers\TransformerService;
/**
* class WC_Admin_Tests_RemoteInboxNotifications_TransformerService
*/
class WC_Admin_Tests_RemoteInboxNotifications_TransformerService extends WC_Unit_Test_Case {
/**
* Test it creates a transformer with snake case 'use' value
*/
public function test_it_creates_a_transformer_with_snake_case_use_value() {
$array_keys = TransformerService::create_transformer( 'array_keys' );
$this->assertInstanceOf( ArrayKeys::class, $array_keys );
}
/**
* Test it returns null when a transformer is not found.
*/
public function test_it_returns_null_when_transformer_is_not_found() {
$transformer = TransformerService::create_transformer( 'i_do_not_exist' );
$this->assertNull( $transformer );
}
/**
* @testdox An exception is thrown when the transformer config is missing 'use'
*/
public function test_it_throw_exception_when_transformer_config_is_missing_use() {
$this->expectException( InvalidArgumentException::class );
$this->expectExceptionMessage( 'Missing required config value: use' );
TransformerService::apply( array( 'value' ), array( new stdClass() ), false, null );
}
/**
* @testdox An exception is thrown when the transformer is not found
*/
public function test_it_throws_exception_when_transformer_is_not_found() {
$this->expectExceptionMessage( 'Unable to find a transformer by name: i_do_not_exist' );
$transformer = $this->transformer_config( 'i_do_not_exist' );
TransformerService::apply( array( 'value' ), array( $transformer ), false, null );
}
/**
* Given two transformers
* When the second transformer returns null
* Then the default value should be returned.
*/
public function test_it_returns_default_when_transformer_returns_null() {
$dot_notation = $this->transformer_config( 'dot_notation', array( 'path' => 'industries' ) );
$array_search = $this->transformer_config( 'array_search', array( 'value' => 'i_do_not_exist' ) );
$items = array(
'industries' => array( 'item1', 'item2' ),
);
$result = TransformerService::apply( $items, array( $dot_notation, $array_search ), true, 'default' );
$this->assertEquals( $result, 'default' );
}
/**
* Given two transformers
* When the second transformer returns null but no default is set
* Then the final result should returned.
*/
public function test_it_returns_null_when_transformer_returns_null() {
$dot_notation = $this->transformer_config( 'dot_notation', array( 'path' => 'industries' ) );
$array_search = $this->transformer_config( 'array_search', array( 'value' => 'i_do_not_exist' ) );
$items = array(
'industries' => array( 'item1', 'item2' ),
);
$result = TransformerService::apply( $items, array( $dot_notation, $array_search ), false, null );
$this->assertEquals( $result, null );
}
/**
* Given two transformers
* When the transformed value type is different from the default value type and default is set
* Then the default value should be returned.
*/
public function test_it_returns_default_when_transformer_returns_different_type() {
$dot_notation = $this->transformer_config( 'dot_notation', array( 'path' => 'industries' ) );
$items = array(
'industries' => array(),
);
$result = TransformerService::apply( $items, array( $dot_notation ), true, 'clothing' );
$this->assertEquals( $result, 'clothing' );
}
/**
* Given two transformers
* When the transformed value type is the same with the default value type and default is set
* Then the transformed value should be returned.
*/
public function test_it_returns_default_when_transformer_returns_same_type() {
$dot_notation = $this->transformer_config( 'dot_notation', array( 'path' => 'industries' ) );
$items = array(
'industries' => 'food_and_beverage',
);
$result = TransformerService::apply( $items, array( $dot_notation ), true, 'clothing' );
$this->assertEquals( $result, 'food_and_beverage' );
}
/**
* Given a nested array
* When it uses DotNotation to select 'teams'
* When it uses ArrayColumn to select 'members' in 'teams'
* When it uses ArrayFlatten to flatten 'members'
* When it uses ArraySearch to select 'mothra-member'
* Then 'mothra-member' should be returned.
*/
public function test_it_returns_transformed_value() {
// Given.
$items = array(
'teams' => array(
array(
'name' => 'mothra',
'members' => array( 'mothra-member' ),
),
array(
'name' => 'gezora',
'members' => array( 'gezora-member' ),
),
array(
'name' => 'ghidorah',
'members' => array( 'ghidorah-member' ),
),
),
);
// When.
$dot_notation = $this->transformer_config( 'dot_notation', array( 'path' => 'teams' ) );
$array_column = $this->transformer_config( 'array_column', array( 'key' => 'members' ) );
$array_flatten = $this->transformer_config( 'array_flatten' );
$array_search = $this->transformer_config( 'array_search', array( 'value' => 'mothra-member' ) );
$result = TransformerService::apply( $items, array( $dot_notation, $array_column, $array_flatten, $array_search ), false, null );
// Then.
$this->assertEquals( 'mothra-member', $result );
}
/**
* Creates transformer config object
*
* @param string $name name of the transformer in snake_case.
* @param array $arguments transformer arguments.
*
* @return stdClass
*/
private function transformer_config( $name, array $arguments = array() ) {
$transformer = new stdClass();
$transformer->use = $name;
$transformer->arguments = (object) $arguments;
return $transformer;
}
}