forked from Automattic/vip-go-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettingsValidatorTest.php
89 lines (82 loc) · 1.82 KB
/
SettingsValidatorTest.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
<?php
declare(strict_types = 1);
namespace MyNamespace\TestDemo;
use WP_UnitTestCase;
/**
* @covers MyNamespace\TestDemo\SettingsValidator
* @uses \MyNamespace\TestDemo\Settings
* @uses \MyNamespace\TestDemo\InputFactory::__construct
* @psalm-import-type SettingsArray from Settings
*/
class SettingsValidatorTest extends WP_UnitTestCase {
/**
* @dataProvider data_sanitize
* @uses \MyNamespace\TestDemo\AdminSettings
* @param mixed $value
* @psalm-param SettingsArray $expected
*/
public function test_sanitize( $value, array $expected ): void {
AdminSettings::get_instance()->register_settings();
update_option( Settings::OPTIONS_KEY, $value );
/** @var mixed */
$actual = get_option( Settings::OPTIONS_KEY );
static::assertEquals( $expected, $actual );
}
/**
* @psalm-return iterable<array-key, array{mixed, SettingsArray}>
*/
public function data_sanitize(): iterable {
return [
[
'',
[
'enabled' => false,
'message' => 'Hello',
],
],
[
[ 'enabled' => '1' ],
[
'enabled' => true,
'message' => 'Hello',
],
],
[
[ 'extra' => 'abcdef' ],
[
'enabled' => false,
'message' => 'Hello',
],
],
];
}
/**
* @dataProvider data_ensure_data_shape
* @param mixed[] $value
* @psalm-param SettingsArray $expected
*/
public function test_ensure_data_shape( array $value, array $expected ): void {
$actual = SettingsValidator::ensure_data_shape( $value );
static::assertEquals( $expected, $actual );
}
/**
* @psalm-return iterable<array-key, array{mixed[], SettingsArray}>
*/
public function data_ensure_data_shape(): iterable {
return [
[
[],
Settings::defaults(),
],
[
[
'message' => 20,
'extra' => true,
],
[
'message' => '20',
] + Settings::defaults(),
],
];
}
}