1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Tests; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Dev\SapphireTest; |
7
|
|
|
use SilverStripe\Forms\Form; |
8
|
|
|
use SilverStripe\Forms\SegmentField; |
9
|
|
|
use SilverStripe\Forms\SegmentFieldModifier\AbstractSegmentFieldModifier; |
10
|
|
|
|
11
|
|
|
class SegmentFieldTest extends SapphireTest |
12
|
|
|
{ |
13
|
|
|
public function testSuggest() |
14
|
|
|
{ |
15
|
|
|
$field = new SegmentField('Example'); |
16
|
|
|
|
17
|
|
|
$field->setModifiers(array( |
18
|
|
|
array('array-preview', 'array-suggestion'), |
19
|
|
|
SegmentFieldTestModifier::create(), |
20
|
|
|
)); |
21
|
|
|
|
22
|
|
|
$encoded = $field->suggest($this->getNewRequestMock()); |
23
|
|
|
$decoded = json_decode($encoded); |
24
|
|
|
|
25
|
|
|
$this->assertEquals('ARRAY-SUGGESTION', $decoded->suggestion); |
26
|
|
|
$this->assertEquals('array-preview', $decoded->preview); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testGettersAndSetters() |
30
|
|
|
{ |
31
|
|
|
$field = new SegmentField('Example'); |
32
|
|
|
|
33
|
|
|
$modifier = SegmentFieldTestModifier::create(); |
34
|
|
|
|
35
|
|
|
$field->setModifiers(array( |
36
|
|
|
$modifier, |
37
|
|
|
)); |
38
|
|
|
|
39
|
|
|
$form = $this->getNewFormMock(); |
40
|
|
|
$request = $this->getNewRequestMock(); |
41
|
|
|
|
42
|
|
|
$field->setForm($form)->suggest($request); |
43
|
|
|
|
44
|
|
|
$modifiers = $field->getModifiers(); |
45
|
|
|
|
46
|
|
|
$this->assertSame($modifier, $modifiers[0]); |
47
|
|
|
$this->assertSame($form, $modifiers[0]->getForm()); |
48
|
|
|
$this->assertSame($field, $modifiers[0]->getField()); |
49
|
|
|
$this->assertSame($request, $modifiers[0]->getRequest()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return HTTPRequest |
54
|
|
|
*/ |
55
|
|
|
protected function getNewRequestMock() |
56
|
|
|
{ |
57
|
|
|
return new HTTPRequest('GET', '/'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return Form |
62
|
|
|
*/ |
63
|
|
|
protected function getNewFormMock() |
64
|
|
|
{ |
65
|
|
|
return $this->getMockBuilder(Form::class) |
66
|
|
|
->disableOriginalConstructor() |
67
|
|
|
->setMethods(null) |
68
|
|
|
->getMock(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|