From 1d607be09f36f5c4e82319db42ce09c6936010cf Mon Sep 17 00:00:00 2001 From: Hassansin Date: Tue, 24 Jul 2018 19:20:49 +0600 Subject: [PATCH] Drop `abstract static` methods [ch4671] #40 * use different php versions from Travis * disable master-only travis build * Use php7 in Dockerfile and update Tests namespace * Trigger Travis build * Travis before_script * Fixed Call to undefined method ClientTest::setExpectedException() in phpunit 6 * Removed abstract static method, testing in php 5 & 7, code formatting * class alias --- .gitattributes | 3 +- .travis.yml | 17 +- Dockerfile | 12 +- Makefile | 15 +- composer.json | 1 - php.ini | 1 + phpunit.xml.dist | 3 +- src/Customer.php | 4 +- src/Import/Customer.php | 3 +- src/Import/CustomerInvoices.php | 3 +- src/Import/DataSource.php | 3 +- src/Import/Invoice.php | 3 +- src/Import/LineItems/OneTime.php | 3 +- src/Import/LineItems/Subscription.php | 3 +- src/Import/Plan.php | 3 +- src/Import/Subscription.php | 5 +- .../Transactions/AbstractTransaction.php | 3 +- src/Import/Transactions/Payment.php | 3 +- src/Import/Transactions/Refund.php | 3 +- src/LineItems/AbstractLineItem.php | 48 ++-- src/Resource/EntryTrait.php | 2 - tests/Unit/CustomerTest.php | 229 ++++++++++++++++++ .../Exceptions/ChartMogulExceptionTest.php | 2 +- tests/Unit/Http/ClientTest.php | 7 +- tests/Unit/Import/DataSourceTest.php | 2 +- tests/Unit/InvoiceTest.php | 2 +- tests/Unit/MetricsTest.php | 106 ++++++++ tests/Unit/Resources/AbstractModelTest.php | 2 +- tests/Unit/SubscriptionTest.php | 27 ++- tests/bootstrap.php | 5 + 30 files changed, 453 insertions(+), 70 deletions(-) create mode 100644 php.ini create mode 100644 tests/Unit/CustomerTest.php create mode 100644 tests/Unit/MetricsTest.php diff --git a/.gitattributes b/.gitattributes index 00232af..fd4ddfc 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1 @@ -/coverage export-ignore - +/coverage export-ignore php.ini diff --git a/.travis.yml b/.travis.yml index 9d77e7b..a1790d2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,12 @@ -sudo: required +language: php +php: + - '5.5' + - '5.6' + - '7.2' + - nightly -services: - - docker +before_script: + - composer install script: - - make test - -branches: - only: - - master + - phpunit diff --git a/Dockerfile b/Dockerfile index 5ba6cb9..bf02454 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,14 @@ -FROM php:5.5.37-fpm +ARG VERSION +FROM php:${VERSION} RUN curl -O https://getcomposer.org/composer.phar RUN mv composer.phar /usr/local/bin/composer RUN chmod a+x /usr/local/bin/composer -RUN pecl install xdebug-2.5.5 && docker-php-ext-enable xdebug RUN apt-get update && apt-get install -y git unzip +ARG VERSION +RUN if [ "$VERSION" = "7.2" ]; then \ + composer global require phpunit/phpunit:^7 && pecl install xdebug-2.7.0alpha1; else \ + composer global require phpunit/phpunit:^4 && pecl install xdebug-2.5.5; fi +RUN docker-php-ext-enable xdebug +RUN composer global require hirak/prestissimo +ENV PATH="${PATH}:/root/.composer/vendor/bin" +COPY php.ini /usr/local/etc/php/ diff --git a/Makefile b/Makefile index 3ff4569..af16aa6 100644 --- a/Makefile +++ b/Makefile @@ -1,15 +1,16 @@ -RUNNER=docker run -it --rm --workdir "/src" -v "$(PWD):/src" chartmogulphp /bin/bash -c +RUNNER=docker run -it --rm --workdir "/src" -v "$(PWD):/src" -v "$(HOME)/.composer/cache:/root/.composer/cache" chartmogulphp5 /bin/bash -c .PHONY: build composer php build: - if [ "$(shell docker images -q chartmogulphp 2> /dev/null)" = "" ]; then docker build --tag=chartmogulphp .; fi; -composer: build - $(RUNNER) "composer $(filter-out $@,$(MAKECMDGOALS))" -dependencies: build - if [ ! -d vendor ]; then make composer install; fi; + @docker build --build-arg VERSION=5.5 --tag=chartmogulphp5 . + @docker build --build-arg VERSION=7.2 --tag=chartmogulphp7 . +composer: + @$(RUNNER) "composer $(filter-out $@,$(MAKECMDGOALS))" +dependencies: + make -s composer update -- --prefer-dist test: dependencies - $(RUNNER) "./vendor/bin/phpunit --coverage-text --coverage-html ./coverage " + $(RUNNER) "phpunit --coverage-text --coverage-html ./coverage " php: dependencies $(RUNNER) "php $(filter-out $@,$(MAKECMDGOALS))" cs: dependencies diff --git a/composer.json b/composer.json index 71584e5..ce728b0 100644 --- a/composer.json +++ b/composer.json @@ -29,7 +29,6 @@ "php-http/mock-client": "^0.3", "squizlabs/php_codesniffer": "^2.7", "phpdocumentor/phpdocumentor": "^2.9", - "phpunit/phpunit": "4.*", "evert/phpdoc-md": "^0.2.0" } } diff --git a/php.ini b/php.ini new file mode 100644 index 0000000..fc1fa14 --- /dev/null +++ b/php.ini @@ -0,0 +1 @@ +error_reporting = E_ALL diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 0768065..51a5098 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,6 +5,7 @@ convertNoticesToExceptions="true" convertWarningsToExceptions="true" stopOnFailure="false" + verbose="true" bootstrap="tests/bootstrap.php"> @@ -16,4 +17,4 @@ ./src - \ No newline at end of file + diff --git a/src/Customer.php b/src/Customer.php index 9aafc0e..2edfb30 100644 --- a/src/Customer.php +++ b/src/Customer.php @@ -110,9 +110,9 @@ public static function findByExternalId($externalId) $response = static::all($externalId); if (is_null($response)) { - return null; + return null; } else { - return $response->first(); + return $response->first(); } } diff --git a/src/Import/Customer.php b/src/Import/Customer.php index b105037..9002dee 100644 --- a/src/Import/Customer.php +++ b/src/Import/Customer.php @@ -5,6 +5,7 @@ /** * @deprecated Use ChartMogul\Customer, Import\CustomerInvoices or Import\Subscription. */ -class Customer extends ChartMogul\Customer { +class Customer extends ChartMogul\Customer +{ } diff --git a/src/Import/CustomerInvoices.php b/src/Import/CustomerInvoices.php index f9e491d..016319f 100644 --- a/src/Import/CustomerInvoices.php +++ b/src/Import/CustomerInvoices.php @@ -5,6 +5,7 @@ /** * @deprecated Use \ChartMogul\CustomerInvoices */ -class CustomerInvoices extends \ChartMogul\CustomerInvoices { +class CustomerInvoices extends \ChartMogul\CustomerInvoices +{ } diff --git a/src/Import/DataSource.php b/src/Import/DataSource.php index cb2928b..ddc5675 100644 --- a/src/Import/DataSource.php +++ b/src/Import/DataSource.php @@ -5,6 +5,7 @@ /** * @deprecated Use ChartMogul\DataSource */ -class DataSource extends ChartMogul\DataSource { +class DataSource extends ChartMogul\DataSource +{ } diff --git a/src/Import/Invoice.php b/src/Import/Invoice.php index 346a09f..51a3613 100644 --- a/src/Import/Invoice.php +++ b/src/Import/Invoice.php @@ -5,6 +5,7 @@ /** * @deprecated Use \ChartMogul\Invoice */ -class Invoice extends \ChartMogul\Invoice { +class Invoice extends \ChartMogul\Invoice +{ } diff --git a/src/Import/LineItems/OneTime.php b/src/Import/LineItems/OneTime.php index 5d5fa4b..60b5a7b 100644 --- a/src/Import/LineItems/OneTime.php +++ b/src/Import/LineItems/OneTime.php @@ -6,6 +6,7 @@ * @codeCoverageIgnore * @deprecated Use ChartMogul\OneTime */ -class OneTime extends ChartMogul\OneTime { +class OneTime extends ChartMogul\OneTime +{ } diff --git a/src/Import/LineItems/Subscription.php b/src/Import/LineItems/Subscription.php index c529f45..26a1bd8 100644 --- a/src/Import/LineItems/Subscription.php +++ b/src/Import/LineItems/Subscription.php @@ -6,6 +6,7 @@ * @codeCoverageIgnore * @deprecated Use ChartMogul\Subscription */ -class Subscription extends Subscription{ +class Subscription extends Subscription +{ } diff --git a/src/Import/Plan.php b/src/Import/Plan.php index 5839afa..476ef93 100644 --- a/src/Import/Plan.php +++ b/src/Import/Plan.php @@ -5,6 +5,7 @@ /** * @deprecated Use ChartMogul\Plan */ -class Plan extends ChartMogul\Plan { +class Plan extends ChartMogul\Plan +{ } diff --git a/src/Import/Subscription.php b/src/Import/Subscription.php index f8e7231..e5ec99c 100644 --- a/src/Import/Subscription.php +++ b/src/Import/Subscription.php @@ -5,6 +5,7 @@ /** * @deprecated Use \ChartMogul\Subscription */ -class Subscription extends \ChartMogul\Subscription { +class Subscription extends \ChartMogul\Subscription +{ -} \ No newline at end of file +} diff --git a/src/Import/Transactions/AbstractTransaction.php b/src/Import/Transactions/AbstractTransaction.php index df4cd56..8612bd5 100644 --- a/src/Import/Transactions/AbstractTransaction.php +++ b/src/Import/Transactions/AbstractTransaction.php @@ -6,6 +6,7 @@ * @property-read string $uuid * @deprecated Use ChartMogul\AbstractTransaction */ -abstract class AbstractTransaction extends ChartMogul\AbstractTransaction { +abstract class AbstractTransaction extends ChartMogul\AbstractTransaction +{ } diff --git a/src/Import/Transactions/Payment.php b/src/Import/Transactions/Payment.php index 985b412..54e7bc0 100644 --- a/src/Import/Transactions/Payment.php +++ b/src/Import/Transactions/Payment.php @@ -6,6 +6,7 @@ * @codeCoverageIgnore * @deprecated Use ChartMogul\Payment */ -class Payment extends ChartMogul\Payment { +class Payment extends ChartMogul\Payment +{ } diff --git a/src/Import/Transactions/Refund.php b/src/Import/Transactions/Refund.php index a6d6e93..ef57dce 100644 --- a/src/Import/Transactions/Refund.php +++ b/src/Import/Transactions/Refund.php @@ -6,6 +6,7 @@ * @codeCoverageIgnore * @deprecated Use ChartMogul\Refund */ -class Refund extends ChartMogul\Refund { +class Refund extends ChartMogul\Refund +{ } diff --git a/src/LineItems/AbstractLineItem.php b/src/LineItems/AbstractLineItem.php index 550b651..d3c79f1 100644 --- a/src/LineItems/AbstractLineItem.php +++ b/src/LineItems/AbstractLineItem.php @@ -1,24 +1,24 @@ -entries = new ArrayCollection($entries); diff --git a/tests/Unit/CustomerTest.php b/tests/Unit/CustomerTest.php new file mode 100644 index 0000000..eed16bd --- /dev/null +++ b/tests/Unit/CustomerTest.php @@ -0,0 +1,229 @@ + 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $uuid = 'cus_de305d54-75b4-431b-adb2-eb6b9e546012'; + + $cmClient = new Client(null, $mockClient); + $result = Customer::retrieve($uuid, $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/customers/".$uuid, $uri->getPath()); + + $this->assertTrue($result instanceof Customer); + $this->assertEquals($uuid, $result->uuid); + } + public function testCreateCustomer() + { + $stream = Psr7\stream_for(CustomerTest::CREATE_CUSTOMER_JSON); + $response = new Response(200, ['Content-Type' => 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $cmClient = new Client(null, $mockClient); + $result = Customer::create([ + "data_source_uuid" => "ds_fef05d54-47b4-431b-aed2-eb6b9e545430", + "external_id" => "cus_0001", + "name" => "Adam Smith", + "email" => "adam@smith.com", + "country" => "US", + "city" => "New York", + "lead_created_at" => "2016-10-01T00:00:00.000Z", + "free_trial_started_at" => "2016-11-02T00:00:00.000Z" + ], $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("POST", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/customers", $uri->getPath()); + + $this->assertTrue($result instanceof Customer); + } + public function testSearchCustomer(){ + + $stream = Psr7\stream_for(CustomerTest::SEARCH_CUSTOMER_JSON); + $response = new Response(200, ['Content-Type' => 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + + $email = "bob@examplecompany.com"; + $cmClient = new Client(null, $mockClient); + $result = Customer::search($email, $cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("email=".urlencode($email), $uri->getQuery()); + $this->assertEquals("/v1/customers/search", $uri->getPath()); + + $this->assertTrue($result[0] instanceof Customer); + $this->assertEquals($result->has_more, false); + $this->assertEquals($result->page, 1); + $this->assertEquals($result->per_page, 200); + } +} diff --git a/tests/Unit/Exceptions/ChartMogulExceptionTest.php b/tests/Unit/Exceptions/ChartMogulExceptionTest.php index af5cdca..c615240 100644 --- a/tests/Unit/Exceptions/ChartMogulExceptionTest.php +++ b/tests/Unit/Exceptions/ChartMogulExceptionTest.php @@ -2,7 +2,7 @@ use ChartMogul\Exceptions\ChartMogulException; -class ChartMogulExceptionTest extends PHPUnit_Framework_TestCase +class ChartMogulExceptionTest extends \PHPUnit\Framework\TestCase { public function testConstruct() diff --git a/tests/Unit/Http/ClientTest.php b/tests/Unit/Http/ClientTest.php index 7f6d595..a504aec 100644 --- a/tests/Unit/Http/ClientTest.php +++ b/tests/Unit/Http/ClientTest.php @@ -3,7 +3,7 @@ use ChartMogul\Http\Client; use ChartMogul\Exceptions\ChartMogulException; -class ClientTest extends PHPUnit_Framework_TestCase +class ClientTest extends \PHPUnit\Framework\TestCase { public function testConstructor() @@ -83,6 +83,7 @@ public function provider() } /** * @dataProvider provider + * @expectedException \ChartMogul\Exceptions\ChartMogulException */ public function testHandleResponseExceptions($status, $exception) { @@ -91,8 +92,6 @@ public function testHandleResponseExceptions($status, $exception) ->setMethods(null) ->getMock(); - $this->setExpectedException($exception); - $res = Http\Discovery\MessageFactoryDiscovery::find()->createResponse( $status, null, @@ -137,7 +136,7 @@ public function testSend($path, $method, $data, $target, $rBody) ->method('getUserAgent') ->willReturn('agent'); - $response = $this->getMock('Psr\Http\Message\ResponseInterface'); + $response = $this->getMockBuilder('Psr\Http\Message\ResponseInterface')->getMock(); $mock->expects($this->once()) ->method('handleResponse') diff --git a/tests/Unit/Import/DataSourceTest.php b/tests/Unit/Import/DataSourceTest.php index be52e4a..f5e6780 100644 --- a/tests/Unit/Import/DataSourceTest.php +++ b/tests/Unit/Import/DataSourceTest.php @@ -4,6 +4,6 @@ use ChartMogul\Http\Client; use ChartMogul\Configuration; -// class DataSourceTest extends PHPUnit_Framework_TestCase { +// class DataSourceTest extends \PHPUnit\Framework\TestCase { // } diff --git a/tests/Unit/InvoiceTest.php b/tests/Unit/InvoiceTest.php index 801fd73..8a960b3 100644 --- a/tests/Unit/InvoiceTest.php +++ b/tests/Unit/InvoiceTest.php @@ -6,7 +6,7 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Response; -class InvoiceTest extends PHPUnit_Framework_TestCase +class InvoiceTest extends \PHPUnit\Framework\TestCase { const ALL_INVOICE_JSON = '{ "invoices": [ diff --git a/tests/Unit/MetricsTest.php b/tests/Unit/MetricsTest.php new file mode 100644 index 0000000..e859c78 --- /dev/null +++ b/tests/Unit/MetricsTest.php @@ -0,0 +1,106 @@ + 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $cmClient = new Client(null, $mockClient); + $result = Metrics::all(["interval" => "month"],$cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("interval=month", $uri->getQuery()); + $this->assertEquals("/v1/metrics/all", $uri->getPath()); + + $this->assertTrue($result->entries[0] instanceof AllKeyMetric); + $this->assertEquals($result->entries[0]->date, "2015-01-31"); + + } + public function testLtv(){ + + $stream = Psr7\stream_for(MetricsTest::LTV_JSON); + $response = new Response(200, ['Content-Type' => 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $cmClient = new Client(null, $mockClient); + $result = Metrics::ltv([ + "start-date" => "2015-01-01", + "end-date" => "2015-11-01", + ],$cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("start-date=2015-01-01&end-date=2015-11-01", $uri->getQuery()); + $this->assertEquals("/v1/metrics/ltv", $uri->getPath()); + + $this->assertTrue($result->entries[0] instanceof LTV); + $this->assertTrue($result->summary instanceof Summary); + } +} diff --git a/tests/Unit/Resources/AbstractModelTest.php b/tests/Unit/Resources/AbstractModelTest.php index caec614..c6c872a 100644 --- a/tests/Unit/Resources/AbstractModelTest.php +++ b/tests/Unit/Resources/AbstractModelTest.php @@ -2,7 +2,7 @@ use ChartMogul\Resource\AbstractModel; -class AbstractModelTest extends PHPUnit_Framework_TestCase +class AbstractModelTest extends \PHPUnit\Framework\TestCase { diff --git a/tests/Unit/SubscriptionTest.php b/tests/Unit/SubscriptionTest.php index a53002b..944187e 100644 --- a/tests/Unit/SubscriptionTest.php +++ b/tests/Unit/SubscriptionTest.php @@ -6,7 +6,7 @@ use GuzzleHttp\Psr7; use GuzzleHttp\Psr7\Response; -class SubscriptionTest extends PHPUnit_Framework_TestCase +class SubscriptionTest extends \PHPUnit\Framework\TestCase { const ALL_SUBS_JSON = '{ @@ -23,6 +23,11 @@ class SubscriptionTest extends PHPUnit_Framework_TestCase "current_page": 2, "total_pages": 3 }'; + + const CANCEL_SUBSCRIPTION = '{ + "cancellation_dates": ["2016-01-01T10:00:00.000Z", "2017-01-01T10:00:00.000Z"] + }'; + public function testAllSubscriptions() { $stream = Psr7\stream_for(SubscriptionTest::ALL_SUBS_JSON); @@ -47,4 +52,24 @@ public function testAllSubscriptions() $this->assertEquals(2, $result->current_page); $this->assertEquals(3, $result->total_pages); } + public function testCancel(){ + $stream = Psr7\stream_for(SubscriptionTest::CANCEL_SUBSCRIPTION); + $response = new Response(200, ['Content-Type' => 'application/json'], $stream); + $mockClient = new \Http\Mock\Client(); + $mockClient->addResponse($response); + + $cmClient = new Client(null, $mockClient); + $subsUUID = "sub_e6bc5407-e258-4de0-bb43-61faaf062035"; + $subscription = new ChartMogul\Subscription(["uuid" => $subsUUID], $cmClient); + $canceldate = '2016-01-01T10:00:00.000Z'; + $result = $subscription->cancel($canceldate); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("PATCH", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/import/subscriptions/".$subsUUID, $uri->getPath()); + + $this->assertTrue($result instanceof Subscription); + } } diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 6c8c4f5..39c0122 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -1,3 +1,8 @@