forked from silverstripe/silverstripe-linkfield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiLinkFieldTest.php
More file actions
90 lines (83 loc) · 2.84 KB
/
Copy pathMultiLinkFieldTest.php
File metadata and controls
90 lines (83 loc) · 2.84 KB
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
<?php
namespace SilverStripe\LinkField\Tests\Form;
use ArrayIterator;
use ReflectionMethod;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\LinkField\Form\MultiLinkField;
use SilverStripe\Model\List\ArrayList;
use PHPUnit\Framework\Attributes\DataProvider;
class MultiLinkFieldTest extends SapphireTest
{
public static function getDataForProvider(): array
{
return [
'empty string' => [
'value' => '',
'expected' => [],
],
'non-comma-separated string' => [
'value' => 'this is a string',
'expected' => ['this is a string'],
],
'non-comma-separated non-numeric string' => [
'value' => ' 1, a, 2',
'expected' => ['1, a, 2'],
],
'comma-separated string' => [
'value' => '1,2,3,4',
'expected' => [1, 2, 3, 4],
],
'comma-separated string with whitesapce' => [
'value' => " 1,2 , 3, 4 \n ",
'expected' => [1, 2, 3, 4],
],
'number' => [
'value' => 1234,
'expected' => [1234],
],
'arraylist' => [
'value' => new ArrayList([['ID' => 1], ['ID' => 54]]),
'expected' => [1, 54],
],
'non-array iterable' => [
'value' => new ArrayIterator([1, 'string', []]),
'expected' => [1, 'string', []],
],
'empty array' => [
'value' => [],
'expected' => [],
],
'array with values' => [
'value' => [1, 'string', []],
'expected' => [1, 'string', []],
],
];
}
public static function provideConvertValueToArray(): array
{
return MultiLinkFieldTest::getDataForProvider();
}
#[DataProvider('provideConvertValueToArray')]
public function testConvertValueToArray(mixed $value, array $expected): void
{
$field = new MultiLinkField('');
$reflectionMethod = new ReflectionMethod($field, 'convertValueToArray');
$this->assertSame($expected, $reflectionMethod->invoke($field, $value));
}
public static function provideSetSubmittedValue(): array
{
return array_merge(MultiLinkFieldTest::getDataForProvider(), [
'comma-separated string with brackets' => [
'value' => '[1,2,3,4]',
'expected' => [1, 2, 3, 4],
],
]);
}
#[DataProvider('provideSetSubmittedValue')]
public function testSetSubmittedValue(mixed $value, array $expected): void
{
$field = new MultiLinkField('');
$field->setSubmittedValue($value);
$this->assertSame($expected, $field->getValue($value));
}
}