|
7 | 7 | namespace Ray\WebFormModule; |
8 | 8 |
|
9 | 9 | use Doctrine\Common\Annotations\AnnotationReader; |
10 | | -use Ray\Aop\Arguments; |
11 | 10 | use Ray\Aop\ReflectiveMethodInvocation; |
| 11 | +use Ray\Di\AbstractModule; |
| 12 | +use Ray\Di\Injector; |
| 13 | +use Ray\Di\InjectorInterface; |
12 | 14 | use Ray\WebFormModule\Exception\InvalidFormPropertyException; |
13 | | -use Ray\WebFormModule\Exception\InvalidOnFailureMethod; |
14 | 15 | use Ray\WebFormModule\Exception\ValidationException; |
15 | 16 |
|
16 | 17 | class AuraInputInterceptorTest extends \PHPUnit_Framework_TestCase |
17 | 18 | { |
18 | 19 | /** |
19 | | - * @var ReflectiveMethodInvocation |
| 20 | + * @var InjectorInterface |
20 | 21 | */ |
21 | | - private $methodInvocation; |
22 | | - |
23 | | - public function setUp() |
24 | | - { |
25 | | - parent::setUp(); |
26 | | - } |
| 22 | + private $injector; |
27 | 23 |
|
28 | 24 | /** |
29 | | - * @param $method |
| 25 | + * @var FakeController |
30 | 26 | */ |
31 | | - public function getMethodInvocation($method, array $submit, FailureHandlerInterface $handler = null) |
32 | | - { |
33 | | - $handler = $handler ?: new OnFailureMethodHandler; |
34 | | - $object = $this->getController($submit); |
| 27 | + private $controller; |
35 | 28 |
|
36 | | - return new ReflectiveMethodInvocation( |
37 | | - $object, |
38 | | - new \ReflectionMethod($object, $method), |
39 | | - new Arguments($submit), |
40 | | - [ |
41 | | - new AuraInputInterceptor(new AnnotationReader, $handler) |
42 | | - ] |
43 | | - ); |
44 | | - } |
45 | | - |
46 | | - public function getController(array $submit) |
| 29 | + public function setUp() |
47 | 30 | { |
48 | | - $controller = new FakeController; |
49 | | - /** @var $fakeForm FakeForm */ |
50 | | - $fakeForm = (new FormFactory)->newInstance(FakeForm::class); |
51 | | - $fakeForm->setSubmit($submit); |
52 | | - $controller->setForm($fakeForm); |
53 | | - |
54 | | - return $controller; |
| 31 | + $this->injector = new Injector(new class() extends AbstractModule { |
| 32 | + protected function configure() |
| 33 | + { |
| 34 | + $this->install(new AuraInputModule); |
| 35 | + $this->bind(FormInterface::class)->annotatedWith('contact_form')->to(FakeForm::class); |
| 36 | + } |
| 37 | + }); |
| 38 | + $this->controller = $this->injector->getInstance(FakeController::class); |
55 | 39 | } |
56 | 40 |
|
57 | | - public function proceed($controller) |
58 | | - { |
59 | | - $invocation = new ReflectiveMethodInvocation( |
60 | | - $controller, |
61 | | - new \ReflectionMethod($controller, 'createAction'), |
62 | | - new Arguments([]), |
63 | | - [ |
64 | | - new AuraInputInterceptor(new AnnotationReader, new OnFailureMethodHandler) |
65 | | - ] |
66 | | - ); |
67 | | - $invocation->proceed(); |
68 | | - } |
| 41 | +// /** |
| 42 | +// * @param $method |
| 43 | +// */ |
| 44 | +// public function getMethodInvocation(string $method, array $submit, FailureHandlerInterface $handler = null) |
| 45 | +// { |
| 46 | +// $handler = $handler ?: new OnFailureMethodHandler; |
| 47 | +// $object = $this->getController($submit); |
| 48 | +// |
| 49 | +// $invocation = new ReflectiveMethodInvocation( |
| 50 | +// $object, |
| 51 | +// $method, |
| 52 | +// $submit, |
| 53 | +// [ |
| 54 | +// new AuraInputInterceptor(new AnnotationReader, $handler) |
| 55 | +// ] |
| 56 | +// ); |
| 57 | +// |
| 58 | +// return $invocation; |
| 59 | +// } |
| 60 | +// |
| 61 | +// public function getController(array $submit) |
| 62 | +// { |
| 63 | +// $controller = new FakeController; |
| 64 | +// /** @var $fakeForm FakeForm */ |
| 65 | +// $fakeForm = (new FormFactory)->newInstance(FakeForm::class); |
| 66 | +// $fakeForm->setSubmit($submit); |
| 67 | +// $controller->setForm($fakeForm); |
| 68 | +// |
| 69 | +// return $controller; |
| 70 | +// } |
| 71 | +// |
| 72 | +// public function proceed($controller) |
| 73 | +// { |
| 74 | +// $invocation = new ReflectiveMethodInvocation( |
| 75 | +// $controller, |
| 76 | +// new \ReflectionMethod($controller, 'createAction'), |
| 77 | +// [], |
| 78 | +// [ |
| 79 | +// new AuraInputInterceptor(new AnnotationReader, new OnFailureMethodHandler) |
| 80 | +// ] |
| 81 | +// ); |
| 82 | +// $invocation->proceed(); |
| 83 | +// } |
69 | 84 |
|
70 | 85 | public function testProceedFailed() |
71 | 86 | { |
72 | | - $invocation = $this->getMethodInvocation('createAction', []); |
73 | | - $result = $invocation->proceed(); |
| 87 | + $result = $this->controller->createAction([]); |
74 | 88 | $this->assertSame('400', $result); |
75 | 89 | } |
76 | 90 |
|
77 | 91 | public function testProceed() |
78 | 92 | { |
79 | | - $invocation = $this->getMethodInvocation('createAction', ['BEAR']); |
80 | | - $result = $invocation->proceed(); |
| 93 | + $result = $this->controller->createAction('BEAR'); |
81 | 94 | $this->assertSame('201', $result); |
82 | 95 | } |
83 | 96 |
|
84 | 97 | public function invalidControllerProvider() |
85 | 98 | { |
86 | 99 | return [ |
87 | | - [new FakeInvalidController1], |
88 | | - [new FakeInvalidController2] |
| 100 | + [$this->injector->getInstance(FakeInvalidController1::class)], |
| 101 | + [$this->injector->getInstance(FakeInvalidController2::class)] |
89 | 102 | ]; |
90 | 103 | } |
91 | 104 |
|
92 | | - /** |
93 | | - * @dataProvider invalidControllerProvider |
94 | | - * |
95 | | - * @param $controller |
96 | | - */ |
97 | | - public function testInvalidFormPropertyByMissingProperty($controller) |
| 105 | + public function testInvalidFormPropertyByMissingProperty() |
98 | 106 | { |
99 | 107 | $this->setExpectedException(InvalidFormPropertyException::class); |
100 | | - $this->proceed($controller); |
| 108 | + $controller = $this->injector->getInstance(FakeInvalidController1::class); |
| 109 | + $controller->createAction(); |
| 110 | + } |
| 111 | + |
| 112 | + public function testInvalidFormPropertyByMissingProperty2() |
| 113 | + { |
| 114 | + $this->setExpectedException(InvalidFormPropertyException::class); |
| 115 | + $controller = $this->injector->getInstance(FakeInvalidController2::class); |
| 116 | + $controller->createAction(); |
101 | 117 | } |
102 | 118 |
|
103 | 119 | public function testInvalidFormPropertyException() |
104 | 120 | { |
105 | | - $this->setExpectedException(InvalidOnFailureMethod::class); |
106 | | - $controller = new FakeInvalidController3; |
107 | | - /** @var $fakeForm FakeForm */ |
108 | | - $fakeForm = (new FormFactory)->newInstance(FakeForm::class); |
109 | | - $fakeForm->setSubmit(['name' => '']); |
110 | | - $controller->setForm($fakeForm); |
111 | | - $this->proceed($controller); |
| 121 | + $this->setExpectedException(InvalidFormPropertyException::class); |
| 122 | + /** @var FakeInvalidController3 $controller */ |
| 123 | + $controller = $this->injector->getInstance(FakeInvalidController3::class); |
| 124 | + $controller->createAction(''); |
112 | 125 | } |
113 | 126 |
|
114 | 127 | public function testInvalidFormPropertyByInvalidInstance() |
115 | 128 | { |
116 | 129 | $this->setExpectedException(InvalidFormPropertyException::class); |
117 | | - $object = new FakeInvalidController1; |
118 | | - $invocation = new ReflectiveMethodInvocation( |
119 | | - $object, |
120 | | - new \ReflectionMethod($object, 'createAction'), |
121 | | - new Arguments(['name' => '']), |
122 | | - [ |
123 | | - new AuraInputInterceptor(new AnnotationReader, new OnFailureMethodHandler) |
124 | | - ] |
125 | | - ); |
126 | | - $invocation->proceed(); |
| 130 | + $this->setExpectedException(InvalidFormPropertyException::class); |
| 131 | + $controller = $this->injector->getInstance(FakeInvalidController1::class); |
| 132 | + $controller->createAction(''); |
127 | 133 | } |
128 | 134 |
|
129 | 135 | public function testProceedWithVndErrorHandler() |
130 | 136 | { |
| 137 | + /** @var FakeController $controller */ |
| 138 | + $controller = $this->injector->getInstance(FakeController::class); |
131 | 139 | try { |
132 | | - $invocation = $this->getMethodInvocation('createAction', [], new VndErrorHandler(new AnnotationReader)); |
133 | | - $invocation->proceed(); |
| 140 | + $controller->createAction(''); |
134 | 141 | } catch (ValidationException $e) { |
135 | 142 | $this->assertInstanceOf(FormValidationError::class, $e->error); |
136 | 143 | $json = (string) $e->error; |
|
0 commit comments