Skip to content

Commit

Permalink
Codegen again & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardm-stripe committed Jul 9, 2021
1 parent 15c25bc commit b4ed515
Show file tree
Hide file tree
Showing 4 changed files with 180 additions and 37 deletions.
42 changes: 25 additions & 17 deletions lib/Quote.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,26 @@ class Quote extends ApiResource
const STATUS_DRAFT = 'draft';
const STATUS_OPEN = 'open';

/**
* @param callable $readBodyChunkCallable
* @param null|array $params
* @param null|array|string $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\File the created file
*/
public function pdf($readBodyChunkCallable, $params = null, $opts = null)
{
$opts = \Stripe\Util\RequestOptions::parse($opts);
if (null === $opts->apiBase) {
$opts->apiBase = Stripe::$apiUploadBase;
}

$url = $this->instanceUrl() . '/pdf';
$this->_requestStream('get', $url, $readBodyChunkCallable, $params, $opts);
}

/**
* @param null|array $params
* @param null|array|string $opts
Expand Down Expand Up @@ -114,31 +134,19 @@ public function finalizeQuote($params = null, $opts = null)
/**
* @param null|array $params
* @param null|array|string $opts
* @param mixed $id
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\Quote the alled quote
*/
public function allLineItems($params = null, $opts = null)
public static function allLineItems($id, $params = null, $opts = null)
{
$url = $this->instanceUrl() . '/line_items';
list($response, $opts) = $this->_request('get', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response, $opts);
$url = static::resourceUrl($id) . '/line_items';
list($response, $opts) = static::_staticRequest('get', $url, $params, $opts);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);

return $obj;
}

/**
* @param callable $readBodyChunkCallable
* @param null|array $params
* @param null|array|string $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*/
public function pdf($readBodyChunkCallable, $params = null, $opts = null)
{
$url = $this->instanceUrl() . '/pdf';
$this->_requestStream('get', $url, $readBodyChunkCallable, $params, $opts);
}
}
30 changes: 17 additions & 13 deletions lib/Service/QuoteService.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,23 @@ public function finalizeQuote($id, $params = null, $opts = null)
}

/**
* Download the PDF for a finalized quote.
* Retrieves the quote with the given ID.
*
* @param string $id
* @param callable $readBodyChunkCallable
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\Quote
*/
public function pdf($id, $readBodyChunkCallable, $params = null, $opts = null)
public function retrieve($id, $params = null, $opts = null)
{
return $this->requestStream('get', $this->buildPath('/v1/quotes/%s/pdf', $id), $params, $opts);
return $this->request('get', $this->buildPath('/v1/quotes/%s', $id), $params, $opts);
}

/**
* Retrieves the quote with the given ID.
* A quote models prices and services for a customer.
*
* @param string $id
* @param null|array $params
Expand All @@ -131,24 +132,27 @@ public function pdf($id, $readBodyChunkCallable, $params = null, $opts = null)
*
* @return \Stripe\Quote
*/
public function retrieve($id, $params = null, $opts = null)
public function update($id, $params = null, $opts = null)
{
return $this->request('get', $this->buildPath('/v1/quotes/%s', $id), $params, $opts);
return $this->request('post', $this->buildPath('/v1/quotes/%s', $id), $params, $opts);
}

/**
* A quote models prices and services for a customer.
* Download the PDF for a finalized quote.
*
* @param string $id
* @param callable $readBodyChunkCallable
* @param null|array $params
* @param null|array|\Stripe\Util\RequestOptions $opts
* @param null|array|string $opts
*
* @throws \Stripe\Exception\ApiErrorException if the request fails
*
* @return \Stripe\Quote
*/
public function update($id, $params = null, $opts = null)
public function pdf($id, $readBodyChunkCallable, $params = null, $opts = null)
{
return $this->request('post', $this->buildPath('/v1/quotes/%s', $id), $params, $opts);
$opts = \Stripe\Util\RequestOptions::parse($opts);
if (!isset($opts->apiBase)) {
$opts->apiBase = $this->getClient()->getFilesBase();
}
$this->requestStream('get', $this->buildPath('/v1/quotes/%s/pdf', $id), $readBodyChunkCallable, $params, $opts);
}
}
18 changes: 11 additions & 7 deletions tests/Stripe/QuoteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function testIsFinalizable()
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/finalize'
);
$resource->finalize();
$resource->finalizeQuote();
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

