Skip to content

Commit

Permalink
added more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Keil committed Apr 29, 2014
1 parent e8e32b6 commit 777d1d7
Show file tree
Hide file tree
Showing 13 changed files with 277 additions and 23 deletions.
4 changes: 2 additions & 2 deletions src/Response/AutoConsolidateAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class AutoConsolidateAddresses implements ResponseInterface
/**
* Sets archived address
*
* @param array $addresses
* @param array $addresses List of bitcoin addresses
*/
public function setConsolidated(array $addresses)
{
Expand All @@ -36,7 +36,7 @@ public function setConsolidated(array $addresses)
/**
* Returns archived address
*
* @return array
* @return array List of bitcoin addresses
*/
public function getConsolidated()
{
Expand Down
3 changes: 1 addition & 2 deletions src/Service/ResponsePluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class ResponsePluginManager extends AbstractPluginManager
/**
* Validate the plugin
*
* Checks that the filter loaded is either a valid callback or an instance
* of FilterInterface.
* Checks that the response loaded is either a valid callback or an instance of ResponseInterface.
*
* @param mixed $plugin
* @return void
Expand Down
56 changes: 56 additions & 0 deletions test/Response/AutoConsolidateAddressesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
/**
* Sake
*
* @link http://github.com/sandrokeil/BlockchainWalletApi for the canonical source repository
* @copyright Copyright (c) 2014 Sandro Keil
* @license http://github.com/sandrokeil/BlockchainWalletApi/blob/master/LICENSE.txt New BSD License
*/

namespace SakeTest\BlockchainWalletApi\Response;

use PHPUnit_Framework_TestCase as TestCase;
use Sake\BlockchainWalletApi\Response\AutoConsolidateAddresses;

/**
* Class AutoConsolidateAddressesTest
*
* Tests integrity of \Sake\BlockchainWalletApi\Response\AutoConsolidateAddresses
*/
class AutoConsolidateAddressesTest extends TestCase
{
/**
* Tests if class implements response interface
*
* @group response
*
* @codeCoverageIgnore
*/
public function testIsResponseClass()
{
$cut = new AutoConsolidateAddresses();
$this->assertInstanceOf('\Sake\BlockchainWalletApi\Response\ResponseInterface', $cut);
}

/**
* Tests if response can be configured via setter
*
* @group response
*
* @covers \Sake\BlockchainWalletApi\Response\AutoConsolidateAddresses::setConsolidated
* @covers \Sake\BlockchainWalletApi\Response\AutoConsolidateAddresses::getConsolidated
*/
public function testIfResponseDataCanBeSet()
{
$cut = new AutoConsolidateAddresses();

$addresses = array(
'1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq',
'34tfskdlfcvkdjhvkjwehf',
);

$cut->setConsolidated($addresses);

$this->assertEquals($addresses, $cut->getConsolidated());
}
}
4 changes: 2 additions & 2 deletions test/Service/BlockchainWalletFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function setUp()
}

/**
* Tests getServiceConfig() should should return service manager configuration
* Tests createService() returns a valid and configured service instance.
*
* @covers \Sake\BlockchainWalletApi\Service\BlockchainWalletFactory::createService
* @covers \Sake\BlockchainWalletApi\Service\BlockchainWalletFactory::__construct
Expand Down Expand Up @@ -87,7 +87,7 @@ public function testCreateService()
}

/**
* Tests getServiceConfig() should should return service manager configuration
* Tests createService() throws runtime exception if no options are provided
*
* @covers \Sake\BlockchainWalletApi\Service\BlockchainWalletFactory::createService
* @covers \Sake\BlockchainWalletApi\Service\BlockchainWalletFactory::getModule
Expand Down
100 changes: 83 additions & 17 deletions test/Service/BlockchainWalletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Sake\BlockchainWalletApi\Request;
use Sake\BlockchainWalletApi\Response;
use Sake\BlockchainWalletApi\Service\BlockchainWallet;
use Sake\BlockchainWalletApi\Service\BlockchainWalletOptions;
use Zend\Http;
use PHPUnit_Framework_TestCase as TestCase;
Expand Down Expand Up @@ -40,16 +41,13 @@ public function testSendWithRequestAddressBalance()
/* @var $response Response\AddressBalance */
$response = $service->send($request);

$expected = trim(file_get_contents(__DIR__ . '/TestAsset/Request/address_balance.txt'));
// workaround for crlf line endings
$actual = preg_replace('~\R~u', "\n", trim($service->getClient()->getLastRawRequest()));
$this->assertEquals(0, $response->getBalance());

$this->assertEquals(
$expected,
$actual,
$expected . PHP_EOL . ' does not match ' . PHP_EOL . $actual
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/address_balance.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
$this->assertEquals(0, $response->getBalance());
}

/**
Expand All @@ -69,17 +67,13 @@ public function testSendWithRequestWalletBalance()
/* @var $response Response\WalletBalance */
$response = $service->send($request);

$expected = trim(file_get_contents(__DIR__ . '/TestAsset/Request/wallet_balance.txt'));
// workaround for crlf line endings
$actual = preg_replace('~\R~u', "\n", trim($service->getClient()->getLastRawRequest()));
$this->assertEquals(0, $response->getBalance());

$this->assertEquals(
$expected,
$actual,
$expected . PHP_EOL . ' does not match ' . PHP_EOL . $actual
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/wallet_balance.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);

$this->assertEquals(0, $response->getBalance());
}

