Skip to content

Commit

Permalink
DRY up test classes (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredtking committed Jan 29, 2022
1 parent 385e4b2 commit d5492bb
Show file tree
Hide file tree
Showing 31 changed files with 573 additions and 2,004 deletions.
16 changes: 1 addition & 15 deletions src/BaseDocument.php
Expand Up @@ -30,6 +30,7 @@ abstract class BaseDocument extends BaseObject
use Operations\All;
use Operations\Update;
use Operations\Delete;
use Operations\VoidDocument;

/**
* Fetches the document's file attachments.
Expand Down Expand Up @@ -59,19 +60,4 @@ public function attachments(array $opts = [])

return [$attachments, $metadata];
}

/**
* Voids the document.
*
* @return bool
*/
public function void()
{
$response = $this->_client->request('post', $this->getEndpoint().'/void', [], []);

// update the local values with the response
$this->_values = array_replace((array) $response['body'], ['id' => $this->id]);

return 200 == $response['code'];
}
}
21 changes: 21 additions & 0 deletions src/Operations/VoidDocument.php
@@ -0,0 +1,21 @@
<?php

namespace Invoiced\Operations;

trait VoidDocument
{
/**
* Voids the document.
*
* @return bool
*/
public function void()
{
$response = $this->_client->request('post', $this->getEndpoint().'/void', [], []);

// update the local values with the response
$this->_values = array_replace((array) $response['body'], ['id' => $this->id]);

return 200 == $response['code'];
}
}
25 changes: 25 additions & 0 deletions tests/AbstractEndpointTestCase.php
@@ -0,0 +1,25 @@
<?php

namespace Invoiced\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Invoiced\Client;
use PHPUnit_Framework_TestCase;

abstract class AbstractEndpointTestCase extends PHPUnit_Framework_TestCase
{
/**
* @param Response|null $response
*
* @return Client
*/
protected function makeClient($response = null)
{
if (!$response) {
return new Client('API_KEY');
}

return new Client('API_KEY', false, null, new MockHandler([$response]));
}
}
38 changes: 7 additions & 31 deletions tests/ChargeTest.php
Expand Up @@ -2,47 +2,23 @@

namespace Invoiced\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Invoiced\Charge;
use Invoiced\Client;
use PHPUnit_Framework_TestCase;
use Invoiced\Tests\Traits\GetEndpointTrait;