Expand All @@ -115,18 +115,22 @@ public function testCanListLineItems()
'get',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/line_items'
);
$resources = Quote::listLineItems(self::TEST_RESOURCE_ID);
$resources = Quote::allLineItems(self::TEST_RESOURCE_ID);
static::assertInternalType('array', $resources->data);
}

public function testCanPdf()
{
$resource = Quote::retrieve(self::TEST_RESOURCE_ID);
$this->expectsRequest(
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/finalize'
$this->expectsRequestStream(
'get',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/pdf',
null
);
$resource->pdf();
static::assertInstanceOf(\Stripe\Quote::class, $resource);
$output = '';
$resource->pdf(function ($chunk) use (&$output) {
$output .= $chunk;
});
static::assertSame('Stripe binary response', $output);
}
}
127 changes: 127 additions & 0 deletions tests/Stripe/Service/QuoteServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<?php

namespace Stripe\Service;

/**
* @internal
* @covers \Stripe\Service\QuoteService
*/
final class QuoteServiceTest extends \PHPUnit\Framework\TestCase
{
use \Stripe\TestHelper;

const TEST_RESOURCE_ID = 'qt_123';

/** @var \Stripe\StripeClient */
private $client;

/** @var QuoteService */
private $service;

/**
* @before
*/
protected function setUpService()
{
$this->client = new \Stripe\StripeClient(['api_key' => 'sk_test_123', 'api_base' => MOCK_URL, 'files_base' => MOCK_URL]);
$this->service = new QuoteService($this->client);
}

public function testAll()
{
$this->expectsRequest(
'get',
'/v1/quotes'
);
$resources = $this->service->all();
static::assertInternalType('array', $resources->data);
static::assertInstanceOf(\Stripe\Quote::class, $resources->data[0]);
}

public function testCreate()
{
$this->expectsRequest(
'post',
'/v1/quotes'
);
$resource = $this->service->create([
'customer' => 'cus_123',
]);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testRetrieve()
{
$this->expectsRequest(
'get',
'/v1/quotes/' . self::TEST_RESOURCE_ID
);
$resource = $this->service->retrieve(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testUpdate()
{
$this->expectsRequest(
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID
);
$resource = $this->service->update(self::TEST_RESOURCE_ID, [
'metadata' => ['key' => 'value'],
]);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testFinalizeQuote()
{
$this->expectsRequest(
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/finalize'
);
$resource = $this->service->finalizeQuote(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testCancel()
{
$this->expectsRequest(
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/cancel'
);
$resource = $this->service->cancel(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testAccept()
{
$this->expectsRequest(
'post',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/cancel'
);
$resource = $this->service->cancel(self::TEST_RESOURCE_ID);
static::assertInstanceOf(\Stripe\Quote::class, $resource);
}

public function testAllLines()
{
$this->expectsRequest(
'get',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/line_items'
);
$resources = $this->service->allLineItems(self::TEST_RESOURCE_ID);
static::assertInternalType('array', $resources->data);
}

public function testPdf()
{
$this->expectsRequestStream(
'get',
'/v1/quotes/' . self::TEST_RESOURCE_ID . '/pdf',
);
$output = '';
$resources = $this->service->pdf(self::TEST_RESOURCE_ID, function ($chunk) use (&$output) {
$output .= $chunk;
});
static::assertSame('Stripe binary response', $output);
}
}

0 comments on commit b4ed515

Please sign in to comment.