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
+ $ model = Model::create ()->set ('array ' , $ this ->array );
20
+ $ data = array ('array ' => $ this ->array );
21
+ $ callback = 'myFunc ' ;
22
+
23
+ //setup
24
+ $ view = JsonPView::create ();
25
+
26
+ try {
27
+ // empty js callback function name
28
+ $ view ->toString ($ model );
29
+
30
+ $ this ->fail ('empty callback javascript function name expected! ' );
31
+ } catch (WrongArgumentException $ e ) {}
32
+
33
+ try {
34
+ $ view ->setCallback ('34_callback ' ); // invalid javascript function name
35
+
36
+ $ this ->fail ('invalid javascript function name expected! ' );
37
+ } catch (WrongArgumentException $ e ) {}
38
+
39
+ $ view ->setCallback ($ callback );
40
+
41
+ $ this ->assertEquals ($ this ->makeString ($ callback , $ data ), $ view ->toString ($ model ) );
42
+
43
+ $ simpleStringableObject = SimpleStringableObject::create ()->setString ($ callback );
44
+
45
+ $ view ->setCallback ($ simpleStringableObject );
46
+
47
+ $ this ->assertEquals ($ this ->makeString ($ callback , $ data ), $ view ->toString ($ model ) );
48
+ }
49
+
50
+ /**
51
+ * @param $callback
52
+ * @param $data
53
+ * @return string
54
+ */
55
+ protected function makeString ($ callback , $ data )
56
+ {
57
+ return $ callback .'( ' .json_encode (
58
+ $ data
59
+ ).'); ' ;
60
+ }
61
+
62
+ }
63
+
64
+ class SimpleStringableObject implements Stringable
65
+ {
66
+ protected $ string = null ;
67
+
68
+
69
+ /**
70
+ * @static
71
+ * @return SimpleStringableObject
72
+ */
73
+ public static function create ()
74
+ {
75
+ return new self ();
76
+ }
77
+
78
+ /**
79
+ * @param $value
80
+ * @return SimpleStringableObject
81
+ */
82
+ public function setString ($ value )
83
+ {
84
+ Assert::isString ($ value );
85
+
86
+ $ this ->string = $ value ;
87
+
88
+ return $ this ;
89
+ }
90
+
91
+ /**
92
+ * @return str
93
+ */
94
+ public function toString ()
95
+ {
96
+ return $ this ->string ;
97
+ }
98
+ }
99
+ ?>
0 commit comments