Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a few tests #20

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ before_script:

script:
- composer run phpcs
- ./vendor/bin/phpunit
- composer run phpstan
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
use Link0\Bunq\Middleware\ResponseSignatureMiddleware;
use Psr\Http\Message\ResponseInterface;

final class Client
final class Client implements ClientInterface
{
/**
* @var GuzzleClient
Expand Down
35 changes: 35 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php declare(strict_types=1);

namespace Link0\Bunq;

interface ClientInterface
{
/**
* @param string $endpoint
* @return array
*/
public function get(string $endpoint, array $headers = []): array;

/**
* @param string $endpoint
* @param array $body
* @param array $headers
* @return array
*/
public function post(string $endpoint, array $body, array $headers = []): array;

/**
* @param string $endpoint
* @param array $body
* @param array $headers
* @return array
*/
public function put(string $endpoint, array $body, array $headers = []): array;

/**
* @param string $endpoint
* @param array $headers
* @return void
*/
public function delete(string $endpoint, array $headers = []);
}
7 changes: 6 additions & 1 deletion src/Domain/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ public static function fromInteger(int $id): Id
return new Id($id);
}

public function id()
{
return $this->id;
}

/**
* @return string
*/
public function __toString()
{
return (string) $this->id;
return (string) $this->id();
}
}
11 changes: 7 additions & 4 deletions src/Middleware/DebugMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Link0\Bunq\Middleware;

