diff --git a/src/BaseDocument.php b/src/BaseDocument.php index ffd4f20..abc0a71 100644 --- a/src/BaseDocument.php +++ b/src/BaseDocument.php @@ -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. @@ -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']; - } } diff --git a/src/Operations/VoidDocument.php b/src/Operations/VoidDocument.php new file mode 100644 index 0000000..eb61b15 --- /dev/null +++ b/src/Operations/VoidDocument.php @@ -0,0 +1,21 @@ +_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']; + } +} diff --git a/tests/AbstractEndpointTestCase.php b/tests/AbstractEndpointTestCase.php new file mode 100644 index 0000000..aaca42d --- /dev/null +++ b/tests/AbstractEndpointTestCase.php @@ -0,0 +1,25 @@ +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); diff --git a/tests/ContactTest.php b/tests/ContactTest.php index ce1e00c..6b7fddd 100644 --- a/tests/ContactTest.php +++ b/tests/ContactTest.php @@ -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' => '; rel="self", ; rel="first", ; 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'; } diff --git a/tests/CouponTest.php b/tests/CouponTest.php index 218c6e4..8485987 100644 --- a/tests/CouponTest.php +++ b/tests/CouponTest.php @@ -2,126 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\Coupon; -use PHPUnit_Framework_TestCase; - -class CouponTest 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 CouponTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Some Coupon"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":"test","name":"Some Item"}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $coupon = new Coupon(self::$invoiced, 'test'); - $this->assertEquals('/coupons/test', $coupon->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $coupon = self::$invoiced->Coupon->create(['id' => 'test', 'name' => 'Test']); - - $this->assertInstanceOf('Invoiced\\Coupon', $coupon); - $this->assertEquals('test', $coupon->id); - $this->assertEquals('Test', $coupon->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Coupon->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $coupon = self::$invoiced->Coupon->retrieve('test'); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $coupon = new Coupon(self::$invoiced, 'test'); - $this->assertFalse($coupon->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $coupon = new Coupon(self::$invoiced, 'test'); - $coupon->name = 'Some Item'; - $this->assertTrue($coupon->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $coupon = new Coupon(self::$invoiced, 'test'); - $coupon->name = 'Test'; - $coupon->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($coupons, $metadata) = self::$invoiced->Coupon->all(); - - $this->assertTrue(is_array($coupons)); - $this->assertCount(1, $coupons); - $this->assertEquals('test', $coupons[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $coupon = new Coupon(self::$invoiced, 'test'); - $this->assertTrue($coupon->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\Coupon'; + const EXPECTED_ENDPOINT = '/coupons/123'; } diff --git a/tests/CreditBalanceAdjustmentTest.php b/tests/CreditBalanceAdjustmentTest.php index 54658f1..2972b12 100644 --- a/tests/CreditBalanceAdjustmentTest.php +++ b/tests/CreditBalanceAdjustmentTest.php @@ -2,126 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\CreditBalanceAdjustment; -use PHPUnit_Framework_TestCase; - -class CreditBalanceAdjustmentTest 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 CreditBalanceAdjustmentTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":"test","amount":800}'), - new Response(200, [], '{"id":"test","amount":800}'), - new Response(200, [], '{"id":"test","amount":800}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":1234,"amount":800}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $creditBalanceAdjustment = new CreditBalanceAdjustment(self::$invoiced, 'test'); - $this->assertEquals('/credit_balance_adjustments/test', $creditBalanceAdjustment->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $creditBalanceAdjustment = self::$invoiced->CreditBalanceAdjustment->create(['id' => 'test', 'amount' => 800]); - - $this->assertInstanceOf('Invoiced\\CreditBalanceAdjustment', $creditBalanceAdjustment); - $this->assertEquals('test', $creditBalanceAdjustment->id); - $this->assertEquals(800, $creditBalanceAdjustment->amount); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->CreditBalanceAdjustment->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $creditBalanceAdjustment = self::$invoiced->CreditBalanceAdjustment->retrieve('test'); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $creditBalanceAdjustment = new CreditBalanceAdjustment(self::$invoiced, 'test'); - $this->assertFalse($creditBalanceAdjustment->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $creditBalanceAdjustment = new CreditBalanceAdjustment(self::$invoiced, 'test'); - $creditBalanceAdjustment->amount = 800; - $this->assertTrue($creditBalanceAdjustment->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $creditBalanceAdjustment = new CreditBalanceAdjustment(self::$invoiced, 'test'); - $creditBalanceAdjustment->amount = 800; - $creditBalanceAdjustment->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($creditBalanceAdjustments, $metadata) = self::$invoiced->CreditBalanceAdjustment->all(); - - $this->assertTrue(is_array($creditBalanceAdjustments)); - $this->assertCount(1, $creditBalanceAdjustments); - $this->assertEquals(1234, $creditBalanceAdjustments[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $creditBalanceAdjustment = new CreditBalanceAdjustment(self::$invoiced, 'test'); - $this->assertTrue($creditBalanceAdjustment->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\CreditBalanceAdjustment'; + const EXPECTED_ENDPOINT = '/credit_balance_adjustments/123'; } diff --git a/tests/CreditNoteTest.php b/tests/CreditNoteTest.php index c43db80..98820a5 100644 --- a/tests/CreditNoteTest.php +++ b/tests/CreditNoteTest.php @@ -2,176 +2,28 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\CreditNote; -use PHPUnit_Framework_TestCase; - -class CreditNoteTest extends PHPUnit_Framework_TestCase +use Invoiced\Tests\Traits\AttachmentsTrait; +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\SendTrait; +use Invoiced\Tests\Traits\UpdateTrait; +use Invoiced\Tests\Traits\VoidTrait; + +class CreditNoteTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":123,"number":"CN-0001"}'), - new Response(200, [], '{"id":123,"number":"CN-0001"}'), - new Response(200, [], '{"id":123,"closed":true}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"number":"CN-0001"}]'), - new Response(204), - new Response(201, [], '[{"id":4567,"email":"test@example.com"}]'), - new Response(200, ['X-Total-Count' => 10, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"file":{"id":456}}]'), - new Response(200, [], '{"id":"1234","status":"voided"}'), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $creditNote = new CreditNote(self::$invoiced, 123); - $this->assertEquals('/credit_notes/123', $creditNote->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $creditNote = self::$invoiced->CreditNote->create(['customer' => 123]); - - $this->assertInstanceOf('Invoiced\\CreditNote', $creditNote); - $this->assertEquals(123, $creditNote->id); - $this->assertEquals('CN-0001', $creditNote->number); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->CreditNote->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $creditNote = self::$invoiced->CreditNote->retrieve(123); - - $this->assertInstanceOf('Invoiced\\CreditNote', $creditNote); - $this->assertEquals(123, $creditNote->id); - $this->assertEquals('CN-0001', $creditNote->number); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $creditNote = new CreditNote(self::$invoiced, 123); - $this->assertFalse($creditNote->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $creditNote = new CreditNote(self::$invoiced, 123); - $creditNote->closed = true; - $this->assertTrue($creditNote->save()); - - $this->assertTrue($creditNote->closed); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $creditNote = new CreditNote(self::$invoiced, 123); - $creditNote->closed = true; - $creditNote->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($creditNotes, $metadata) = self::$invoiced->CreditNote->all(); - - $this->assertTrue(is_array($creditNotes)); - $this->assertCount(1, $creditNotes); - $this->assertEquals(123, $creditNotes[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $creditNote = new CreditNote(self::$invoiced, 123); - $this->assertTrue($creditNote->delete()); - } - - /** - * @return void - */ - public function testSend() - { - $creditNote = new CreditNote(self::$invoiced, 123); - $emails = $creditNote->send(); - - $this->assertTrue(is_array($emails)); - $this->assertCount(1, $emails); - $this->assertInstanceOf('Invoiced\\Email', $emails[0]); - $this->assertEquals(4567, $emails[0]->id); - } - - /** - * @return void - */ - public function testAttachments() - { - $creditNote = new CreditNote(self::$invoiced, 123); - list($attachments, $metadata) = $creditNote->attachments(); - $this->assertTrue(is_array($attachments)); - $this->assertCount(1, $attachments); - $this->assertEquals(456, $attachments[0]->id); - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(10, $metadata->total_count); - } - - /** - * @return void - */ - public function testVoid() - { - $creditNote = new CreditNote(self::$invoiced, 1234); - $creditNote->void(); - - $this->assertInstanceOf('Invoiced\\CreditNote', $creditNote); - $this->assertEquals(1234, $creditNote->id); - $this->assertEquals('voided', $creditNote->status); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + use VoidTrait; + use SendTrait; + use AttachmentsTrait; + + const OBJECT_CLASS = 'Invoiced\\CreditNote'; + const EXPECTED_ENDPOINT = '/credit_notes/123'; } diff --git a/tests/CustomerTest.php b/tests/CustomerTest.php index 59f34dd..3c19340 100644 --- a/tests/CustomerTest.php +++ b/tests/CustomerTest.php @@ -6,11 +6,26 @@ use GuzzleHttp\Psr7\Response; use Invoiced\Client; use Invoiced\Customer; -use 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; use stdClass; -class CustomerTest extends PHPUnit_Framework_TestCase +class CustomerTest extends AbstractEndpointTestCase { + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\Customer'; + const EXPECTED_ENDPOINT = '/customers/123'; + /** * @var Client */ @@ -22,12 +37,6 @@ class CustomerTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { $mock = new MockHandler([ - new Response(201, [], '{"id":123,"name":"Pied Piper"}'), - new Response(200, [], '{"id":"123","name":"Pied Piper"}'), - new Response(200, [], '{"id":123,"name":"Pied Piper","notes":"Terrible customer"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"name":"Pied Piper"}]'), - new Response(204), new Response(201, [], '[{"id":4567,"email":"test@example.com"}]'), new Response(200, [], '{"total_outstanding":1000,"available_credits":0,"past_due":true}'), new Response(201, [], '{"id":123,"name":"Nancy"}'), @@ -52,105 +61,6 @@ public static function setUpBeforeClass() self::$invoiced = new Client('API_KEY', false, null, $mock); } - /** - * @return void - */ - public function testGetEndpoint() - { - $customer = new Customer(self::$invoiced, 123); - $this->assertEquals('/customers/123', $customer->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $customer = self::$invoiced->Customer->create(['name' => 'Pied Piper']); - - $this->assertInstanceOf('Invoiced\\Customer', $customer); - $this->assertEquals(123, $customer->id); - $this->assertEquals('Pied Piper', $customer->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Customer->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $customer = self::$invoiced->Customer->retrieve(123); - - $this->assertInstanceOf('Invoiced\\Customer', $customer); - $this->assertEquals(123, $customer->id); - $this->assertEquals('Pied Piper', $customer->name); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $customer = new Customer(self::$invoiced, 123); - $this->assertFalse($customer->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $customer = new Customer(self::$invoiced, 123); - $customer->notes = 'Terrible customer'; - $this->assertTrue($customer->save()); - - $this->assertEquals('Terrible customer', $customer->notes); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $customer = new Customer(self::$invoiced, 123); - $customer->notes = 'Great customer'; - $customer->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($customers, $metadata) = self::$invoiced->Customer->all(); - - $this->assertTrue(is_array($customers)); - $this->assertCount(1, $customers); - $this->assertEquals(123, $customers[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $customer = new Customer(self::$invoiced, 123); - $this->assertTrue($customer->delete()); - } - /** * @return void */ diff --git a/tests/EstimateTest.php b/tests/EstimateTest.php index 94ac608..6ccb4ce 100644 --- a/tests/EstimateTest.php +++ b/tests/EstimateTest.php @@ -2,190 +2,44 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; -use Invoiced\Client; use Invoiced\Estimate; -use PHPUnit_Framework_TestCase; - -class EstimateTest extends PHPUnit_Framework_TestCase +use Invoiced\Tests\Traits\AttachmentsTrait; +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\SendTrait; +use Invoiced\Tests\Traits\UpdateTrait; +use Invoiced\Tests\Traits\VoidTrait; + +class EstimateTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":123,"number":"CN-0001"}'), - new Response(200, [], '{"id":123,"number":"CN-0001"}'), - new Response(200, [], '{"id":123,"closed":true}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"number":"CN-0001"}]'), - new Response(204), - new Response(201, [], '[{"id":4567,"email":"test@example.com"}]'), - new Response(200, ['X-Total-Count' => 10, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"file":{"id":456}}]'), - new Response(201, [], '{"id":456,"total":500}'), - new Response(200, [], '{"id":"1234","status":"voided"}'), - ]); + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + use VoidTrait; + use SendTrait; + use AttachmentsTrait; - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $estimate = new Estimate(self::$invoiced, 123); - $this->assertEquals('/estimates/123', $estimate->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $estimate = self::$invoiced->Estimate->create(['customer' => 123]); - - $this->assertInstanceOf('Invoiced\\Estimate', $estimate); - $this->assertEquals(123, $estimate->id); - $this->assertEquals('CN-0001', $estimate->number); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Estimate->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $estimate = self::$invoiced->Estimate->retrieve(123); - - $this->assertInstanceOf('Invoiced\\Estimate', $estimate); - $this->assertEquals(123, $estimate->id); - $this->assertEquals('CN-0001', $estimate->number); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $estimate = new Estimate(self::$invoiced, 123); - $this->assertFalse($estimate->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $estimate = new Estimate(self::$invoiced, 123); - $estimate->closed = true; - $this->assertTrue($estimate->save()); - - $this->assertTrue($estimate->closed); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $estimate = new Estimate(self::$invoiced, 123); - $estimate->closed = true; - $estimate->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($estimates, $metadata) = self::$invoiced->Estimate->all(); - - $this->assertTrue(is_array($estimates)); - $this->assertCount(1, $estimates); - $this->assertEquals(123, $estimates[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $estimate = new Estimate(self::$invoiced, 123); - $this->assertTrue($estimate->delete()); - } - - /** - * @return void - */ - public function testSend() - { - $estimate = new Estimate(self::$invoiced, 123); - $emails = $estimate->send(); - - $this->assertTrue(is_array($emails)); - $this->assertCount(1, $emails); - $this->assertInstanceOf('Invoiced\\Email', $emails[0]); - $this->assertEquals(4567, $emails[0]->id); - } - - /** - * @return void - */ - public function testAttachments() - { - $estimate = new Estimate(self::$invoiced, 123); - list($attachments, $metadata) = $estimate->attachments(); - $this->assertTrue(is_array($attachments)); - $this->assertCount(1, $attachments); - $this->assertEquals(456, $attachments[0]->id); - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(10, $metadata->total_count); - } + const OBJECT_CLASS = 'Invoiced\\Estimate'; + const EXPECTED_ENDPOINT = '/estimates/123'; /** * @return void */ public function testInvoice() { - $estimate = new Estimate(self::$invoiced, 456); + $client = $this->makeClient(new Response(201, [], '{"id":456,"total":500}')); + $estimate = new Estimate($client, 456); $invoice = $estimate->invoice(); $this->assertInstanceOf('Invoiced\\Invoice', $invoice); $this->assertEquals(456, $invoice->id); $this->assertEquals(500, $invoice->total); } - - /** - * @return void - */ - public function testVoid() - { - $estimate = new Estimate(self::$invoiced, 1234); - $estimate->void(); - - $this->assertInstanceOf('Invoiced\\Estimate', $estimate); - $this->assertEquals(1234, $estimate->id); - $this->assertEquals('voided', $estimate->status); - } } diff --git a/tests/EventTest.php b/tests/EventTest.php index 0eb4909..1b53f7a 100644 --- a/tests/EventTest.php +++ b/tests/EventTest.php @@ -2,52 +2,14 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\Event; -use PHPUnit_Framework_TestCase; +use Invoiced\Tests\Traits\GetEndpointTrait; +use Invoiced\Tests\Traits\ListTrait; -class EventTest extends PHPUnit_Framework_TestCase +class EventTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; + use ListTrait; + use GetEndpointTrait; - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"type":"customer.created"}]'), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $event = new Event(self::$invoiced, 123); - $this->assertEquals('/events/123', $event->getEndpoint()); - } - - /** - * @return void - */ - public function testAll() - { - list($events, $metadata) = self::$invoiced->Event->all(); - - $this->assertTrue(is_array($events)); - $this->assertCount(1, $events); - $this->assertEquals(123, $events[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } + const OBJECT_CLASS = 'Invoiced\\Event'; + const EXPECTED_ENDPOINT = '/events/123'; } diff --git a/tests/FileTest.php b/tests/FileTest.php index f1dae01..7833df5 100644 --- a/tests/FileTest.php +++ b/tests/FileTest.php @@ -2,84 +2,18 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\File; -use PHPUnit_Framework_TestCase; +use Invoiced\Tests\Traits\CreateTrait; +use Invoiced\Tests\Traits\DeleteTrait; +use Invoiced\Tests\Traits\GetEndpointTrait; +use Invoiced\Tests\Traits\RetrieveTrait; -class FileTest extends PHPUnit_Framework_TestCase +class FileTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use DeleteTrait; - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":456,"name":"Filename"}'), - new Response(200, [], '{"id":456,"name":"Filename"}'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $file = new File(self::$invoiced, 123); - $this->assertEquals('/files/123', $file->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $file = new File(self::$invoiced, null, []); - $file = $file->create(['name' => 'Filename']); - - $this->assertInstanceOf('Invoiced\\File', $file); - $this->assertEquals(456, $file->id); - $this->assertEquals('Filename', $file->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - $file = new File(self::$invoiced, null, []); - $file->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $file = new File(self::$invoiced, null, []); - $file = $file->retrieve(456); - - $this->assertInstanceOf('Invoiced\\File', $file); - $this->assertEquals(456, $file->id); - $this->assertEquals('Filename', $file->name); - } - - /** - * @return void - */ - public function testDelete() - { - $file = new File(self::$invoiced, 456, []); - $this->assertTrue($file->delete()); - } + const OBJECT_CLASS = 'Invoiced\\File'; + const EXPECTED_ENDPOINT = '/files/123'; } diff --git a/tests/InvoiceTest.php b/tests/InvoiceTest.php index 347520c..c9b2231 100644 --- a/tests/InvoiceTest.php +++ b/tests/InvoiceTest.php @@ -6,10 +6,31 @@ use GuzzleHttp\Psr7\Response; use Invoiced\Client; use Invoiced\Invoice; -use PHPUnit_Framework_TestCase; - -class InvoiceTest extends PHPUnit_Framework_TestCase +use Invoiced\Tests\Traits\AttachmentsTrait; +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\SendTrait; +use Invoiced\Tests\Traits\UpdateTrait; +use Invoiced\Tests\Traits\VoidTrait; + +class InvoiceTest extends AbstractEndpointTestCase { + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + use VoidTrait; + use SendTrait; + use AttachmentsTrait; + + const OBJECT_CLASS = 'Invoiced\\Invoice'; + const EXPECTED_ENDPOINT = '/invoices/123'; + /** * @var Client */ @@ -21,15 +42,7 @@ class InvoiceTest extends PHPUnit_Framework_TestCase public static function setUpBeforeClass() { $mock = new MockHandler([ - new Response(201, [], '{"id":123,"number":"INV-0001"}'), - new Response(200, [], '{"id":123,"number":"INV-0001"}'), - new Response(200, [], '{"id":123,"closed":true}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"number":"INV-0001"}]'), - new Response(204), - new Response(201, [], '[{"id":4567,"email":"test@example.com"}]'), new Response(200, [], '{"paid":true}'), - new Response(200, ['X-Total-Count' => 10, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"file":{"id":456}}]'), new Response(201, [], '{"id":123,"status":"active"}'), new Response(200, [], '{"id":"123","status":"active"}'), new Response(201, [], '[{"id":5678,"message":"test"}]'), @@ -37,125 +50,11 @@ public static function setUpBeforeClass() new Response(201, [], '{"id":1212,"notes":"test"}'), new Response(200, [], '{"id":"1212","notes":"test"}'), new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":1212,"notes":"test"}]'), - new Response(200, [], '{"id":"1234","status":"voided"}'), ]); self::$invoiced = new Client('API_KEY', false, null, $mock); } - /** - * @return void - */ - public function testGetEndpoint() - { - $invoice = new Invoice(self::$invoiced, 123); - $this->assertEquals('/invoices/123', $invoice->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $invoice = self::$invoiced->Invoice->create(['customer' => 123]); - - $this->assertInstanceOf('Invoiced\\Invoice', $invoice); - $this->assertEquals(123, $invoice->id); - $this->assertEquals('INV-0001', $invoice->number); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Invoice->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $invoice = self::$invoiced->Invoice->retrieve(123); - - $this->assertInstanceOf('Invoiced\\Invoice', $invoice); - $this->assertEquals(123, $invoice->id); - $this->assertEquals('INV-0001', $invoice->number); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $invoice = new Invoice(self::$invoiced, 123); - $this->assertFalse($invoice->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $invoice = new Invoice(self::$invoiced, 123); - $invoice->closed = true; - $this->assertTrue($invoice->save()); - - $this->assertTrue($invoice->closed); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $invoice = new Invoice(self::$invoiced, 123); - $invoice->closed = true; - $invoice->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($invoices, $metadata) = self::$invoiced->Invoice->all(); - - $this->assertTrue(is_array($invoices)); - $this->assertCount(1, $invoices); - $this->assertEquals(123, $invoices[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $invoice = new Invoice(self::$invoiced, 123); - $this->assertTrue($invoice->delete()); - } - - /** - * @return void - */ - public function testSend() - { - $invoice = new Invoice(self::$invoiced, 123); - $emails = $invoice->send(); - - $this->assertTrue(is_array($emails)); - $this->assertCount(1, $emails); - $this->assertInstanceOf('Invoiced\\Email', $emails[0]); - $this->assertEquals(4567, $emails[0]->id); - } - /** * @return void */ @@ -166,20 +65,6 @@ public function testPay() $this->assertTrue($invoice->paid); } - /** - * @return void - */ - public function testAttachments() - { - $invoice = new Invoice(self::$invoiced, 123); - list($attachments, $metadata) = $invoice->attachments(); - $this->assertTrue(is_array($attachments)); - $this->assertCount(1, $attachments); - $this->assertEquals(456, $attachments[0]->id); - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(10, $metadata->total_count); - } - /** * @return void */ @@ -280,17 +165,4 @@ public function testAllNotes() $this->assertInstanceOf('Invoiced\\Collection', $metadata); $this->assertEquals(15, $metadata->total_count); } - - /** - * @return void - */ - public function testVoid() - { - $invoice = new Invoice(self::$invoiced, 1234); - $invoice->void(); - - $this->assertInstanceOf('Invoiced\\Invoice', $invoice); - $this->assertEquals(1234, $invoice->id); - $this->assertEquals('voided', $invoice->status); - } } diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 286e323..cbf798f 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -2,126 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\Item; -use PHPUnit_Framework_TestCase; - -class ItemTest 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 ItemTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Some Item"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":"test","name":"Some Item"}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $item = new Item(self::$invoiced, 'test'); - $this->assertEquals('/items/test', $item->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $item = self::$invoiced->Item->create(['id' => 'test', 'name' => 'Test']); - - $this->assertInstanceOf('Invoiced\\Item', $item); - $this->assertEquals('test', $item->id); - $this->assertEquals('Test', $item->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Item->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $item = self::$invoiced->Item->retrieve('test'); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $item = new Item(self::$invoiced, 'test'); - $this->assertFalse($item->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $item = new Item(self::$invoiced, 'test'); - $item->name = 'Some Item'; - $this->assertTrue($item->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $item = new Item(self::$invoiced, 'test'); - $item->name = 'Test'; - $item->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($items, $metadata) = self::$invoiced->Item->all(); - - $this->assertTrue(is_array($items)); - $this->assertCount(1, $items); - $this->assertEquals('test', $items[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $item = new Item(self::$invoiced, 'test'); - $this->assertTrue($item->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\Item'; + const EXPECTED_ENDPOINT = '/items/123'; } diff --git a/tests/LineItemTest.php b/tests/LineItemTest.php index a6f7b36..49ae662 100644 --- a/tests/LineItemTest.php +++ b/tests/LineItemTest.php @@ -2,136 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\LineItem; -use PHPUnit_Framework_TestCase; - -class LineItemTest 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 LineItemTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":456,"unit_cost":500}'), - new Response(200, [], '{"id":456,"unit_cost":500}'), - new Response(200, [], '{"id":456,"unit_cost":600}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":456,"unit_cost":500}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $line = new LineItem(self::$invoiced, 123); - $this->assertEquals('/line_items/123', $line->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $line = new LineItem(self::$invoiced, null, []); - $lineItem = $line->create(['unit_cost' => 500]); - - $this->assertInstanceOf('Invoiced\\LineItem', $lineItem); - $this->assertEquals(456, $lineItem->id); - $this->assertEquals(500, $lineItem->unit_cost); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - $lineItem = new LineItem(self::$invoiced, null, []); - $lineItem->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $line = new LineItem(self::$invoiced, null, []); - $lineItem = $line->retrieve(456); - - $this->assertInstanceOf('Invoiced\\LineItem', $lineItem); - $this->assertEquals(456, $lineItem->id); - $this->assertEquals(500, $lineItem->unit_cost); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $lineItem = new LineItem(self::$invoiced, 456, []); - $this->assertFalse($lineItem->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $lineItem = new LineItem(self::$invoiced, 456, []); - $lineItem->unit_cost = 600; - $this->assertTrue($lineItem->save()); - - $this->assertEquals(600, $lineItem->unit_cost); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $lineItem = new LineItem(self::$invoiced, 456, []); - $lineItem->unit_cost = 600; - $lineItem->save(); - } - - /** - * @return void - */ - public function testAll() - { - $lineItem = new LineItem(self::$invoiced, 456, []); - list($lineItems, $metadata) = $lineItem->all(); - - $this->assertTrue(is_array($lineItems)); - $this->assertCount(1, $lineItems); - $this->assertEquals(456, $lineItems[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $lineItem = new LineItem(self::$invoiced, 456, []); - $this->assertTrue($lineItem->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\LineItem'; + const EXPECTED_ENDPOINT = '/line_items/123'; } diff --git a/tests/PaymentPlanTest.php b/tests/PaymentPlanTest.php index 77a248d..a55c943 100644 --- a/tests/PaymentPlanTest.php +++ b/tests/PaymentPlanTest.php @@ -2,54 +2,20 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; -use Invoiced\Client; use Invoiced\PaymentPlan; -use PHPUnit_Framework_TestCase; +use Invoiced\Tests\Traits\CreateTrait; +use Invoiced\Tests\Traits\DeleteTrait; +use Invoiced\Tests\Traits\GetEndpointTrait; -class PaymentPlanTest extends PHPUnit_Framework_TestCase +class PaymentPlanTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":456,"status":"active"}'), - new Response(200, [], '{"id":456,"status":"active"}'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $plan = new PaymentPlan(self::$invoiced, 123); - $this->assertEquals('/payment_plan', $plan->getEndpoint()); - } + use GetEndpointTrait; + use CreateTrait; + use DeleteTrait; - /** - * @return void - */ - public function testCreate() - { - $plan = new PaymentPlan(self::$invoiced, null, []); - $paymentPlan = $plan->create(['date' => 1234, 'amount' => 100]); - - $this->assertInstanceOf('Invoiced\\PaymentPlan', $paymentPlan); - $this->assertEquals(456, $paymentPlan->id); - $this->assertEquals('active', $paymentPlan->status); - } + const OBJECT_CLASS = 'Invoiced\\PaymentPlan'; + const EXPECTED_ENDPOINT = '/payment_plan'; /** * @return void @@ -58,7 +24,7 @@ public function testRetrieve() { $this->setExpectedException('BadMethodCallException'); - $plan = new PaymentPlan(self::$invoiced, null, []); + $plan = new PaymentPlan($this->makeClient(), null, []); $paymentPlan = $plan->retrieve(456); } @@ -67,20 +33,12 @@ public function testRetrieve() */ public function testGet() { - $plan = new PaymentPlan(self::$invoiced, null, []); + $client = $this->makeClient(new Response(200, [], '{"id":456,"status":"active"}')); + $plan = new PaymentPlan($client, null, []); $paymentPlan = $plan->get(); $this->assertInstanceOf('Invoiced\\PaymentPlan', $paymentPlan); $this->assertEquals(456, $paymentPlan->id); $this->assertEquals('active', $paymentPlan->status); } - - /** - * @return void - */ - public function testCancel() - { - $paymentPlan = new PaymentPlan(self::$invoiced, 456, []); - $this->assertTrue($paymentPlan->cancel()); - } } diff --git a/tests/PaymentSourceTest.php b/tests/PaymentSourceTest.php index b80b10d..377c22d 100644 --- a/tests/PaymentSourceTest.php +++ b/tests/PaymentSourceTest.php @@ -2,46 +2,24 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; -use Invoiced\Client; use Invoiced\PaymentSource; -use PHPUnit_Framework_TestCase; +use Invoiced\Tests\Traits\GetEndpointTrait; -class PaymentSourceTest extends PHPUnit_Framework_TestCase +class PaymentSourceTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":1231,"object":"card"}, {"id": 2342,"object":"bank_account"}]'), - ]); + use GetEndpointTrait; - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $plan = new PaymentSource(self::$invoiced, 123); - $this->assertEquals('/payment_sources/123', $plan->getEndpoint()); - } + const OBJECT_CLASS = 'Invoiced\\PaymentSource'; + const EXPECTED_ENDPOINT = '/payment_sources/123'; /** * @return void */ public function testAll() { - $source = new PaymentSource(self::$invoiced); + $client = $this->makeClient(new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":1231,"object":"card"}, {"id": 2342,"object":"bank_account"}]')); + $source = new PaymentSource($client); list($sources, $metadata) = $source->all(); $this->assertTrue(is_array($sources)); diff --git a/tests/PaymentTest.php b/tests/PaymentTest.php index cdd2dab..f0b8917 100644 --- a/tests/PaymentTest.php +++ b/tests/PaymentTest.php @@ -2,141 +2,24 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\Payment; -use PHPUnit_Framework_TestCase; - -class PaymentTest 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\SendTrait; +use Invoiced\Tests\Traits\UpdateTrait; + +class PaymentTest 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(200, [], '{"id":123,"amount":100}'), - new Response(200, [], '{"id":123,"status":"applied"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"amount":100}]'), - new Response(204), - new Response(201, [], '[{"id":4567,"email":"test@example.com"}]'), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $payment = new Payment(self::$invoiced, 123); - $this->assertEquals('/payments/123', $payment->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $payment = self::$invoiced->Payment->create(['customer' => 123]); - - $this->assertInstanceOf('Invoiced\\Payment', $payment); - $this->assertEquals(123, $payment->id); - $this->assertEquals(100, $payment->amount); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Payment->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $payment = self::$invoiced->Payment->retrieve(123); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $payment = new Payment(self::$invoiced, 123); - $this->assertFalse($payment->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $payment = new Payment(self::$invoiced, 123); - $payment->currency = 'usd'; - $this->assertTrue($payment->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $payment = new Payment(self::$invoiced, 123); - $payment->status = 'failed'; - $payment->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($payments, $metadata) = self::$invoiced->Payment->all(); - - $this->assertTrue(is_array($payments)); - $this->assertCount(1, $payments); - $this->assertEquals(123, $payments[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $payment = new Payment(self::$invoiced, 123); - $this->assertTrue($payment->delete()); - } - - /** - * @return void - */ - public function testSend() - { - $payment = new Payment(self::$invoiced, 123); - $emails = $payment->send(); - - $this->assertTrue(is_array($emails)); - $this->assertCount(1, $emails); - $this->assertInstanceOf('Invoiced\\Email', $emails[0]); - $this->assertEquals(4567, $emails[0]->id); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + use SendTrait; + + const OBJECT_CLASS = 'Invoiced\\Payment'; + const EXPECTED_ENDPOINT = '/payments/123'; } diff --git a/tests/PlanTest.php b/tests/PlanTest.php index b548208..5d5dac1 100644 --- a/tests/PlanTest.php +++ b/tests/PlanTest.php @@ -2,126 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\Plan; -use PHPUnit_Framework_TestCase; - -class PlanTest 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 PlanTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Some Item"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":"test","name":"Some Item"}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $plan = new Plan(self::$invoiced, 'test'); - $this->assertEquals('/plans/test', $plan->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $plan = self::$invoiced->Plan->create(['id' => 'test', 'name' => 'Test']); - - $this->assertInstanceOf('Invoiced\\Plan', $plan); - $this->assertEquals('test', $plan->id); - $this->assertEquals('Test', $plan->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Plan->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $plan = self::$invoiced->Plan->retrieve('test'); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $plan = new Plan(self::$invoiced, 'test'); - $this->assertFalse($plan->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $plan = new Plan(self::$invoiced, 'test'); - $plan->name = 'Some Item'; - $this->assertTrue($plan->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $plan = new Plan(self::$invoiced, 'test'); - $plan->name = 'Test'; - $plan->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($plans, $metadata) = self::$invoiced->Plan->all(); - - $this->assertTrue(is_array($plans)); - $this->assertCount(1, $plans); - $this->assertEquals('test', $plans[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $plan = new Plan(self::$invoiced, 'test'); - $this->assertTrue($plan->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\Plan'; + const EXPECTED_ENDPOINT = '/plans/123'; } diff --git a/tests/RefundTest.php b/tests/RefundTest.php index 877d0b8..b701b39 100644 --- a/tests/RefundTest.php +++ b/tests/RefundTest.php @@ -2,46 +2,24 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; -use Invoiced\Client; use Invoiced\Refund; -use PHPUnit_Framework_TestCase; +use Invoiced\Tests\Traits\GetEndpointTrait; -class RefundTest extends PHPUnit_Framework_TestCase +class RefundTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":123,"amount":50,"object":"refund"}'), - ]); + use GetEndpointTrait; - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $refund = new Refund(self::$invoiced, 123); - $this->assertEquals('/123', $refund->getEndpoint()); - } + const OBJECT_CLASS = 'Invoiced\\Refund'; + const EXPECTED_ENDPOINT = '/123'; /** * @return void */ public function testCreate() { - $refund = self::$invoiced->Refund->create(456, ['amount' => 50]); + $client = $this->makeClient(new Response(201, [], '{"id":123,"amount":50,"object":"refund"}')); + $refund = (new Refund($client))->create(456, ['amount' => 50]); $this->assertInstanceOf('Invoiced\\Refund', $refund); $this->assertEquals(123, $refund->id); diff --git a/tests/SubscriptionTest.php b/tests/SubscriptionTest.php index 2482c41..9b3be4f 100644 --- a/tests/SubscriptionTest.php +++ b/tests/SubscriptionTest.php @@ -2,137 +2,34 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; use GuzzleHttp\Psr7\Response; -use Invoiced\Client; use Invoiced\Subscription; -use PHPUnit_Framework_TestCase; - -class SubscriptionTest 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 SubscriptionTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":123,"plan":"starter"}'), - new Response(200, [], '{"id":123,"plan":"starter"}'), - new Response(200, [], '{"id":123,"plan":"pro"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123,"plan":"pro"}]'), - new Response(200, [], '{"id":123,"plan":"pro","status":"canceled"}'), - new Response(200, [], '{"first_invoice":{"id":false},"mrr":9}'), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $subscription = new Subscription(self::$invoiced, 123); - $this->assertEquals('/subscriptions/123', $subscription->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $subscription = self::$invoiced->Subscription->create(['customer' => 123, 'plan' => 'starter']); + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; - $this->assertInstanceOf('Invoiced\\Subscription', $subscription); - $this->assertEquals(123, $subscription->id); - $this->assertEquals('starter', $subscription->plan); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->Subscription->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $subscription = self::$invoiced->Subscription->retrieve(123); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $subscription = new Subscription(self::$invoiced, 123); - $this->assertFalse($subscription->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $subscription = new Subscription(self::$invoiced, 123); - $subscription->plan = 'pro'; - $this->assertTrue($subscription->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $subscription = new Subscription(self::$invoiced, 123); - $subscription->plan = 'starter'; - $subscription->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($subscriptions, $metadata) = self::$invoiced->Subscription->all(); - - $this->assertTrue(is_array($subscriptions)); - $this->assertCount(1, $subscriptions); - $this->assertEquals(123, $subscriptions[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testCancel() - { - $subscription = new Subscription(self::$invoiced, 123); - $this->assertTrue($subscription->cancel()); - $this->assertEquals('canceled', $subscription->status); - } + const OBJECT_CLASS = 'Invoiced\\Subscription'; + const EXPECTED_ENDPOINT = '/subscriptions/123'; /** * @return void */ public function testPreview() { - $subscription = new Subscription(self::$invoiced, 123); + $client = $this->makeClient(new Response(200, [], '{"first_invoice":{"id":false},"mrr":9}')); + $subscription = new Subscription($client, 123); $response = $subscription->preview(); // we do not expect subscription object; just json diff --git a/tests/TaxRateTest.php b/tests/TaxRateTest.php index daafe02..06c6b98 100644 --- a/tests/TaxRateTest.php +++ b/tests/TaxRateTest.php @@ -2,126 +2,22 @@ namespace Invoiced\Tests; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\Psr7\Response; -use Invoiced\Client; -use Invoiced\TaxRate; -use PHPUnit_Framework_TestCase; - -class TaxRateTest 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 TaxRateTest extends AbstractEndpointTestCase { - /** - * @var Client - */ - public static $invoiced; - - /** - * @return void - */ - public static function setUpBeforeClass() - { - $mock = new MockHandler([ - new Response(201, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Test"}'), - new Response(200, [], '{"id":"test","name":"Some Tax"}'), - new Response(401), - new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":"test","name":"Some Item"}]'), - new Response(204), - ]); - - self::$invoiced = new Client('API_KEY', false, null, $mock); - } - - /** - * @return void - */ - public function testGetEndpoint() - { - $taxRate = new TaxRate(self::$invoiced, 'test'); - $this->assertEquals('/tax_rates/test', $taxRate->getEndpoint()); - } - - /** - * @return void - */ - public function testCreate() - { - $taxRate = self::$invoiced->TaxRate->create(['id' => 'test', 'name' => 'Test']); - - $this->assertInstanceOf('Invoiced\\TaxRate', $taxRate); - $this->assertEquals('test', $taxRate->id); - $this->assertEquals('Test', $taxRate->name); - } - - /** - * @return void - */ - public function testRetrieveNoId() - { - $this->setExpectedException('InvalidArgumentException'); - self::$invoiced->TaxRate->retrieve(''); - } - - /** - * @return void - */ - public function testRetrieve() - { - $taxRate = self::$invoiced->TaxRate->retrieve('test'); - } - - /** - * @return void - */ - public function testUpdateNoValue() - { - $taxRate = new TaxRate(self::$invoiced, 'test'); - $this->assertFalse($taxRate->save()); - } - - /** - * @return void - */ - public function testUpdate() - { - $taxRate = new TaxRate(self::$invoiced, 'test'); - $taxRate->name = 'Some Item'; - $this->assertTrue($taxRate->save()); - } - - /** - * @return void - */ - public function testUpdateFail() - { - $this->setExpectedException('Invoiced\\Error\\ApiError'); - - $taxRate = new TaxRate(self::$invoiced, 'test'); - $taxRate->name = 'Test'; - $taxRate->save(); - } - - /** - * @return void - */ - public function testAll() - { - list($taxRates, $metadata) = self::$invoiced->TaxRate->all(); - - $this->assertTrue(is_array($taxRates)); - $this->assertCount(1, $taxRates); - $this->assertEquals('test', $taxRates[0]->id); - - $this->assertInstanceOf('Invoiced\\Collection', $metadata); - $this->assertEquals(15, $metadata->total_count); - } - - /** - * @return void - */ - public function testDelete() - { - $taxRate = new TaxRate(self::$invoiced, 'test'); - $this->assertTrue($taxRate->delete()); - } + use GetEndpointTrait; + use CreateTrait; + use RetrieveTrait; + use UpdateTrait; + use DeleteTrait; + use ListTrait; + + const OBJECT_CLASS = 'Invoiced\\TaxRate'; + const EXPECTED_ENDPOINT = '/tax_rates/123'; } diff --git a/tests/Traits/AttachmentsTrait.php b/tests/Traits/AttachmentsTrait.php new file mode 100644 index 0000000..231957d --- /dev/null +++ b/tests/Traits/AttachmentsTrait.php @@ -0,0 +1,23 @@ +makeClient(new Response(200, ['X-Total-Count' => 10, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"file":{"id":456}}]')); + $class = self::OBJECT_CLASS; + list($attachments, $metadata) = (new $class($client, 123))->attachments(); + $this->assertTrue(is_array($attachments)); + $this->assertCount(1, $attachments); + $this->assertEquals(456, $attachments[0]->id); + $this->assertInstanceOf('Invoiced\\Collection', $metadata); + $this->assertEquals(10, $metadata->total_count); + } +} diff --git a/tests/Traits/CreateTrait.php b/tests/Traits/CreateTrait.php new file mode 100644 index 0000000..16a681d --- /dev/null +++ b/tests/Traits/CreateTrait.php @@ -0,0 +1,22 @@ +makeClient(new Response(201, [], '{"id":456}')); + + $class = static::OBJECT_CLASS; + $newObj = (new $class($client))->create(['name' => 'Nancy']); + + $this->assertInstanceOf($class, $newObj); + $this->assertEquals(456, $newObj->id); + } +} diff --git a/tests/Traits/DeleteTrait.php b/tests/Traits/DeleteTrait.php new file mode 100644 index 0000000..da00290 --- /dev/null +++ b/tests/Traits/DeleteTrait.php @@ -0,0 +1,19 @@ +makeClient(new Response(204)); + $class = self::OBJECT_CLASS; + $obj = new $class($client, 456, []); + $this->assertTrue($obj->delete()); + } +} diff --git a/tests/Traits/GetEndpointTrait.php b/tests/Traits/GetEndpointTrait.php new file mode 100644 index 0000000..475e901 --- /dev/null +++ b/tests/Traits/GetEndpointTrait.php @@ -0,0 +1,16 @@ +makeClient(); + $class = static::OBJECT_CLASS; + $this->assertEquals(static::EXPECTED_ENDPOINT, (new $class($client, 123))->getEndpoint()); + } +} diff --git a/tests/Traits/ListTrait.php b/tests/Traits/ListTrait.php new file mode 100644 index 0000000..fe3db07 --- /dev/null +++ b/tests/Traits/ListTrait.php @@ -0,0 +1,26 @@ +makeClient(new Response(200, ['X-Total-Count' => 15, 'Link' => '; rel="self", ; rel="first", ; rel="last"'], '[{"id":123}]')); + + $class = static::OBJECT_CLASS; + list($objects, $metadata) = (new $class($client))->all(); + + $this->assertTrue(is_array($objects)); + $this->assertCount(1, $objects); + $this->assertEquals(123, $objects[0]->id); + + $this->assertInstanceOf('Invoiced\\Collection', $metadata); + $this->assertEquals(15, $metadata->total_count); + } +} diff --git a/tests/Traits/RetrieveTrait.php b/tests/Traits/RetrieveTrait.php new file mode 100644 index 0000000..99fc802 --- /dev/null +++ b/tests/Traits/RetrieveTrait.php @@ -0,0 +1,33 @@ +setExpectedException('InvalidArgumentException'); + $client = $this->makeClient(); + $class = self::OBJECT_CLASS; + (new $class($client))->retrieve(''); + } + + /** + * @return void + */ + public function testRetrieve() + { + $client = $this->makeClient(new Response(200, [], '{"id":456}')); + + $class = self::OBJECT_CLASS; + $obj = (new $class($client))->retrieve(456); + + $this->assertInstanceOf($class, $obj); + $this->assertEquals(456, $obj->id); + } +} diff --git a/tests/Traits/SendTrait.php b/tests/Traits/SendTrait.php new file mode 100644 index 0000000..1c3b33b --- /dev/null +++ b/tests/Traits/SendTrait.php @@ -0,0 +1,24 @@ +makeClient(new Response(201, [], '[{"id":4567,"email":"test@example.com"}]')); + + $class = self::OBJECT_CLASS; + $emails = (new $class($client, 123))->send(); + + $this->assertTrue(is_array($emails)); + $this->assertCount(1, $emails); + $this->assertInstanceOf('Invoiced\\Email', $emails[0]); + $this->assertEquals(4567, $emails[0]->id); + } +} diff --git a/tests/Traits/UpdateTrait.php b/tests/Traits/UpdateTrait.php new file mode 100644 index 0000000..cde7f76 --- /dev/null +++ b/tests/Traits/UpdateTrait.php @@ -0,0 +1,48 @@ +makeClient(); + $class = self::OBJECT_CLASS; + $obj = new $class($client, 456, []); + $this->assertFalse($obj->save()); + } + + /** + * @return void + */ + public function testUpdate() + { + $client = $this->makeClient(new Response(200, [], '{"id":456,"name":"Nancy Drew"}')); + + $class = self::OBJECT_CLASS; + $obj = new $class($client, 456, []); + $obj->name = 'Nancy Drew'; /* @phpstan-ignore-line */ + $this->assertTrue($obj->save()); + + $this->assertEquals('Nancy Drew', $obj->name); + } + + /** + * @return void + */ + public function testUpdateFail() + { + $this->setExpectedException('Invoiced\\Error\\ApiError'); + + $client = $this->makeClient(new Response(401)); + $class = self::OBJECT_CLASS; + $obj = new $class($client, 456, []); + $obj->name = 'Nancy Drew'; /* @phpstan-ignore-line */ + $obj->save(); + } +} diff --git a/tests/Traits/VoidTrait.php b/tests/Traits/VoidTrait.php new file mode 100644 index 0000000..48ce8f8 --- /dev/null +++ b/tests/Traits/VoidTrait.php @@ -0,0 +1,20 @@ +makeClient(new Response(200, [], '{"id":"1234","status":"voided"}')); + $class = self::OBJECT_CLASS; + $obj = new $class($client, 1234, []); + $this->assertTrue($obj->void()); + $this->assertEquals('voided', $obj->status); + } +}