Skip to content

Commit

Permalink
Switch out phpunit mocking for mockery and fix issue with BSON tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanmac committed Dec 28, 2014
1 parent a71e0c2 commit 6e44ca7
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 95 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,10 @@ text/x-yaml > YAML
application/yaml > YAML
application/x-yaml > YAML
BSON
----
application/bson > BSON
MISC
----
application/vnd.php.serialized > Serialized Object
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
},
"require-dev": {
"phpunit/phpunit": "4.*",
"mockery/mockery": "0.9.3",
"satooshi/php-coveralls": "0.6.*"
},
"autoload": {
Expand Down
64 changes: 64 additions & 0 deletions tests/BSONTest.php
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());
}
}
20 changes: 16 additions & 4 deletions tests/JSONTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@
require dirname(__FILE__)."/../vendor/autoload.php";

use Nathanmac\Utilities\Parser\Parser;
use \Mockery as m;

class JSONTest extends PHPUnit_Framework_TestCase {

protected function tearDown()
{
m::close();
}

/** @test */
public function parse_auto_detect_json_data()
{
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload'));
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
->shouldDeferMissing()
->shouldAllowMockingProtectedMethods();

$parser->shouldReceive('getFormat')
->twice()
->andReturn('json');

$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('json', $parser->getFormat());
$this->assertEquals(array('status' => 123, 'message' => 'hello world'), $parser->payload());
Expand Down
111 changes: 75 additions & 36 deletions tests/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.*'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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'));
Expand All @@ -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());
}
}
22 changes: 15 additions & 7 deletions tests/QueryStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
require dirname(__FILE__)."/../vendor/autoload.php";

use Nathanmac\Utilities\Parser\Parser;
use \Mockery as m;

class QueryStrTest extends PHPUnit_Framework_TestCase {

protected function tearDown()
{
m::close();
}

/** @test */
public function parse_auto_detect_query_string_data()
{
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload', 'getFormat'));
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
->shouldDeferMissing()
->shouldAllowMockingProtectedMethods();

$parser->expects($this->any())
->method('getFormat')
->will($this->returnValue('querystr'));
$parser->shouldReceive('getFormat')
->once()
->andReturn('querystr');

$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->payload());
}
Expand Down
22 changes: 15 additions & 7 deletions tests/SerializeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,29 @@
require dirname(__FILE__)."/../vendor/autoload.php";

use Nathanmac\Utilities\Parser\Parser;
use \Mockery as m;

class SerializeTest extends PHPUnit_Framework_TestCase {

protected function tearDown()
{
m::close();
}

/** @test */
public function parse_auto_detect_serialized_data()
{
$parser = $this->getMock('Nathanmac\Utilities\Parser\Parser', array('getPayload', 'getFormat'));
$parser = m::mock('Nathanmac\Utilities\Parser\Parser')
->shouldDeferMissing()
->shouldAllowMockingProtectedMethods();

$parser->expects($this->any())
->method('getFormat')
->will($this->returnValue('serialize'));
$parser->shouldReceive('getFormat')
->once()
->andReturn('serialize');

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

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

0 comments on commit 6e44ca7

Please sign in to comment.