use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -47,9 +47,12 @@ public static function request()
*/
public static function response()
{
return function (RequestInterface $request, $options, FulfilledPromise $responsePromise) {
$responsePromise->then(function (ResponseInterface $response) {
echo chr(27) . '[33m' . "RESPONSE: HTTP/" . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . chr(27) . "[0m\n";
return function (RequestInterface $request, $options, PromiseInterface $responsePromise) {
return $responsePromise->then(function (ResponseInterface $response) {
echo chr(27) . '[33m' . "RESPONSE: HTTP/" .
$response->getProtocolVersion() . ' ' .
$response->getStatusCode() . ' ' .
$response->getReasonPhrase() . chr(27) . "[0m\n";

foreach ($response->getHeaders() as $key => $headers) {
foreach ($headers as $header) {
Expand Down
7 changes: 7 additions & 0 deletions src/Middleware/RequestIdMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@

final class RequestIdMiddleware
{
/**
* @var string
*/
private $sessionToken;

/**
* @param string $sessionToken
*/
public function __construct($sessionToken)
{
$this->sessionToken = $sessionToken;
Expand Down
1 change: 0 additions & 1 deletion src/Middleware/ResponseSignatureMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public function __invoke(ResponseInterface $response)

$signatureData .= "\n\n";
$signatureData .= (string) $response->getBody();

$rawSignature = base64_decode($serverSignature);
$verify = openssl_verify($signatureData, $rawSignature, $this->publicKey, self::SIGNATURE_ALGORITHM);
if ($verify !== 1) {
Expand Down
7 changes: 4 additions & 3 deletions src/Service/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@
namespace Link0\Bunq\Service;

use Link0\Bunq\Client;
use Link0\Bunq\ClientInterface;
use Link0\Bunq\Domain\Id;
use Link0\Bunq\Domain\User;

final class UserService
{
/**
* @var Client
* @var ClientInterface
*/
private $client;

/**
* @param Client $client
* @param ClientInterface $client
*/
public function __construct(Client $client)
public function __construct(ClientInterface $client)
{
$this->client = $client;
}
Expand Down
35 changes: 35 additions & 0 deletions tests/unit/Domain/IdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Link0\Bunq\Tests\Domain;

use Link0\Bunq\Domain\Id;
use PHPUnit\Framework\TestCase;

final class IdTest extends TestCase
{
/**
* @expectedException \Assert\InvalidArgumentException
* @expectedExceptionMessage Id must be greater or equal to 0
*/
public function test_that_id_must_not_be_negative()
{
Id::fromInteger(-42);
}

public function test_that_id_can_be_zero()
{
$id = Id::fromInteger(0);
$this->assertSame(0, $id->id());
}
public function test_that_id_can_greater_than_zero()
{
$id = Id::fromInteger(42);
$this->assertSame(42, $id->id());
}

public function test_that_id_can_be_casted_to_string()
{
$id = Id::fromInteger(42);
$this->assertEquals('42', (string) $id);
}
}
33 changes: 33 additions & 0 deletions tests/unit/Domain/Keypair/PrivateKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Link0\Bunq\Tests\Domain\Keypair;

use Link0\Bunq\Domain\Keypair\PrivateKey;
use PHPUnit\Framework\TestCase;

final class PrivateKeyTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid PrivateKey format. Please use a shielded format
*/
public function test_that_private_key_must_be_pem_formatted()
{
$key = explode("\n", TEST_PRIVATE_KEY);
array_shift($key); // -----BEGIN PRIVATE KEY -----
array_pop($key); // -----END PRIVATE KEY -----
new PrivateKey(implode("\n", $key));
}

public function test_that_private_key_is_castable_to_string()
{
$key = new PrivateKey(TEST_PRIVATE_KEY);
$this->assertEquals(TEST_PRIVATE_KEY, (string)$key);
}

public function test_that_actual_key_is_stripped_from_debug_information()
{
$key = new PrivateKey(TEST_PRIVATE_KEY);
$this->assertContains('***PRIVATE KEY***', print_r($key, true));
}
}
35 changes: 35 additions & 0 deletions tests/unit/Domain/Keypair/PublicKeyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Link0\Bunq\Tests\Domain\Keypair;

use Link0\Bunq\Domain\Keypair\PublicKey;
use PHPUnit\Framework\TestCase;

final class PublicKeyTest extends TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid PublicKey format. Please use a shielded format
*/
public function test_that_private_key_must_be_pem_formatted()
{
$key = explode("\n", TEST_PUBLIC_KEY);
array_shift($key); // -----BEGIN PUBLIC KEY -----
array_pop($key); // -----END PUBLIC KEY -----
new PublicKey(implode("\n", $key));
}

public function test_that_private_key_is_castable_to_string()
{
$key = new PublicKey(TEST_PUBLIC_KEY);
$this->assertSame(TEST_PUBLIC_KEY, (string)$key);
}

public function test_that_public_key_can_be_made_from_server_response()
{
$key = PublicKey::fromServerPublicKey([
'server_public_key' => TEST_PUBLIC_KEY
]);
$this->assertEquals(TEST_PUBLIC_KEY, $key);
}
}
36 changes: 36 additions & 0 deletions tests/unit/Environment/ProductionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace Link0\Bunq\Tests\Environment;

use Link0\Bunq\Environment\Production;
use PHPUnit\Framework\TestCase;

final class ProductionTest extends TestCase
{
public function test_that_production_is_not_in_debug_mode_by_default()
{
$this->assertFalse((new Production())->inDebugMode());
}

public function test_that_production_can_be_switched_to_debug_mode()
{
$debug = true;
$this->assertTrue((new Production($debug))->inDebugMode());
}

public function test_that_version_is_one()
{
$this->assertEquals(Production::VERSION, (new Production())->version());
}

public function test_that_service_url_equals_constant()
{
$this->assertEquals(Production::SERVICE_URL, (new Production())->serviceUrl());
}

public function test_that_endpoint_aggregates_other_variables()
{
$env = new Production();
$this->assertEquals($env->serviceUrl() . '/' . $env->version() . '/', $env->endpoint());
}
}
36 changes: 36 additions & 0 deletions tests/unit/Environment/SandboxTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace Link0\Bunq\Tests\Environment;

use Link0\Bunq\Environment\Sandbox;
use PHPUnit\Framework\TestCase;

final class SandboxTest extends TestCase
{
public function test_that_sandbox_is_not_in_debug_mode_by_default()
{
$this->assertFalse((new Sandbox())->inDebugMode());
}

public function test_that_sandbox_can_be_switched_to_debug_mode()
{
$debug = true;
$this->assertTrue((new Sandbox($debug))->inDebugMode());
}

public function test_that_version_is_one()
{
$this->assertEquals(Sandbox::VERSION, (new Sandbox())->version());
}

public function test_that_service_url_equals_constant()
{
$this->assertEquals(Sandbox::SERVICE_URL, (new Sandbox())->serviceUrl());
}

public function test_that_endpoint_aggregates_other_variables()
{
$env = new Sandbox();
$this->assertEquals($env->serviceUrl() . '/' . $env->version() . '/', $env->endpoint());
}
}
65 changes: 65 additions & 0 deletions tests/unit/Middleware/DebugMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php declare(strict_types=1);

namespace Link0\Bunq\Tests\Middleware;

use GuzzleHttp\Promise\FulfilledPromise;
use Link0\Bunq\Middleware\DebugMiddleware;
use Mockery;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

final class DebugMiddlewareTest extends TestCase
{
public function test_that_requests_can_be_represented_as_debug_output()
{
$request = Mockery::mock(RequestInterface::class);
$request->shouldReceive('getMethod')->andReturn('GET');
$request->shouldReceive('getRequestTarget')->andReturn('/request/target');
$request->shouldReceive('getHeaders')->andReturn([
'SomeHeader' => ['SomeValue', 'SomeOtherValue'],
]);
$request->shouldReceive('getBody')->andReturn('{"foo": "bar"}');

$requestMiddleware = DebugMiddleware::request();

ob_start();
$requestMiddleware($request);
$actual = ob_get_clean();

// Assert it contains all the desired information
$this->assertContains('REQUEST', $actual);
$this->assertContains('GET', $actual);
$this->assertContains('/request/target', $actual);
$this->assertContains('SomeHeader: SomeValue', $actual);
$this->assertContains('SomeHeader: SomeOtherValue', $actual);
$this->assertContains('{"foo": "bar"}', $actual);
$this->assertContains('[foo] => bar', $actual);
}

public function test_that_responses_can_be_represented_as_debug_output()
{
$request = Mockery::mock(RequestInterface::class);
$response = Mockery::mock(ResponseInterface::class);

$response->shouldReceive('getProtocolVersion')->andReturn('1.1');
$response->shouldReceive('getStatusCode')->andReturn('200');
$response->shouldReceive('getReasonPhrase')->andReturn('OK');
$response->shouldReceive('getBody')->andReturn('{"foo": "bar"}');
$response->shouldReceive('getHeaders')->andReturn([
'Foo' => ['Bar', 'Baz']
]);

$promise = new FulfilledPromise($response);
$responseMiddleware = DebugMiddleware::response();

ob_start();
$responseMiddleware($request, [], $promise)->wait();
$actual = ob_get_clean();

$this->assertContains('RESPONSE: HTTP/1.1 200 OK', $actual);
$this->assertContains('Foo: Bar', $actual);
$this->assertContains('Foo: Baz', $actual);
$this->assertContains('[foo] => bar', $actual);
}
}
Loading