Skip to content

Commit

Permalink
Merge pull request #13 from lsnepomuceno/dev
Browse files Browse the repository at this point in the history
Feature: Adds support for laravel 11 and php 8.3
  • Loading branch information
lsnepomuceno authored Mar 25, 2024
2 parents ac55d30 + 6e586a5 commit 07ab898
Show file tree
Hide file tree
Showing 21 changed files with 602 additions and 305 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,25 @@ permissions:

jobs:
run:
name: Run
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '8.1', '8.2', '8.3' ]
laravel: [ '10.*','9.*' ]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
env:
extensions: mbstring, dom, fileinfo, openssl, json, imagick, swoole, sqlite3
key: cache-v5
name: PHP ${{ matrix.php-versions }} - Laravel ${{ matrix.laravel }} - ${{ matrix.operating-system}}
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Setup cache environment
id: cache-env
Expand All @@ -34,7 +40,7 @@ jobs:
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
Expand All @@ -47,6 +53,11 @@ jobs:
env:
update: true

- name: Install Laravel Dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --prefer-dist --no-interaction --no-suggest
- name: Testing PHP version
run: |
php -v
Expand All @@ -63,7 +74,7 @@ jobs:
- name: Install dependencies
run: |
composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
composer update -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests
run: |
Expand Down
79 changes: 79 additions & 0 deletions .github/workflows/action_laravel_11.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Laravel Brazilian Ceps

on:
push:
branches: [ main, dev ]
pull_request:
branches: [ main, dev ]

permissions:
contents: read

jobs:
run:
runs-on: ${{ matrix.operating-system }}
strategy:
fail-fast: false
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '8.2', '8.3' ]
laravel: [ '11.*' ]
include:
- laravel: 11.*
testbench: 9.*
env:
extensions: mbstring, dom, fileinfo, openssl, json, imagick, swoole, sqlite3, sqlite
key: cache-v5
name: PHP ${{ matrix.php-versions }} - Laravel ${{ matrix.laravel }} - ${{ matrix.operating-system}}
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup cache environment
id: cache-env
uses: shivammathur/cache-extensions@develop
with:
php-version: ${{ matrix.php-versions }}
extensions: ${{ env.extensions }}
key: ${{ env.key }}

- name: Cache extensions
uses: actions/cache@v4
with:
path: ${{ steps.cache-env.outputs.dir }}
key: ${{ steps.cache-env.outputs.key }}
restore-keys: ${{ steps.cache-env.outputs.key }}

- name: Setup PHP with latest versions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
env:
update: true

