diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..3a37ec0 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,39 @@ +name: tests + +on: + pull_request: + push: + branches: + - master + - '*.x' + +jobs: + tests: + runs-on: ubuntu-22.04 + + strategy: + matrix: + php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3] + + name: PHP ${{ matrix.php }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install composer dependencies + uses: nick-invision/retry@v3 + with: + timeout_minutes: 5 + max_attempts: 5 + command: composer install --no-interaction --no-progress + + - name: Execute tests + run: vendor/bin/phpunit diff --git a/.gitignore b/.gitignore index 4dfacb8..6d78f51 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ /vendor # Files +.phpunit.result.cache composer.lock diff --git a/composer.json b/composer.json index d916294..4f12f3b 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "guzzlehttp/guzzle": "^6.4|^7.0" }, "require-dev": { - "phpunit/phpunit": "^8.0" + "phpunit/phpunit": "^7.0|^8.0" }, "autoload": { "psr-4": { diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..482b8c3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,13 @@ + + + + + ./tests/Unit + + + + + diff --git a/tests/Unit/Api/Response/BaselinkerExceptionTest.php b/tests/Unit/Api/Response/BaselinkerExceptionTest.php new file mode 100644 index 0000000..32a65e1 --- /dev/null +++ b/tests/Unit/Api/Response/BaselinkerExceptionTest.php @@ -0,0 +1,17 @@ +assertEquals('Message', $exception->responseMessage()); + $this->assertEquals('CODE', $exception->responseCode()); + } +} diff --git a/tests/Unit/Api/Response/ResponseTest.php b/tests/Unit/Api/Response/ResponseTest.php new file mode 100644 index 0000000..6fee803 --- /dev/null +++ b/tests/Unit/Api/Response/ResponseTest.php @@ -0,0 +1,54 @@ + 'SUCCESS', + 'list' => [ + 'foo' => 'bar', + ], + ]); + + $response = new Response( + new GuzzleResponse(200, [], $body) + ); + + $this->assertEquals('{"status":"SUCCESS","list":{"foo":"bar"}}', $response->contents()); + + $this->assertEquals([ + 'status' => 'SUCCESS', + 'list' => [ + 'foo' => 'bar', + ], + ], $response->toArray()); + + $this->assertEquals([ + 'foo' => 'bar', + ], $response->getParameter('list')); + } + + public function testWithErrorThrowsException(): void + { + $body = json_encode([ + 'status' => 'ERROR', + 'error_code' => 'CODE', + 'error_message' => 'Message', + ]); + + $this->expectException(BaselinkerException::class); + $this->expectExceptionMessage('[CODE] Message'); + + new Response( + new GuzzleResponse(200, [], $body) + ); + } +} diff --git a/tests/Unit/BaselinkerTest.php b/tests/Unit/BaselinkerTest.php new file mode 100644 index 0000000..aac35cd --- /dev/null +++ b/tests/Unit/BaselinkerTest.php @@ -0,0 +1,69 @@ + 'foo', + ]); + + $productCatalog = $baselinker->productCatalog(); + + $this->assertInstanceOf(ProductCatalogInterface::class, $productCatalog); + } + + public function testExternalStorages(): void + { + $baselinker = new Baselinker([ + 'token' => 'foo', + ]); + + $externalStorages = $baselinker->externalStorages(); + + $this->assertInstanceOf(ExternalStoragesInterface::class, $externalStorages); + } + + public function testOrders(): void + { + $baselinker = new Baselinker([ + 'token' => 'foo', + ]); + + $orders = $baselinker->orders(); + + $this->assertInstanceOf(OrdersInterface::class, $orders); + } + + public function testOrderReturns(): void + { + $baselinker = new Baselinker([ + 'token' => 'foo', + ]); + + $orderReturns = $baselinker->orderReturns(); + + $this->assertInstanceOf(OrderReturnsInterface::class, $orderReturns); + } + + public function testCourierShipments(): void + { + $baselinker = new Baselinker([ + 'token' => 'foo', + ]); + + $courierShipments = $baselinker->courierShipments(); + + $this->assertInstanceOf(CourierShipmentsInterface::class, $courierShipments); + } +} diff --git a/tests/Unit/ConfigTest.php b/tests/Unit/ConfigTest.php new file mode 100644 index 0000000..be96033 --- /dev/null +++ b/tests/Unit/ConfigTest.php @@ -0,0 +1,50 @@ + 'foo', + ]); + + $this->assertEquals('foo', $config->getToken()); + } + + /** + * @dataProvider invalidConfigDataProvider + */ + public function testSetWithInvalidTokenThrowsException(array $config): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Parameter "token" must be provided in the configuration.'); + + new Config($config); + } + + public static function invalidConfigDataProvider(): Iterator + { + yield 'empty config' => [ + [], + ]; + + yield 'null as token' => [ + [ + 'token' => null, + ] + ]; + + yield 'empty string as token' => [ + [ + 'token' => '', + ] + ]; + } +}