Skip to content

Commit

Permalink
AliasInsertRequestTest, AliasInsertAssertRequestTest added
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasmeike committed Jul 25, 2016
1 parent 5095573 commit 07c3517
Show file tree
Hide file tree
Showing 2 changed files with 170 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Ticketpark\SaferpayJson\Tests\SecureAliasStore;

use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertAssertRequest;

/**
* Created by PhpStorm.
* User: thomasm
* Date: 25.07.2016
* Time: 11:13
*/
class AliasInsertAssertRequestTest extends \PHPUnit_Framework_TestCase
{
public function testErrorResponse()
{
$initializer = new AliasInsertAssertRequest();
$initializer->setBrowser($this->getBrowserMock(false));
$response = $initializer->execute();

$this->assertInstanceOf('Ticketpark\SaferpayJson\Message\ErrorResponse', $response);
}

public function testSuccessfulResponse()
{
$initializer = new AliasInsertAssertRequest();
$initializer->setBrowser($this->getBrowserMock(true));
$response = $initializer->execute();

$this->assertInstanceOf('Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertAssertResponse', $response);
}

public function getBrowserMock($successful)
{
$browser = $this->getMockBuilder('Buzz\Browser')
->disableOriginalConstructor()
->setMethods(array('post'))
->getMock();

$browser->expects($this->once())
->method('post')
->will($this->returnValue($this->getResponseMock($successful)));

return $browser;
}

public function getResponseMock($successful)
{
$response = $this->getMockBuilder('Buzz\Message\Response')
->disableOriginalConstructor()
->setMethods(array('getStatusCode', 'isClientError', 'getContent'))
->getMock();

$response->expects($this->any())
->method('isClientError')
->will($this->returnValue(!$successful));

$response->expects($this->any())
->method('getStatusCode')
->will($this->returnValue(200));

if ($successful) {
$content = $this->getFakedApiResponse('Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertAssertResponse');
} else {
$content = $this->getFakedApiResponse('Ticketpark\SaferpayJson\Message\ErrorResponse');
}

$response->expects($this->any())
->method('getContent')
->will($this->returnValue($content));

return $response;
}

public function getFakedApiResponse($class)
{
AnnotationRegistry::registerLoader('class_exists');
$serializer = SerializerBuilder::create()->build();

$response = new $class();

return $serializer->serialize($response, 'json');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php
namespace Ticketpark\SaferpayJson\Tests\SecureAliasStore;

use Doctrine\Common\Annotations\AnnotationRegistry;
use JMS\Serializer\SerializerBuilder;
use Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertRequest;

/**
* Created by PhpStorm.
* User: thomasm
* Date: 25.07.2016
* Time: 11:14
*/
class AliasInsertRequestTest extends \PHPUnit_Framework_TestCase
{
public function testErrorResponse()
{
$initializer = new AliasInsertRequest();
$initializer->setBrowser($this->getBrowserMock(false));
$response = $initializer->execute();

$this->assertInstanceOf('Ticketpark\SaferpayJson\Message\ErrorResponse', $response);
}

public function testSuccessfulResponse()
{
$initializer = new AliasInsertRequest();
$initializer->setBrowser($this->getBrowserMock(true));
$response = $initializer->execute();

$this->assertInstanceOf('Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertResponse', $response);
}

public function getBrowserMock($successful)
{
$browser = $this->getMockBuilder('Buzz\Browser')
->disableOriginalConstructor()
->setMethods(array('post'))
->getMock();

$browser->expects($this->once())
->method('post')
->will($this->returnValue($this->getResponseMock($successful)));

return $browser;
}

public function getResponseMock($successful)
{
$response = $this->getMockBuilder('Buzz\Message\Response')
->disableOriginalConstructor()
->setMethods(array('getStatusCode', 'isClientError', 'getContent'))
->getMock();

$response->expects($this->any())
->method('isClientError')
->will($this->returnValue(!$successful));

$response->expects($this->any())
->method('getStatusCode')
->will($this->returnValue(200));

if ($successful) {
$content = $this->getFakedApiResponse('Ticketpark\SaferpayJson\SecureAliasStore\AliasInsertResponse');
} else {
$content = $this->getFakedApiResponse('Ticketpark\SaferpayJson\Message\ErrorResponse');
}

$response->expects($this->any())
->method('getContent')
->will($this->returnValue($content));

return $response;
}

public function getFakedApiResponse($class)
{
AnnotationRegistry::registerLoader('class_exists');
$serializer = SerializerBuilder::create()->build();

$response = new $class();

return $serializer->serialize($response, 'json');
}
}

0 comments on commit 07c3517

Please sign in to comment.