1
+ <?php
2
+ /***************************************************************************
3
+ * Copyright (C) 2012 by Georgiy T. Kutsurua *
4
+ * *
5
+ * This program is free software; you can redistribute it and/or modify *
6
+ * it under the terms of the GNU Lesser General Public License as *
7
+ * published by the Free Software Foundation; either version 3 of the *
8
+ * License, or (at your option) any later version. *
9
+ * *
10
+ ***************************************************************************/
11
+
12
+ final class JsonPViewTest extends TestCase
13
+ {
14
+ protected $ array = array ('<foo> ' ,"'bar' " ,'"baz" ' ,'&blong& ' );
15
+
16
+
17
+ public function testMain ()
18
+ {
19
+ $ this ->execCallback ('myCallback ' );
20
+
21
+ try {
22
+ $ this ->execCallback ('' ); // empty js callback function name
23
+
24
+ $ this ->fail ('empty callback javascript function name expected! ' );
25
+ } catch (WrongArgumentException $ e ) {}
26
+
27
+ try {
28
+ $ this ->execCallback ('34_callback ' ); // invalid javascript function name
29
+
30
+ $ this ->fail ('invalid javascript function name expected! ' );
31
+ } catch (WrongArgumentException $ e ) {}
32
+
33
+ }
34
+
35
+ protected function execCallback ($ callback )
36
+ {
37
+ Assert::isScalar ($ callback );
38
+
39
+ $ model = Model::create ()->set ('array ' , $ this ->array );
40
+ $ data = array ('array ' => $ this ->array );
41
+
42
+ //setup
43
+ $ view = JsonPView::create ()->setCallback ($ callback );
44
+
45
+ //execution and check
46
+ $ this ->assertEquals (
47
+ $ callback .'( ' .json_encode (
48
+ $ data
49
+ ).'); ' ,
50
+ $ view ->toString ($ model )
51
+ );
52
+
53
+ //setup from stringable object
54
+ $ view = JsonPView::create ()->setCallback (
55
+ SimpleStringableObject::create ()->setString ($ callback )
56
+ );
57
+
58
+ //execution and check
59
+ $ this ->assertEquals (
60
+ $ callback .'( ' .json_encode (
61
+ $ data
62
+ ).'); ' ,
63
+ $ view ->toString ($ model )
64
+ );
65
+
66
+ }
67
+
68
+ }
69
+
70
+ class SimpleStringableObject implements Stringable
71
+ {
72
+ protected $ string = null ;
73
+
74
+
75
+ /**
76
+ * @static
77
+ * @return SimpleStringableObject
78
+ */
79
+ public static function create ()
80
+ {
81
+ return new self ();
82
+ }
83
+
84
+ /**
85
+ * @param $value
86
+ * @return SimpleStringableObject
87
+ */
88
+ public function setString ($ value )
89
+ {
90
+ Assert::isString ($ value );
91
+
92
+ $ this ->string = $ value ;
93
+
94
+ return $ this ;
95
+ }
96
+
97
+ /**
98
+ * @return str
99
+ */
100
+ public function toString ()
101
+ {
102
+ return $ this ->string ;
103
+ }
104
+ }
105
+ ?>
0 commit comments