55use App \Widgets \Profile \TestNamespace \TestFeed ;
66use App \Widgets \TestDefaultSlider ;
77use App \Widgets \TestMyClass ;
8+ use App \Widgets \TestRepeatableFeed ;
89use App \Widgets \TestWidgetWithDIInRun ;
910use App \Widgets \TestWidgetWithParamsInRun ;
1011use Arrilot \Widgets \Misc \Wrapper ;
12+ use Arrilot \Widgets \WidgetId ;
1113use PhpSpec \ObjectBehavior ;
1214use Prophecy \Argument ;
1315use spec \Arrilot \Widgets \Dummies \Slider ;
1416
1517class WidgetFactorySpec extends ObjectBehavior
1618{
19+ /**
20+ * A mock for producing JS object for ajax.
21+ *
22+ * @param $widgetName
23+ * @param $widgetParams
24+ *
25+ * @return string
26+ */
27+ private function mockProduceJavascriptData ($ widgetName , $ widgetParams = [])
28+ {
29+ return json_encode ([
30+ 'id ' => 1 ,
31+ 'name ' => $ widgetName ,
32+ 'params ' => serialize ($ widgetParams ),
33+ '_token ' => 'token_stub ' ,
34+ 'skip_widget_container ' => 1 ,
35+ ]);
36+ }
37+
1738 protected $ config = [
1839 'defaultNamespace ' => 'App\Widgets ' ,
1940 'customNamespaces ' => [
20- 'slider ' => 'spec\Arrilot\Widgets\Dummies ' ,
21- 'testWidgetName ' => '' ,
41+ 'slider ' => 'spec\Arrilot\Widgets\Dummies ' ,
42+ 'testRepeatableFeed ' => 'spec\Arrilot\Widgets\Dummies ' ,
43+ 'testWidgetName ' => '' ,
2244 ],
2345 ];
2446
2547 public function let (Wrapper $ wrapper )
2648 {
2749 $ this ->beConstructedWith ($ this ->config , $ wrapper );
50+ WidgetId::reset ();
2851 }
2952
3053 public function it_is_initializable ()
@@ -37,23 +60,32 @@ public function it_can_run_widget_from_default_namespace(Wrapper $wrapper)
3760 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
3861 call_user_func_array ([new TestDefaultSlider ([]), 'run ' ], [])
3962 );
40- $ this ->testDefaultSlider ()->shouldReturn ("Default test slider was executed with \$slides = 6 " );
63+ $ this ->testDefaultSlider ()
64+ ->shouldReturn (
65+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Default test slider was executed with $slides = 6</span> '
66+ );
4167 }
4268
4369 public function it_can_run_widget_from_custom_namespace (Wrapper $ wrapper )
4470 {
4571 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
4672 call_user_func_array ([new Slider ([]), 'run ' ], [])
4773 );
48- $ this ->slider ()->shouldReturn ("Slider was executed with \$slides = 6 " );
74+ $ this ->slider ()
75+ ->shouldReturn (
76+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Slider was executed with $slides = 6</span> '
77+ );
4978 }
5079
5180 public function it_provides_config_override (Wrapper $ wrapper )
5281 {
5382 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
5483 call_user_func_array ([new Slider (['slides ' => 5 ]), 'run ' ], ['slides ' => 5 ])
5584 );
56- $ this ->slider (['slides ' => 5 ])->shouldReturn ("Slider was executed with \$slides = 5 " );
85+ $ this ->slider (['slides ' => 5 ])
86+ ->shouldReturn (
87+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Slider was executed with $slides = 5</span> '
88+ );
5789 }
5890
5991 public function it_throws_exception_for_bad_widget_class ()
@@ -66,46 +98,101 @@ public function it_can_run_widgets_with_additional_params(Wrapper $wrapper)
6698 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
6799 call_user_func_array ([new TestWidgetWithParamsInRun ([]), 'run ' ], ['asc ' ])
68100 );
69- $ this ->testWidgetWithParamsInRun ([], 'asc ' )->shouldReturn ("TestWidgetWithParamsInRun was executed with \$flag = asc " );
101+ $ this ->testWidgetWithParamsInRun ([], 'asc ' )
102+ ->shouldReturn (
103+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">TestWidgetWithParamsInRun was executed with $flag = asc</span> '
104+ );
70105 }
71106
72107 public function it_can_run_widgets_with_method_injection (Wrapper $ wrapper )
73108 {
74109 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
75110 call_user_func_array ([new TestWidgetWithDIInRun ([]), 'run ' ], [new TestMyClass ()])
76111 );
77- $ this ->testWidgetWithParamsInRun ()->shouldReturn ('bar ' );
112+ $ this ->testWidgetWithParamsInRun ()
113+ ->shouldReturn (
114+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">bar</span> '
115+ );
78116 }
79117
80118 public function it_can_run_widgets_with_run_method (Wrapper $ wrapper )
81119 {
82120 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
83121 call_user_func_array ([new TestDefaultSlider ([]), 'run ' ], [])
84122 );
85- $ this ->run ('testDefaultSlider ' )->shouldReturn ("Default test slider was executed with \$slides = 6 " );
123+ $ this ->run ('testDefaultSlider ' )
124+ ->shouldReturn (
125+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Default test slider was executed with $slides = 6</span> '
126+ );
86127 }
87128
88129 public function it_can_run_widgets_with_run_method_and_config_override (Wrapper $ wrapper )
89130 {
90131 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
91132 call_user_func_array ([new Slider (['slides ' => 5 ]), 'run ' ], ['slides ' => 5 ])
92133 );
93- $ this ->run ('slider ' , ['slides ' => 5 ])->shouldReturn ("Slider was executed with \$slides = 5 " );
134+ $ this ->run ('slider ' , ['slides ' => 5 ])
135+ ->shouldReturn (
136+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Slider was executed with $slides = 5</span> '
137+ );
94138 }
95139
96140 public function it_can_run_nested_widgets (Wrapper $ wrapper )
97141 {
98142 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
99143 call_user_func_array ([new TestFeed ([]), 'run ' ], [])
100144 );
101- $ this ->run ('Profile\TestNamespace\TestFeed ' , ['slides ' => 5 ])->shouldReturn ("Feed was executed with \$slides = 6 " );
145+ $ this ->run ('Profile\TestNamespace\TestFeed ' , ['slides ' => 5 ])
146+ ->shouldReturn (
147+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Feed was executed with $slides = 6</span> '
148+ );
102149 }
103150
104151 public function it_can_run_nested_widgets_with_dot_notation (Wrapper $ wrapper )
105152 {
106153 $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
107154 call_user_func_array ([new TestFeed ([]), 'run ' ], [])
108155 );
109- $ this ->run ('profile.testNamespace.testFeed ' , ['slides ' => 5 ])->shouldReturn ("Feed was executed with \$slides = 6 " );
156+ $ this ->run ('profile.testNamespace.testFeed ' , ['slides ' => 5 ])
157+ ->shouldReturn (
158+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Feed was executed with $slides = 6</span> '
159+ );
160+ }
161+
162+ public function it_can_run_multiple_widgets (Wrapper $ wrapper )
163+ {
164+ $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
165+ call_user_func_array ([new Slider ([]), 'run ' ], [])
166+ );
167+ $ this ->slider ()
168+ ->shouldReturn (
169+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Slider was executed with $slides = 6</span> '
170+ );
171+
172+ $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
173+ call_user_func_array ([new Slider (['slides ' => 5 ]), 'run ' ], ['slides ' => 5 ])
174+ );
175+ $ this ->slider (['slides ' => 5 ])
176+ ->shouldReturn (
177+ '<span id="arrilot-widget-container-2" class="arrilot-widget-container">Slider was executed with $slides = 5</span> '
178+ );
179+ }
180+
181+ public function it_can_run_async_widget (Wrapper $ wrapper )
182+ {
183+ $ config = [];
184+ $ params = [$ config ];
185+
186+ $ wrapper ->csrf_token ()->willReturn ('token_stub ' );
187+ $ wrapper ->appCall (Argument::any (), Argument::any ())->willReturn (
188+ call_user_func_array ([new TestRepeatableFeed ([]), 'run ' ], [])
189+ );
190+
191+ $ this ->testRepeatableFeed ($ config )
192+ ->shouldReturn (
193+ '<span id="arrilot-widget-container-1" class="arrilot-widget-container">Feed was executed with $slides = 6 ' .
194+ '<script type="text/javascript">setTimeout( function() { $( \'#arrilot-widget-container-1 \').load( \'/arrilot/load-widget \', ' .$ this ->mockProduceJavascriptData ('TestRepeatableFeed ' , $ params ).') }, 10000)</script> ' .
195+ '</span> '
196+ );
110197 }
111198}
0 commit comments