33require dirname (__FILE__ )."/../vendor/autoload.php " ;
44
55use Nathanmac \Utilities \Parser \Parser ;
6+ use \Mockery as m ;
67
78class ParserTest extends PHPUnit_Framework_TestCase
89{
10+
11+ protected function tearDown ()
12+ {
13+ m::close ();
14+ }
15+
916 /** @test */
1017 public function wildcards_with_simple_structure_json ()
1118 {
12- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
19+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
20+ ->shouldDeferMissing ()
21+ ->shouldAllowMockingProtectedMethods ();
1322
14- $ parser ->expects ($ this ->any ())
15- ->method ('getPayload ' )
16- ->
will (
$ this ->
returnValue (
'{"email": {"to": "[email protected] ", "from": "[email protected] ", "subject": "Hello World", "message": { "body": "Hello this is a sample message" }}} ' ));
23+ $ parser ->shouldReceive ('getPayload ' )
24+ ->
andReturn (
'{"email": {"to": "[email protected] ", "from": "[email protected] ", "subject": "Hello World", "message": { "body": "Hello this is a sample message" }}} ' );
1725
1826 $ this ->assertTrue ($ parser ->has ('email.to ' ));
1927 $ this ->assertTrue ($ parser ->has ('email.message.* ' ));
@@ -36,11 +44,12 @@ public function wildcards_with_simple_structure_json()
3644 /** @test */
3745 public function wildcards_with_array_structure_json ()
3846 {
39- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
47+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
48+ ->shouldDeferMissing ()
49+ ->shouldAllowMockingProtectedMethods ();
4050
41- $ parser ->expects ($ this ->any ())
42- ->method ('getPayload ' )
43- ->will ($ this ->returnValue ('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "world hello"}]} ' ));
51+ $ parser ->shouldReceive ('getPayload ' )
52+ ->andReturn ('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "world hello"}]} ' );
4453
4554 $ this ->assertTrue ($ parser ->has ('comments.*.title ' ));
4655 $ this ->assertTrue ($ parser ->has ('comments.%.title ' ));
@@ -64,35 +73,40 @@ public function wildcards_with_array_structure_json()
6473 /** @test */
6574 public function array_structured_getPayload_json ()
6675 {
67- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
76+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
77+ ->shouldDeferMissing ()
78+ ->shouldAllowMockingProtectedMethods ();
6879
69- $ parser ->expects ( $ this -> any () )
70- -> method ( ' getPayload ' )
71- -> will ( $ this -> returnValue ( '{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "hello world"}]} ' ) );
80+ $ parser ->shouldReceive ( ' getPayload ' )
81+ -> once ( )
82+ -> andReturn ( '{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "hello world"}]} ' );
7283
7384 $ this ->assertEquals (array ("comments " => array (array ("title " => "hello " , "message " => "hello world " ), array ("title " => "world " , "message " => "hello world " ))), $ parser ->payload ());
7485 }
7586
7687 /** @test */
7788 public function alias_all_check ()
7889 {
79- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
90+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
91+ ->shouldDeferMissing ()
92+ ->shouldAllowMockingProtectedMethods ();
8093
81- $ parser ->expects ( $ this -> any () )
82- ->method ( ' getPayload ' )
83- ->will ( $ this -> returnValue ( '{"status":123, "message":"hello world"} ' ) );
94+ $ parser ->shouldReceive ( ' getPayload ' )
95+ ->once ( )
96+ ->andReturn ( '{"status":123, "message":"hello world"} ' );
8497
8598 $ this ->assertEquals (array ('status ' => 123 , 'message ' => 'hello world ' ), $ parser ->all ());
8699 }
87100
88101 /** @test */
89102 public function return_value_for_multi_level_key ()
90103 {
91- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
104+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
105+ ->shouldDeferMissing ()
106+ ->shouldAllowMockingProtectedMethods ();
92107
93- $ parser ->expects ($ this ->any ())
94- ->method ('getPayload ' )
95- ->
will (
$ this ->
returnValue (
'{"id": 123, "note": {"headers": {"to": "[email protected] ", "from": "[email protected] "}, "body": "Hello World"}} ' ));
108+ $ parser ->shouldReceive ('getPayload ' )
109+ ->
andReturn (
'{"id": 123, "note": {"headers": {"to": "[email protected] ", "from": "[email protected] "}, "body": "Hello World"}} ' );
96110
97111 $ this ->assertEquals ('123 ' , $ parser ->get ('id ' ));
98112 $ this ->assertEquals ('Hello World ' , $ parser ->get ('note.body ' ));
@@ -109,11 +123,12 @@ public function return_value_for_multi_level_key()
109123 /** @test */
110124 public function return_value_for_selected_key_use_default_if_not_found ()
111125 {
112- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
126+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
127+ ->shouldDeferMissing ()
128+ ->shouldAllowMockingProtectedMethods ();
113129
114- $ parser ->expects ($ this ->any ())
115- ->method ('getPayload ' )
116- ->will ($ this ->returnValue ('{"status":false, "code":123, "note":"", "message":"hello world"} ' ));
130+ $ parser ->shouldReceive ('getPayload ' )
131+ ->andReturn ('{"status":false, "code":123, "note":"", "message":"hello world"} ' );
117132
118133 $ this ->assertEquals ('ape ' , $ parser ->get ('banana ' , 'ape ' ));
119134 $ this ->assertEquals ('123 ' , $ parser ->get ('code ' , '2345234 ' ));
@@ -124,11 +139,13 @@ public function return_value_for_selected_key_use_default_if_not_found()
124139 /** @test */
125140 public function return_boolean_value_if_getPayload_has_keys ()
126141 {
127- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
142+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
143+ ->shouldDeferMissing ()
144+ ->shouldAllowMockingProtectedMethods ();
128145
129- $ parser ->expects ( $ this -> any () )
130- ->method ( ' getPayload ' )
131- ->will ( $ this -> returnValue ( '{"status":false, "code":123, "note":"", "message":"hello world"} ' ) );
146+ $ parser ->shouldReceive ( ' getPayload ' )
147+ ->times ( 3 )
148+ ->andReturn ( '{"status":false, "code":123, "note":"", "message":"hello world"} ' );
132149
133150 $ this ->assertTrue ($ parser ->has ('status ' , 'code ' ));
134151 $ this ->assertFalse ($ parser ->has ('banana ' ));
@@ -138,23 +155,26 @@ public function return_boolean_value_if_getPayload_has_keys()
138155 /** @test */
139156 public function only_return_selected_fields ()
140157 {
141- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
158+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
159+ ->shouldDeferMissing ()
160+ ->shouldAllowMockingProtectedMethods ();
142161
143- $ parser ->expects ($ this ->any ())
144- ->method ('getPayload ' )
145- ->will ($ this ->returnValue ('{"status":123, "message":"hello world"} ' ));
162+ $ parser ->shouldReceive ('getPayload ' )
163+ ->andReturn ('{"status":123, "message":"hello world"} ' );
146164
147165 $ this ->assertEquals (array ('status ' => 123 ), $ parser ->only ('status ' ));
148166 }
149167
150168 /** @test */
151169 public function except_do_not_return_selected_fields ()
152170 {
153- $ parser = $ this ->getMock ('Nathanmac\Utilities\Parser\Parser ' , array ('getPayload ' ));
171+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
172+ ->shouldDeferMissing ()
173+ ->shouldAllowMockingProtectedMethods ();
154174
155- $ parser ->expects ( $ this -> any () )
156- ->method ( ' getPayload ' )
157- ->will ( $ this -> returnValue ( '{"status":123, "message":"hello world"} ' ) );
175+ $ parser ->shouldReceive ( ' getPayload ' )
176+ ->twice ( )
177+ ->andReturn ( '{"status":123, "message":"hello world"} ' );
158178
159179 $ this ->assertEquals (array ('status ' => 123 ), $ parser ->except ('message ' ));
160180 $ this ->assertEquals (array ('status ' => 123 , 'message ' => 'hello world ' ), $ parser ->except ('message.tags ' ));
@@ -171,4 +191,23 @@ public function format_detection_defaults_to_json()
171191 $ _SERVER ['CONTENT_TYPE ' ] = "somerandomstuff " ;
172192 $ this ->assertEquals ('json ' , $ parser ->getFormat ());
173193 }
194+
195+ /** @test */
196+ public function throw_an_exception_when_parsed_auto_detect_mismatch_content_type ()
197+ {
198+ $ parser = m::mock ('Nathanmac\Utilities\Parser\Parser ' )
199+ ->shouldDeferMissing ()
200+ ->shouldAllowMockingProtectedMethods ();
201+
202+ $ parser ->shouldReceive ('getFormat ' )
203+ ->once ()
204+ ->andReturn ('serialize ' );
205+
206+ $ parser ->shouldReceive ('getPayload ' )
207+ ->once ()
208+ ->andReturn ("<?xml version= \"1.0 \" encoding= \"UTF-8 \"?><xml><status>123</status><message>hello world</message></xml> " );
209+
210+ $ this ->setExpectedException ('Exception ' , 'Failed To Parse Serialized Data ' );
211+ $ this ->assertEquals (array ('status ' => 123 , 'message ' => 'hello world ' ), $ parser ->payload ());
212+ }
174213}
0 commit comments