diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..895ffd6 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +/.github/ export-ignore +/tests/ export-ignore +/.gitattributes export-ignore +/.gitignore export-ignore +/.php-cs-fixer.dist.php export-ignore +/phpstan.dist.neon export-ignore +/phpstan-baseline.neon export-ignore +/phpunit.xml.dist export-ignore + diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..a36fc96 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,45 @@ +name: CI + +on: + - push + - pull_request + +jobs: + tests: + name: Tests + + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + + matrix: + os: + - ubuntu-latest + - windows-latest + + php-version: + - 8.2 + - 8.3 + + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Install PHP with extensions + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + ini-values: memory_limit=-1, assert.exception=1, zend.assertions=1, error_reporting=-1, log_errors_max_len=0, display_errors=On + + - name: Install dependencies with Composer + run: composer install --prefer-dist --no-ansi --no-interaction --no-progress + + - name: Run PHP-CS-Fixer in Linter Mode + run: composer run-script php-cs-fixer -- --dry-run --show-progress=dots --using-cache=no --verbose + + - name: Run PHPStan + run: composer run-script phpstan + + - name: Run test-suites + run: composer run-script test diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..35d2b99 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,31 @@ +name: Release + +on: + push: + tags: + - "**" + +jobs: + create-release: + name: Create Release + + runs-on: ubuntu-latest + + permissions: + contents: write + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Determine tag + run: echo "RELEASE_TAG=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + + - name: Create release + uses: ncipollo/release-action@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + tag: ${{ env.RELEASE_TAG }} + name: v${{ env.RELEASE_TAG }} + generateReleaseNotes: true + diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml deleted file mode 100644 index 2b8d36c..0000000 --- a/.github/workflows/testing.yml +++ /dev/null @@ -1,42 +0,0 @@ -name: PHP Test - -on: - push: - pull_request: - -permissions: - contents: read - -jobs: - build: - - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: 8.2 - - - name: Validate composer.json and composer.lock - run: composer validate --strict - - - name: Cache Composer packages - id: composer-cache - uses: actions/cache@v3 - with: - path: vendor - key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }} - restore-keys: | - ${{ runner.os }}-php- - - - name: Install dependencies - run: composer install --prefer-dist --no-progress - - - name: Run test suite - run: composer run-script test - - - name: Run PHP-Stan - run: composer run-script phpstan diff --git a/.gitignore b/.gitignore index 8feb9b8..0233d94 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ /.phpunit.cache/ /composer.lock /phpunit.xml +/.php-cs-fixer.cache diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php new file mode 100644 index 0000000..5608d5d --- /dev/null +++ b/.php-cs-fixer.dist.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +$header = << + +For the full copyright and license information, please view the LICENSE +file that was distributed with this source code. +EOF; + +$finder = PhpCsFixer\Finder::create() + ->in(__DIR__ . '/src') + ->in(__DIR__ . '/tests') + ->ignoreVCSIgnored(true); + +return (new PhpCsFixer\Config()) + ->setRules([ + '@PSR12' => true, + '@PSR12:risky' => true + ]) + ->setUsingCache(true) + ->setRiskyAllowed(true) + ->setFinder($finder); diff --git a/composer.json b/composer.json index 4b8fe13..657b990 100644 --- a/composer.json +++ b/composer.json @@ -30,19 +30,21 @@ }, "require": { - "php": ">=8.2", - "symfony/http-client": "6.*", - "doctrine/collections": "2.*" + "php": "^8.2", + "symfony/http-client": "^6.0 || ^7.0", + "doctrine/collections": "^2.0" }, "require-dev": { "phpunit/phpunit": "dev-main", - "phpstan/phpstan": "1.10.x-dev" + "phpstan/phpstan": "1.10.x-dev", + "friendsofphp/php-cs-fixer": "^3.54" }, "scripts": { - "test": "@php vendor/bin/phpunit", - "phpstan": "@php vendor/bin/phpstan analyse src tests" + "test": "vendor/bin/phpunit", + "phpstan": "vendor/bin/phpstan analyse", + "php-cs-fixer": "vendor/bin/php-cs-fixer fix" }, "minimum-stability": "stable" diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon new file mode 100644 index 0000000..aa7ab57 --- /dev/null +++ b/phpstan-baseline.neon @@ -0,0 +1,11 @@ +parameters: + ignoreErrors: + - + message: "#^Method ahmedghanem00\\\\TempNumberClient\\\\Exception\\\\API\\\\APIException\\:\\:newFromErrorName\\(\\) should return static\\(ahmedghanem00\\\\TempNumberClient\\\\Exception\\\\API\\\\APIException\\) but returns ahmedghanem00\\\\TempNumberClient\\\\Exception\\\\API\\\\APIException\\.$#" + count: 1 + path: src/Exception/API/APIException.php + + - + message: "#^Unreachable statement \\- code above always terminates\\.$#" + count: 2 + path: tests/Unit/ClientTest.php diff --git a/phpstan.dist.neon b/phpstan.dist.neon new file mode 100644 index 0000000..38ad328 --- /dev/null +++ b/phpstan.dist.neon @@ -0,0 +1,11 @@ +includes: + - phpstan-baseline.neon + +parameters: + level: 5 + + paths: + - src/ + - tests/ + + reportUnmatchedIgnoredErrors: true diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cbd6e09..49aac05 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,32 +1,37 @@ + + + failOnWarning="true"> - - tests/unit + + tests/Unit - - + + - + - src + src - + diff --git a/src/Client.php b/src/Client.php index 744c155..c5be1e2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * @@ -57,8 +67,7 @@ public function __construct( string $apiKey, ?HttpClientInterface $httpClient = null, TempNumberServer $backendServer = TempNumberServer::Production - ) - { + ) { $this->setHttpClient($httpClient ?? HttpClient::create()); $this->setApiKey($apiKey); $this->setBackendServer($backendServer); @@ -79,7 +88,7 @@ public function setApiKey(#[\SensitiveParameter] string $apiKey): void } /** - * @param array $options + * @param array $options * @return void */ public function applyHttpClientOptions(array $options): void diff --git a/src/Enum/ActivationStatus.php b/src/Enum/ActivationStatus.php index 52d045e..3704da5 100644 --- a/src/Enum/ActivationStatus.php +++ b/src/Enum/ActivationStatus.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Enum/Country.php b/src/Enum/Country.php index 98eeafd..3b3b06b 100644 --- a/src/Enum/Country.php +++ b/src/Enum/Country.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Enum/Service.php b/src/Enum/Service.php index f4ed49d..14c0ccd 100644 --- a/src/Enum/Service.php +++ b/src/Enum/Service.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Enum/TempNumberServer.php b/src/Enum/TempNumberServer.php index ee367f4..dd06112 100644 --- a/src/Enum/TempNumberServer.php +++ b/src/Enum/TempNumberServer.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * @@ -15,4 +25,14 @@ enum TempNumberServer: string case Production = 'https://tn-api.com/api/v1/'; case Mock = 'https://mock.temp-number.org/v1/'; + + public static function fromName(string $name): self + { + foreach (self::cases() as $status) { + if ($name === $status->name) { + return $status; + } + } + throw new \ValueError("$name is not a valid backing value for enum " . self::class); + } } diff --git a/src/Exception/API/APIException.php b/src/Exception/API/APIException.php index 257f7fe..7996339 100644 --- a/src/Exception/API/APIException.php +++ b/src/Exception/API/APIException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * @@ -30,8 +40,7 @@ class APIException extends ClientException */ public function __construct( private ResponseInterface $response - ) - { + ) { parent::__construct(static::Description); } @@ -40,7 +49,7 @@ public function __construct( * @param ResponseInterface $response * @return static */ - public static function newFromErrorName(string $errorName, ResponseInterface $response): self + public static function newFromErrorName(string $errorName, ResponseInterface $response): static { $exceptionClass = match (strtolower($errorName)) { 'unauthorizedexception' => UnauthorizedServiceException::class, diff --git a/src/Exception/API/AccountOnHoldException.php b/src/Exception/API/AccountOnHoldException.php index b4da218..bec5b98 100644 --- a/src/Exception/API/AccountOnHoldException.php +++ b/src/Exception/API/AccountOnHoldException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/ExpectedPriceException.php b/src/Exception/API/ExpectedPriceException.php index 9a691de..e508041 100644 --- a/src/Exception/API/ExpectedPriceException.php +++ b/src/Exception/API/ExpectedPriceException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/GoneException.php b/src/Exception/API/GoneException.php index 005de7e..4c214d9 100644 --- a/src/Exception/API/GoneException.php +++ b/src/Exception/API/GoneException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/InvalidRequestParamsException.php b/src/Exception/API/InvalidRequestParamsException.php index 5a90cf4..71e60f6 100644 --- a/src/Exception/API/InvalidRequestParamsException.php +++ b/src/Exception/API/InvalidRequestParamsException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/LowSuccessRateException.php b/src/Exception/API/LowSuccessRateException.php index c69d872..5d26bbe 100644 --- a/src/Exception/API/LowSuccessRateException.php +++ b/src/Exception/API/LowSuccessRateException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/PaymentRequiredException.php b/src/Exception/API/PaymentRequiredException.php index 74087b5..6f8e2d9 100644 --- a/src/Exception/API/PaymentRequiredException.php +++ b/src/Exception/API/PaymentRequiredException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/ResourceBadStateException.php b/src/Exception/API/ResourceBadStateException.php index 5bfc955..4aced9b 100644 --- a/src/Exception/API/ResourceBadStateException.php +++ b/src/Exception/API/ResourceBadStateException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/ResourceNotFoundException.php b/src/Exception/API/ResourceNotFoundException.php index 8dbb9b5..54b3fb3 100644 --- a/src/Exception/API/ResourceNotFoundException.php +++ b/src/Exception/API/ResourceNotFoundException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/ServiceUnavailableException.php b/src/Exception/API/ServiceUnavailableException.php index fbaa24f..e044910 100644 --- a/src/Exception/API/ServiceUnavailableException.php +++ b/src/Exception/API/ServiceUnavailableException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/TemporaryErrorInterface.php b/src/Exception/API/TemporaryErrorInterface.php index 7b3bebf..8193122 100644 --- a/src/Exception/API/TemporaryErrorInterface.php +++ b/src/Exception/API/TemporaryErrorInterface.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/TooManyActivationsPendingException.php b/src/Exception/API/TooManyActivationsPendingException.php index 1e0a5c1..fc2bfa8 100644 --- a/src/Exception/API/TooManyActivationsPendingException.php +++ b/src/Exception/API/TooManyActivationsPendingException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/TooManyRequestsException.php b/src/Exception/API/TooManyRequestsException.php index a0ae066..2f6fb0c 100644 --- a/src/Exception/API/TooManyRequestsException.php +++ b/src/Exception/API/TooManyRequestsException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/API/UnauthorizedServiceException.php b/src/Exception/API/UnauthorizedServiceException.php index a789c81..70e7fbc 100644 --- a/src/Exception/API/UnauthorizedServiceException.php +++ b/src/Exception/API/UnauthorizedServiceException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Exception/ClientException.php b/src/Exception/ClientException.php index c7055d7..a5d2ba5 100644 --- a/src/Exception/ClientException.php +++ b/src/Exception/ClientException.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Result/AbstractResult.php b/src/Result/AbstractResult.php index 23a631b..a4d002f 100644 --- a/src/Result/AbstractResult.php +++ b/src/Result/AbstractResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * @@ -14,7 +24,6 @@ { public function __construct( protected array $resultData, - ) - { + ) { } } diff --git a/src/Result/ActivationHistoryResult.php b/src/Result/ActivationHistoryResult.php index a3903fa..bb0bca8 100644 --- a/src/Result/ActivationHistoryResult.php +++ b/src/Result/ActivationHistoryResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Result/ActivationResult.php b/src/Result/ActivationResult.php index 50f8f43..89b85aa 100644 --- a/src/Result/ActivationResult.php +++ b/src/Result/ActivationResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Result/CountryResult.php b/src/Result/CountryResult.php index aa3e780..46a04f5 100644 --- a/src/Result/CountryResult.php +++ b/src/Result/CountryResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Result/CountryServiceInfoResult.php b/src/Result/CountryServiceInfoResult.php index a7aacdb..d3fea53 100644 --- a/src/Result/CountryServiceInfoResult.php +++ b/src/Result/CountryServiceInfoResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/src/Result/ServiceResult.php b/src/Result/ServiceResult.php index a1ac4dc..2149f1a 100644 --- a/src/Result/ServiceResult.php +++ b/src/Result/ServiceResult.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * diff --git a/tests/unit/ClientTest.php b/tests/Unit/ClientTest.php similarity index 94% rename from tests/unit/ClientTest.php rename to tests/Unit/ClientTest.php index 2f70969..aee6f67 100644 --- a/tests/unit/ClientTest.php +++ b/tests/Unit/ClientTest.php @@ -1,4 +1,14 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); /* * This file is part of the TempNumberClient package. * @@ -15,6 +25,7 @@ use ahmedghanem00\TempNumberClient\Enum\Service; use ahmedghanem00\TempNumberClient\Enum\TempNumberServer; use Exception; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\TestCase; use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface; use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface; @@ -22,9 +33,7 @@ use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface; use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface; -/** - * @covers Client - */ +#[CoversClass(Client::class)] class ClientTest extends TestCase { /** @@ -33,8 +42,6 @@ class ClientTest extends TestCase private Client $client; /** - * @covers Client::retrieveBalance - * * @throws TransportExceptionInterface * @throws ServerExceptionInterface * @throws RedirectionExceptionInterface @@ -99,12 +106,8 @@ public function testRequestNewActivation() */ public function testRetryActivation() { - try { - $this->client->retryActivation(7378322); - $this->assertTrue(true); - } catch (Exception $e) { - throw $e; - } + $this->client->retryActivation(7378322); + $this->assertTrue(true); } /** @@ -217,14 +220,9 @@ public function testRetrievePriceListByCountry() } } - /** - * @return void - */ protected function setUp(): void { - $backendServer = current(array_filter(TempNumberServer::cases(), function ($case) { - return $case->name === getenv('BACKEND_SERVER'); - })); + $backendServer = TempNumberServer::fromName(getenv('BACKEND_SERVER')); $this->client = new Client(getenv('API_KEY'), null, $backendServer);