class ChargeTest extends PHPUnit_Framework_TestCase
class ChargeTest extends AbstractEndpointTestCase
{
/**
* @var Client
*/
public static $invoiced;

/**
* @return void
*/
public static function setUpBeforeClass()
{
$mock = new MockHandler([
new Response(201, [], '{"id":123,"amount":100}'),
new Response(201, [], '{"id":456,"amount":50,"object":"refund"}'),
]);
use GetEndpointTrait;

self::$invoiced = new Client('API_KEY', false, null, $mock);
}

/**
* @return void
*/
public function testGetEndpoint()
{
$charge = new Charge(self::$invoiced, 123);
$this->assertEquals('/charges/123', $charge->getEndpoint());
}
const OBJECT_CLASS = 'Invoiced\\Charge';
const EXPECTED_ENDPOINT = '/charges/123';

/**
* @return void
*/
public function testCreate()
{
$charge = self::$invoiced->Charge->create(['customer' => 123]);
$client = $this->makeClient(new Response(201, [], '{"id":123,"amount":100}'));
$charge = $client->Charge->create(['customer' => 123]);

$this->assertInstanceOf('Invoiced\\Payment', $charge);
$this->assertEquals(123, $charge->id);
Expand Down
148 changes: 17 additions & 131 deletions tests/ContactTest.php
Expand Up @@ -2,136 +2,22 @@

namespace Invoiced\Tests;

use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;
use Invoiced\Client;
use Invoiced\Contact;
use PHPUnit_Framework_TestCase;

class ContactTest extends PHPUnit_Framework_TestCase
use Invoiced\Tests\Traits\CreateTrait;
use Invoiced\Tests\Traits\DeleteTrait;
use Invoiced\Tests\Traits\GetEndpointTrait;
use Invoiced\Tests\Traits\ListTrait;
use Invoiced\Tests\Traits\RetrieveTrait;
use Invoiced\Tests\Traits\UpdateTrait;

class ContactTest extends AbstractEndpointTestCase
{
/**
* @var Client
*/
public static $invoiced;

/**
* @return void
*/
public static function setUpBeforeClass()
{
$mock = new MockHandler([
new Response(201, [], '{"id":456,"name":"Nancy"}'),
new Response(200, [], '{"id":456,"name":"Nancy"}'),
new Response(200, [], '{"id":456,"name":"Nancy Drew"}'),
new Response(401),
new Response(200, ['X-Total-Count' => 15, 'Link' => '<https://api.invoiced.com/contacts?per_page=25&page=1>; rel="self", <https://api.invoiced.com/contacts?per_page=25&page=1>; rel="first", <https://api.invoiced.com/contacts?per_page=25&page=1>; rel="last"'], '[{"id":456,"name":"Nancy"}]'),
new Response(204),
]);

self::$invoiced = new Client('API_KEY', false, null, $mock);
}

/**
* @return void
*/
public function testGetEndpoint()
{
$contact = new Contact(self::$invoiced, 123);
$this->assertEquals('/contacts/123', $contact->getEndpoint());
}

/**
* @return void
*/
public function testCreate()
{
$contact = new Contact(self::$invoiced, null, []);
$contact = $contact->create(['name' => 'Nancy']);

$this->assertInstanceOf('Invoiced\\Contact', $contact);
$this->assertEquals(456, $contact->id);
$this->assertEquals('Nancy', $contact->name);
}

/**
* @return void
*/
public function testRetrieveNoId()
{
$this->setExpectedException('InvalidArgumentException');
$contact = new Contact(self::$invoiced, null, []);
$contact->retrieve('');
}

/**
* @return void
*/
public function testRetrieve()
{
$contact = new Contact(self::$invoiced, null, []);
$contact = $contact->retrieve(456);

$this->assertInstanceOf('Invoiced\\Contact', $contact);
$this->assertEquals(456, $contact->id);
$this->assertEquals('Nancy', $contact->name);
}

/**
* @return void
*/
public function testUpdateNoValue()
{
$contact = new Contact(self::$invoiced, 456, []);
$this->assertFalse($contact->save());
}

/**
* @return void
*/
public function testUpdate()
{
$contact = new Contact(self::$invoiced, 456, []);
$contact->name = 'Nancy Drew';
$this->assertTrue($contact->save());

$this->assertEquals('Nancy Drew', $contact->name);
}

/**
* @return void
*/
public function testUpdateFail()
{
$this->setExpectedException('Invoiced\\Error\\ApiError');

$contact = new Contact(self::$invoiced, 456, []);
$contact->name = 'Nancy Drew';
$contact->save();
}

/**
* @return void
*/
public function testAll()
{
$contact = new Contact(self::$invoiced, 456, []);
list($contacts, $metadata) = $contact->all();

$this->assertTrue(is_array($contacts));
$this->assertCount(1, $contacts);
$this->assertEquals(456, $contacts[0]->id);

$this->assertInstanceOf('Invoiced\\Collection', $metadata);
$this->assertEquals(15, $metadata->total_count);
}

/**
* @return void
*/
public function testDelete()
{
$contact = new Contact(self::$invoiced, 456, []);
$this->assertTrue($contact->delete());
}
use GetEndpointTrait;
use CreateTrait;
use RetrieveTrait;
use UpdateTrait;
use DeleteTrait;
use ListTrait;

const OBJECT_CLASS = 'Invoiced\\Contact';
const EXPECTED_ENDPOINT = '/contacts/123';
}

0 comments on commit d5492bb

Please sign in to comment.