/**
Expand All @@ -93,14 +87,23 @@ public function testSendWithRequestWalletBalance()
public function testSendWithRequestNewAddress()
{
$service = $this->getStubForTest(file_get_contents(__DIR__ . '/TestAsset/Response/new_address.txt'));
$label = 'Order No : 1234';

$request = new Request\NewAddress();

$request->setLabel($label);

/* @var $response Response\NewAddress */
$response = $service->send($request);

$this->assertEquals('Order No : 1234', $response->getLabel());
$this->assertEquals($label, $response->getLabel());
$this->assertEquals('18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy', $response->getAddress());

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/new_address.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand All @@ -127,6 +130,12 @@ public function testSendWithRequestListAddresses()
$this->assertArrayHasKey('1A8JiWcwvpY7tAopUkSnGuEYHmzGYfZPiq', $addresses);
$this->assertArrayHasKey('17p49XUC2fw4Fn53WjZqYAm4APKqhNPEkY', $addresses);
$this->assertInstanceOf('\Sake\BlockchainWalletApi\Response\Address', current($addresses));

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/list_addresses.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand Down Expand Up @@ -158,6 +167,12 @@ public function testSendWithRequestSend()
'Some funds are pending confirmation and cannot be spent yet (Value 0.001 BTC)',
$response->getNotice()
);

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/send.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand Down Expand Up @@ -190,6 +205,12 @@ public function testSendWithRequestSendMany()
'f322d01ad784e5deeb25464a5781c3b20971c1863679ca506e702e3e33c18e9c',
$response->getTxHash()
);

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/send_many.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand All @@ -203,13 +224,21 @@ public function testSendWithRequestSendMany()
public function testSendWithRequestAddressArchive()
{
$service = $this->getStubForTest(file_get_contents(__DIR__ . '/TestAsset/Response/address_archive.txt'));
$address = '18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy';

$request = new Request\AddressArchive();
$request->setAddress($address);

/* @var $response Response\AddressArchive */
$response = $service->send($request);

$this->assertEquals('18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy', $response->getArchived());
$this->assertEquals($address, $response->getArchived());

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/address_archive.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand All @@ -223,13 +252,21 @@ public function testSendWithRequestAddressArchive()
public function testSendWithRequestAddressUnarchive()
{
$service = $this->getStubForTest(file_get_contents(__DIR__ . '/TestAsset/Response/address_unarchive.txt'));
$address = '18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy';

$request = new Request\AddressUnarchive();
$request->setAddress($address);

/* @var $response Response\AddressUnarchive */
$response = $service->send($request);

$this->assertEquals('18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy', $response->getActive());

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/address_unarchive.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand All @@ -255,6 +292,12 @@ public function testSendWithRequestAutoConsolidateAddresses()
$consolidated = array('18fyqiZzndTxdVo7g9ouRogB4uFj86JJiy', '1Q1AtvCyKhtveGm3187mgNRh5YcukUWjQC');

$this->assertEquals($consolidated, $response->getConsolidated());

$this->assertEquals(
$this->getLastRawRequestExpected(__DIR__ . '/TestAsset/Request/auto_consolidate_addresses.txt'),
$this->getLastRawRequest($service),
'Requests does not match'
);
}

/**
Expand Down Expand Up @@ -284,4 +327,27 @@ protected function getStubForTest($response, array $methods = null)
$stub->getClient()->getAdapter()->setResponse($response);
return $stub;
}

/**
* Returns last raw request from client
*
* @param BlockchainWallet $service
* @return string Raw request
*/
protected function getLastRawRequest(BlockchainWallet $service)
{
// workaround for crlf line endings
return preg_replace('~\R~u', "\n", trim($service->getClient()->getLastRawRequest()));
}

/**
* Returns last raw request from client
*
* @param string $file File for content
* @return string Expected request
*/
protected function getLastRawRequestExpected($file)
{
return trim(file_get_contents($file));
}
}

0 comments on commit 777d1d7

Please sign in to comment.