|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Knp\RadBundle\Form; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpec\Exception\Example\PendingException; |
|
6
|
|
|
use PhpSpec\ObjectBehavior; |
|
7
|
|
|
|
|
8
|
|
|
class FormManagerSpec extends ObjectBehavior |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @param Symfony\Component\HttpFoundation\RequestStack $requestStack |
|
12
|
|
|
* @param Symfony\Component\HttpFoundation\Request $request |
|
13
|
|
|
* @param Knp\RadBundle\Form\FormCreatorInterface $creator1 |
|
14
|
|
|
* @param Knp\RadBundle\Form\FormCreatorInterface $creator2 |
|
15
|
|
|
* @param Knp\RadBundle\Form\FormCreatorInterface $creator3 |
|
16
|
|
|
*/ |
|
17
|
|
|
function let($requestStack, $request, $creator1, $creator2, $creator3) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->beConstructedWith($requestStack); |
|
20
|
|
|
|
|
21
|
|
|
$requestStack->getCurrentRequest()->willReturn($request); |
|
22
|
|
|
$this->registerCreator($creator1, 2); |
|
23
|
|
|
$this->registerCreator($creator2, 3); |
|
24
|
|
|
$this->registerCreator($creator3, 1); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
function it_should_be_able_to_register_creators() |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
$this->getCreators()->shouldHaveCount(3); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function it_should_be_able_to_register_creators_orderly($creator1, $creator2, $creator3) |
|
|
|
|
|
|
33
|
|
|
{ |
|
34
|
|
|
$this->getCreators()->shouldReturn(array($creator2, $creator1, $creator3)); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_should_be_able_to_return_creators_many_times($creator1, $creator2, $creator3) |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$this->getCreators(); |
|
40
|
|
|
$this->getCreators()->shouldReturn(array($creator2, $creator1, $creator3)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param stdClass $object |
|
45
|
|
|
*/ |
|
46
|
|
|
function it_should_try_to_create_form_with_registered_creators($object, $creator1, $creator2) |
|
|
|
|
|
|
47
|
|
|
{ |
|
48
|
|
|
$creator2->create($object, 'edit', array())->willReturn(null)->shouldBeCalled(); |
|
49
|
|
|
$creator1->create($object, 'edit', array())->willReturn(true)->shouldBeCalled(); |
|
50
|
|
|
|
|
51
|
|
|
$this->createObjectForm($object, 'edit'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param stdClass $object |
|
56
|
|
|
*/ |
|
57
|
|
|
function its_createObjectForm_should_throw_exception_if_no_creator_fits($object, $creator1, $creator2, $creator3) |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
$creator1->create($object, 'edit', array())->willReturn(null)->shouldBeCalled(); |
|
60
|
|
|
$creator2->create($object, 'edit', array())->willReturn(null)->shouldBeCalled(); |
|
61
|
|
|
$creator3->create($object, 'edit', array())->willReturn(null)->shouldBeCalled(); |
|
62
|
|
|
|
|
63
|
|
|
$this->shouldThrow()->duringCreateObjectForm($object, 'edit'); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param stdClass $object |
|
68
|
|
|
*/ |
|
69
|
|
|
function it_should_return_first_non_null_result_from_creator($object, $creator1, $creator2, $creator3) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
$creator2->create($object, 'edit', array())->willReturn(null)->shouldBeCalled(); |
|
72
|
|
|
$creator1->create($object, 'edit', array())->willReturn(true)->shouldBeCalled(); |
|
73
|
|
|
$creator3->create($object, 'edit', array())->shouldNotBeCalled(); |
|
74
|
|
|
|
|
75
|
|
|
$this->createObjectForm($object, 'edit'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param stdClass $object |
|
80
|
|
|
* @param Symfony\Component\Form\Form $form |
|
81
|
|
|
*/ |
|
82
|
|
|
function it_should_be_able_to_bind_unsafe_requests($object, $request, $form, $creator2) |
|
|
|
|
|
|
83
|
|
|
{ |
|
84
|
|
|
$request->isMethodSafe()->willReturn(false); |
|
85
|
|
|
$request->getMethod()->willReturn('PUT'); |
|
86
|
|
|
$creator2->create($object, null, array('method' => 'PUT'))->willReturn($form)->shouldBeCalled(); |
|
87
|
|
|
$form->handleRequest($request)->shouldBeCalled(); |
|
88
|
|
|
$form->submit($request)->shouldNotBeCalled(); |
|
89
|
|
|
|
|
90
|
|
|
$this->createBoundObjectForm($object); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param stdClass $object |
|
95
|
|
|
* @param Symfony\Component\Form\Form $form |
|
96
|
|
|
*/ |
|
97
|
|
|
function it_should_not_bind_get_safe_requests($object, $request, $form, $creator2) |
|
|
|
|
|
|
98
|
|
|
{ |
|
99
|
|
|
$request->isMethodSafe()->willReturn(true); |
|
100
|
|
|
$request->getMethod()->willReturn('GET'); |
|
101
|
|
|
$creator2->create($object, null, array())->willReturn($form)->shouldBeCalled(); |
|
102
|
|
|
$form->handleRequest($request)->shouldBeCalled(); |
|
103
|
|
|
$form->submit($request)->shouldNotBeCalled(); |
|
104
|
|
|
|
|
105
|
|
|
$this->createBoundObjectForm($object); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|