- name: Install Laravel Dependencies
run: |
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
composer update --prefer-dist --no-interaction --no-suggest
- name: Testing PHP version
run: |
php -v
php -r "if(strpos(phpversion(), '${{ matrix.php-versions }}') === false) {throw new Exception('Wrong PHP version Installed');}"
- name: Testing Composer version
run: |
composer -V
php -r "if(strpos(@exec('composer -V'), 'Composer version') === false) {throw new Exception('Composer not found');}"
- name: Validate composer.json and composer.lock
run: |
composer validate
- name: Install dependencies
run: |
composer update -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
- name: Execute tests
run: |
vendor/bin/testbench package:test
10 changes: 5 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"ext-json": "*",
"ext-fileinfo": "*",
"ext-mbstring": "*",
"illuminate/support": "^9 || ^10",
"illuminate/http": "^9 || ^10",
"guzzlehttp/guzzle": "^7.5"
"illuminate/support": "^9 || ^10 || ^11",
"illuminate/http": "^9 || ^10 || ^11",
"guzzlehttp/guzzle": "^7"
},
"autoload": {
"psr-4": {
Expand All @@ -48,7 +48,7 @@
"lsnepomuceno/laravel-a1-pdf-sign": "Sign PDF files with valid x509 certificates."
},
"require-dev": {
"orchestra/testbench": "^8",
"nunomaduro/collision": "^7"
"orchestra/testbench": "^7 || ^8 || ^9",
"nunomaduro/collision": "^6 || ^7 || ^8"
}
}
5 changes: 5 additions & 0 deletions src/CepProviders/ApiCep.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
7 changes: 6 additions & 1 deletion src/CepProviders/BrasilApiV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
public function get(string $cep): ?CepEntity
{
try {
$data = $this->client->get("{$this->formatCep($cep)}.json")
$data = $this->client->get($this->formatCep($cep))
->object();

$this->setOriginalProviderResponse($data);
Expand All @@ -48,4 +48,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
7 changes: 6 additions & 1 deletion src/CepProviders/BrasilApiV2.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct()
public function get(string $cep): ?CepEntity
{
try {
$data = $this->client->get("{$this->formatCep($cep)}.json")
$data = $this->client->get($this->formatCep($cep))
->object();

$this->setOriginalProviderResponse($data);
Expand All @@ -48,4 +48,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
5 changes: 5 additions & 0 deletions src/CepProviders/OpenCep.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
5 changes: 5 additions & 0 deletions src/CepProviders/Pagarme.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
5 changes: 5 additions & 0 deletions src/CepProviders/Postomon.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
5 changes: 5 additions & 0 deletions src/CepProviders/ViaCep.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,9 @@ public function get(string $cep): ?CepEntity
return null;
}
}

public function getBaseUrl(): string
{
return self::BASE_URL;
}
}
2 changes: 2 additions & 0 deletions src/Contracts/ConsultableCEPProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
interface ConsultableCEPProvider
{
public function get(string $cep): ?CepEntity;

public function getBaseUrl(): string;
}
101 changes: 55 additions & 46 deletions tests/Feature/ApiCepProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,95 @@
namespace LSNepomuceno\LaravelBrazilianCeps\Tests\Feature;

use Exception;
use Illuminate\Support\Facades\Http;
use LSNepomuceno\LaravelBrazilianCeps\CepProviders\ApiCep;
use LSNepomuceno\LaravelBrazilianCeps\Entities\CepEntity;
use LSNepomuceno\LaravelBrazilianCeps\Tests\Helpers\DefaultValues;
use LSNepomuceno\LaravelBrazilianCeps\Tests\TestCase;
use LSNepomuceno\LaravelBrazilianCeps\Tests\HttpTestCase;

class ApiCepProviderTest extends TestCase
class ApiCepProviderTest extends HttpTestCase
{
protected ApiCep $cepProvider;

protected function setUp(): void
{
parent::setUp();
$this->cepProvider = new ApiCep();
}

public function testValidatesCepProviderName()
{
$apiCepProvider = new ApiCep();
$this->assertEquals('ApiCep', $apiCepProvider->getProviderName());
$cepProvider = new ApiCep();
$this->assertEquals('ApiCep', $cepProvider->getProviderName());
}

private function mockResponseSuccess(): void
{
$mockResponse = [
'city' => $this->faker->city(),
'code' => $this->faker->postcode(),
'address' => $this->faker->streetName(),
'state' => $this->faker->stateAbbr(),
'district' => $this->faker->name()
];

Http::fake([
"{$this->cepProvider->getBaseUrl()}*" => Http::response($mockResponse, 200)
]);
}

/**
* @throws Exception
*/
public function testValidatesOriginalResponseStructure()
{
$cep = '29018-210';
$apiCepProvider = new ApiCep();
$apiCepProvider->get($cep);

$originalProviderResponse = $apiCepProvider->getOriginalProviderResponse();

$requiredFields = [
'code',
'state',
'city',
'district',
'address'
];
$this->mockResponseSuccess();

foreach ($requiredFields as $field) {
$this->assertNotEmpty($field);
$this->assertArrayHasKey($field, (array) $originalProviderResponse);
}
$cepProvider = new ApiCep();
$response = $cepProvider->get($this->faker->postcode());
$originalProviderResponse = $cepProvider->getOriginalProviderResponse();
$this->assertInstanceOf(CepEntity::class, $response);
$this->assertNotEmpty($originalProviderResponse);
}

/**
* @throws Exception
* @depends testValidatesOriginalResponseStructure
*/
public function testValidatesIfTheRequestWillBeExecutedSuccessfully()
{
$cep = '29018-210';
$apiCepProvider = new ApiCep();
$response = $apiCepProvider->get($cep);

if (!$response) {
$this->markTestSkipped(
'APICep provider unavailable.'
);
}
$this->mockResponseSuccess();

$requiredFields = DefaultValues::successfullyRequiredFields();
$optionalFields = DefaultValues::optionalFields();
$cepProvider = new ApiCep();
$response = $cepProvider->get($this->faker->postcode());

$this->assertIsObject($response);

$this->assertInstanceOf(CepEntity::class, $response);
$this->assertRequiredFields($response);
$this->assertOptionalFields($response);
}

/**
* @throws Exception
*/
public function testValidatesOriginalResponseStructureError()
{
$this->mockErrorResponse($this->cepProvider->getBaseUrl());
$cepProvider = new ApiCep();

foreach ($requiredFields as $field) {
$this->assertNotEmpty($field);
$this->assertArrayHasKey($field, (array) $response);
}
$response = $cepProvider->get('12345678');
$originalProviderResponse = $cepProvider->getOriginalProviderResponse();

foreach ($optionalFields as $field) {
$this->isNull($field);
$this->assertArrayHasKey($field, (array) $response);
}
$this->assertNull($response);
$this->assertNull($originalProviderResponse);
}

/**
* @throws Exception
*/
public function testValidatesWhenAnInvalidZipCepIsReceived()
{
$cep = '66666666';
$apiCepProvider = new ApiCep();
$response = $apiCepProvider->get($cep);
$cep = '66666666';
$cepProvider = new ApiCep();
$response = $cepProvider->get($cep);

$this->assertNull($response);
}
Expand Down
Loading

0 comments on commit 07ab898

Please sign in to comment.