-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch out phpunit mocking for mockery and fix issue with BSON tests
- Loading branch information
Showing
9 changed files
with
234 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
require dirname(__FILE__) . "/../vendor/autoload.php"; | ||
|
||
use Nathanmac\Utilities\Parser\Parser; | ||
use \Mockery as m; | ||
|
||
class BSONTest extends PHPUnit_Framework_TestCase { | ||
|
||
protected function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
/** @test */ | ||
public function parser_validates_bson_data() | ||
{ | ||
if (function_exists('bson_decode')) { | ||
$expected = array('status' => 123, 'message' => 'hello world'); | ||
$payload = bson_encode($expected); | ||
|
||
$parser = new Parser(); | ||
$this->assertEquals($expected, $parser->bson($payload)); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function parser_empty_bson_data() | ||
{ | ||
if (function_exists('bson_decode')) { | ||
$parser = new Parser(); | ||
$this->assertEquals(array(), $parser->bson("")); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function throw_an_exception_when_bson_library_not_loaded() | ||
{ | ||
if (! function_exists('bson_decode')) { | ||
$this->setExpectedException('Exception', 'Failed To Parse BSON - Supporting Library Not Available'); | ||
|
||
$parser = new Parser(); | ||
$this->assertEquals(array(), $parser->bson("")); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function throws_an_exception_when_parsed_bson_bad_data() | ||
{ | ||
if (! function_exists('bson_decode')) { | ||
$parser = new Parser(); | ||
$this->setExpectedException('Exception', 'Failed To Parse BSON'); | ||
$parser->bson('as|df>ASFBw924hg2='); | ||
} | ||
} | ||
|
||
/** @test */ | ||
public function format_detection_bson() | ||
{ | ||
$parser = new Parser(); | ||
$_SERVER['HTTP_CONTENT_TYPE'] = "application/bson"; | ||
$this->assertEquals('bson', $parser->getFormat()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,17 +3,25 @@ | |
require dirname(__FILE__)."/../vendor/autoload.php"; | ||
|
||
use Nathanmac\Utilities\Parser\Parser; | ||
use \Mockery as m; | ||
|
||
class ParserTest extends PHPUnit_Framework_TestCase | ||
{ | ||
|
||
protected function tearDown() | ||
{ | ||
m::close(); | ||
} | ||
|
||
/** @test */ | ||
public function wildcards_with_simple_structure_json() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"email": {"to": "[email protected]", "from": "[email protected]", "subject": "Hello World", "message": { "body": "Hello this is a sample message" }}}')); | ||
$parser->shouldReceive('getPayload') | ||
->andReturn('{"email": {"to": "[email protected]", "from": "[email protected]", "subject": "Hello World", "message": { "body": "Hello this is a sample message" }}}'); | ||
|
||
$this->assertTrue($parser->has('email.to')); | ||
$this->assertTrue($parser->has('email.message.*')); | ||
|
@@ -36,11 +44,12 @@ public function wildcards_with_simple_structure_json() | |
/** @test */ | ||
public function wildcards_with_array_structure_json() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "world hello"}]}')); | ||
$parser->shouldReceive('getPayload') | ||
->andReturn('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "world hello"}]}'); | ||
|
||
$this->assertTrue($parser->has('comments.*.title')); | ||
$this->assertTrue($parser->has('comments.%.title')); | ||
|
@@ -64,35 +73,40 @@ public function wildcards_with_array_structure_json() | |
/** @test */ | ||
public function array_structured_getPayload_json() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "hello world"}]}')); | ||
$parser->shouldReceive('getPayload') | ||
->once() | ||
->andReturn('{"comments": [{ "title": "hello", "message": "hello world"}, {"title": "world", "message": "hello world"}]}'); | ||
|
||
$this->assertEquals(array("comments" => array(array("title" => "hello", "message" => "hello world"), array("title" => "world", "message" => "hello world"))), $parser->payload()); | ||
} | ||
|
||
/** @test */ | ||
public function alias_all_check() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"status":123, "message":"hello world"}')); | ||
$parser->shouldReceive('getPayload') | ||
->once() | ||
->andReturn('{"status":123, "message":"hello world"}'); | ||
|
||
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->all()); | ||
} | ||
|
||
/** @test */ | ||
public function return_value_for_multi_level_key() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"id": 123, "note": {"headers": {"to": "[email protected]", "from": "[email protected]"}, "body": "Hello World"}}')); | ||
$parser->shouldReceive('getPayload') | ||
->andReturn('{"id": 123, "note": {"headers": {"to": "[email protected]", "from": "[email protected]"}, "body": "Hello World"}}'); | ||
|
||
$this->assertEquals('123', $parser->get('id')); | ||
$this->assertEquals('Hello World', $parser->get('note.body')); | ||
|
@@ -109,11 +123,12 @@ public function return_value_for_multi_level_key() | |
/** @test */ | ||
public function return_value_for_selected_key_use_default_if_not_found() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"status":false, "code":123, "note":"", "message":"hello world"}')); | ||
$parser->shouldReceive('getPayload') | ||
->andReturn('{"status":false, "code":123, "note":"", "message":"hello world"}'); | ||
|
||
$this->assertEquals('ape', $parser->get('banana', 'ape')); | ||
$this->assertEquals('123', $parser->get('code', '2345234')); | ||
|
@@ -124,11 +139,13 @@ public function return_value_for_selected_key_use_default_if_not_found() | |
/** @test */ | ||
public function return_boolean_value_if_getPayload_has_keys() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"status":false, "code":123, "note":"", "message":"hello world"}')); | ||
$parser->shouldReceive('getPayload') | ||
->times(3) | ||
->andReturn('{"status":false, "code":123, "note":"", "message":"hello world"}'); | ||
|
||
$this->assertTrue($parser->has('status', 'code')); | ||
$this->assertFalse($parser->has('banana')); | ||
|
@@ -138,23 +155,26 @@ public function return_boolean_value_if_getPayload_has_keys() | |
/** @test */ | ||
public function only_return_selected_fields() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"status":123, "message":"hello world"}')); | ||
$parser->shouldReceive('getPayload') | ||
->andReturn('{"status":123, "message":"hello world"}'); | ||
|
||
$this->assertEquals(array('status' => 123), $parser->only('status')); | ||
} | ||
|
||
/** @test */ | ||
public function except_do_not_return_selected_fields() | ||
{ | ||
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload')); | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->expects($this->any()) | ||
->method('getPayload') | ||
->will($this->returnValue('{"status":123, "message":"hello world"}')); | ||
$parser->shouldReceive('getPayload') | ||
->twice() | ||
->andReturn('{"status":123, "message":"hello world"}'); | ||
|
||
$this->assertEquals(array('status' => 123), $parser->except('message')); | ||
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->except('message.tags')); | ||
|
@@ -171,4 +191,23 @@ public function format_detection_defaults_to_json() | |
$_SERVER['CONTENT_TYPE'] = "somerandomstuff"; | ||
$this->assertEquals('json', $parser->getFormat()); | ||
} | ||
|
||
/** @test */ | ||
public function throw_an_exception_when_parsed_auto_detect_mismatch_content_type() | ||
{ | ||
$parser = m::mock('Nathanmac\Utilities\Parser\Parser') | ||
->shouldDeferMissing() | ||
->shouldAllowMockingProtectedMethods(); | ||
|
||
$parser->shouldReceive('getFormat') | ||
->once() | ||
->andReturn('serialize'); | ||
|
||
$parser->shouldReceive('getPayload') | ||
->once() | ||
->andReturn("<?xml version=\"1.0\" encoding=\"UTF-8\"?><xml><status>123</status><message>hello world</message></xml>"); | ||
|
||
$this->setExpectedException('Exception', 'Failed To Parse Serialized Data'); | ||
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->payload()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.