Skip to content

Commit 6e44ca7

Browse files
committed
Switch out phpunit mocking for mockery and fix issue with BSON tests
1 parent a71e0c2 commit 6e44ca7

File tree

9 files changed

+234
-95
lines changed

9 files changed

+234
-95
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ text/x-yaml > YAML
190190
application/yaml > YAML
191191
application/x-yaml > YAML
192192
193+
BSON
194+
----
195+
application/bson > BSON
196+
193197
MISC
194198
----
195199
application/vnd.php.serialized > Serialized Object

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"require-dev": {
1919
"phpunit/phpunit": "4.*",
20+
"mockery/mockery": "0.9.3",
2021
"satooshi/php-coveralls": "0.6.*"
2122
},
2223
"autoload": {

tests/BSONTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
require dirname(__FILE__) . "/../vendor/autoload.php";
4+
5+
use Nathanmac\Utilities\Parser\Parser;
6+
use \Mockery as m;
7+
8+
class BSONTest extends PHPUnit_Framework_TestCase {
9+
10+
protected function tearDown()
11+
{
12+
m::close();
13+
}
14+
15+
/** @test */
16+
public function parser_validates_bson_data()
17+
{
18+
if (function_exists('bson_decode')) {
19+
$expected = array('status' => 123, 'message' => 'hello world');
20+
$payload = bson_encode($expected);
21+
22+
$parser = new Parser();
23+
$this->assertEquals($expected, $parser->bson($payload));
24+
}
25+
}
26+
27+
/** @test */
28+
public function parser_empty_bson_data()
29+
{
30+
if (function_exists('bson_decode')) {
31+
$parser = new Parser();
32+
$this->assertEquals(array(), $parser->bson(""));
33+
}
34+
}
35+
36+
/** @test */
37+
public function throw_an_exception_when_bson_library_not_loaded()
38+
{
39+
if (! function_exists('bson_decode')) {
40+
$this->setExpectedException('Exception', 'Failed To Parse BSON - Supporting Library Not Available');
41+
42+
$parser = new Parser();
43+
$this->assertEquals(array(), $parser->bson(""));
44+
}
45+
}
46+
47+
/** @test */
48+
public function throws_an_exception_when_parsed_bson_bad_data()
49+
{
50+
if (! function_exists('bson_decode')) {
51+
$parser = new Parser();
52+
$this->setExpectedException('Exception', 'Failed To Parse BSON');
53+
$parser->bson('as|df>ASFBw924hg2=');
54+
}
55+
}
56+
57+
/** @test */
58+
public function format_detection_bson()
59+
{
60+
$parser = new Parser();
61+
$_SERVER['HTTP_CONTENT_TYPE'] = "application/bson";
62+
$this->assertEquals('bson', $parser->getFormat());
63+
}
64+
}

tests/JSONTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,29 @@
33
require dirname(__FILE__)."/../vendor/autoload.php";
44

55
use Nathanmac\Utilities\Parser\Parser;
6+
use \Mockery as m;
67

78
class JSONTest extends PHPUnit_Framework_TestCase {
89

10+
protected function tearDown()
11+
{
12+
m::close();
13+
}
14+
915
/** @test */
1016
public function parse_auto_detect_json_data()
1117
{
12-
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload'));
18+
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
19+
->shouldDeferMissing()
20+
->shouldAllowMockingProtectedMethods();
21+
22+
$parser->shouldReceive('getFormat')
23+
->twice()
24+
->andReturn('json');
1325

14-
$parser->expects($this->any())
15-
->method('getPayload')
16-
->will($this->returnValue('{"status":123, "message":"hello world"}'));
26+
$parser->shouldReceive('getPayload')
27+
->once()
28+
->andReturn('{"status":123, "message":"hello world"}');
1729

1830
$this->assertEquals('json', $parser->getFormat());
1931
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->payload());

tests/ParserTest.php

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,25 @@
33
require dirname(__FILE__)."/../vendor/autoload.php";
44

55
use Nathanmac\Utilities\Parser\Parser;
6+
use \Mockery as m;
67

78
class 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
}

tests/QueryStrTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33
require dirname(__FILE__)."/../vendor/autoload.php";
44

55
use Nathanmac\Utilities\Parser\Parser;
6+
use \Mockery as m;
67

78
class QueryStrTest extends PHPUnit_Framework_TestCase {
89

10+
protected function tearDown()
11+
{
12+
m::close();
13+
}
14+
915
/** @test */
1016
public function parse_auto_detect_query_string_data()
1117
{
12-
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload', 'getFormat'));
18+
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
19+
->shouldDeferMissing()
20+
->shouldAllowMockingProtectedMethods();
1321

14-
$parser->expects($this->any())
15-
->method('getFormat')
16-
->will($this->returnValue('querystr'));
22+
$parser->shouldReceive('getFormat')
23+
->once()
24+
->andReturn('querystr');
1725

18-
$parser->expects($this->any())
19-
->method('getPayload')
20-
->will($this->returnValue('status=123&message=hello world'));
26+
$parser->shouldReceive('getPayload')
27+
->once()
28+
->andReturn('status=123&message=hello world');
2129

2230
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->payload());
2331
}

tests/SerializeTest.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@
33
require dirname(__FILE__)."/../vendor/autoload.php";
44

55
use Nathanmac\Utilities\Parser\Parser;
6+
use \Mockery as m;
67

78
class SerializeTest extends PHPUnit_Framework_TestCase {
89

10+
protected function tearDown()
11+
{
12+
m::close();
13+
}
14+
915
/** @test */
1016
public function parse_auto_detect_serialized_data()
1117
{
12-
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload', 'getFormat'));
18+
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
19+
->shouldDeferMissing()
20+
->shouldAllowMockingProtectedMethods();
1321

14-
$parser->expects($this->any())
15-
->method('getFormat')
16-
->will($this->returnValue('serialize'));
22+
$parser->shouldReceive('getFormat')
23+
->once()
24+
->andReturn('serialize');
1725

18-
$parser->expects($this->any())
19-
->method('getPayload')
20-
->will($this->returnValue('a:2:{s:6:"status";i:123;s:7:"message";s:11:"hello world";}'));
26+
$parser->shouldReceive('getPayload')
27+
->once()
28+
->andReturn('a:2:{s:6:"status";i:123;s:7:"message";s:11:"hello world";}');
2129

2230
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->payload());
2331
}

0 commit comments

Comments
 (0)