Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 - New version #15

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ jobs:
matrix:
operating-system: [ ubuntu-latest ]
php-versions: [ '8.1', '8.2', '8.3' ]
laravel: [ '10.*','9.*' ]
laravel: [ '10.*' ]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
env:
extensions: mbstring, dom, fileinfo, openssl, json, imagick, swoole, sqlite3
key: cache-v5
Expand Down Expand Up @@ -78,4 +76,4 @@ jobs:

- name: Execute tests
run: |
vendor/bin/testbench package:test
composer run test
2 changes: 1 addition & 1 deletion .github/workflows/action_laravel_11.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,4 @@ jobs:

- name: Execute tests
run: |
vendor/bin/testbench package:test
composer run test
16 changes: 11 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"ext-json": "*",
"ext-fileinfo": "*",
"ext-mbstring": "*",
"illuminate/support": "^9 || ^10 || ^11",
"illuminate/http": "^9 || ^10 || ^11",
"illuminate/support": "^10 || ^11",
"illuminate/http": "^10 || ^11",
"guzzlehttp/guzzle": "^7"
},
"autoload": {
Expand All @@ -35,7 +35,7 @@
}
},
"scripts": {
"test": "vendor/bin/testbench package:test"
"test": "vendor/bin/pest --parallel --processes=10"
},
"extra": {
"laravel": {
Expand All @@ -48,7 +48,13 @@
"lsnepomuceno/laravel-a1-pdf-sign": "Sign PDF files with valid x509 certificates."
},
"require-dev": {
"orchestra/testbench": "^7 || ^8 || ^9",
"nunomaduro/collision": "^6 || ^7 || ^8"
"orchestra/testbench": "^8 || ^9",
"nunomaduro/collision": "^7 || ^8",
"pestphp/pest": "^2.35"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

# Minimum requirements
* PHP: ^8.1, ^8.2 or ^8.3
* Laravel: 9, 10 or 11
* Laravel: 10 or 11
* PHP Extensions: fileinfo, mbstring, json

# Install
Expand Down
62 changes: 62 additions & 0 deletions tests/Pest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
|--------------------------------------------------------------------------
| Test Case
|--------------------------------------------------------------------------
|
| The closure you provide to your test functions is always bound to a specific PHPUnit test
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may
| need to change it using the "uses()" function to bind a different classes or traits.
|
*/

use LSNepomuceno\LaravelBrazilianCeps\Tests\TestCase;

uses(TestCase::class)->in(__DIR__);

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

expect()->extend('toBeType', function (string $type) {
$actual = $this->value;

return match ($type) {
'int', 'integer' => expect($actual)->toBeInt(),
'bool', 'boolean' => expect($actual)->toBeBool(),
'string' => expect($actual)->toBeString(),
'array' => expect($actual)->toBeArray(),
'float', 'double' => expect($actual)->toBeFloat(),
'null' => expect($actual)->toBeNull(),
'numeric' => expect(is_numeric($actual))->toBeTrue(),
default => throw new InvalidArgumentException("Type not supported: {$type}"),
};
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected function setUpFaker(): void

protected function defineEnvironment($app): void
{
$app['config']->set('app.env', 'testing');
$app['config']->set('database.default', 'testing');
}

Expand Down
74 changes: 24 additions & 50 deletions tests/Unit/CepEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,58 +3,32 @@
namespace LSNepomuceno\LaravelBrazilianCeps\Tests\Unit;

use LSNepomuceno\LaravelBrazilianCeps\Entities\CepEntity;
use LSNepomuceno\LaravelBrazilianCeps\Tests\TestCase;

class CepEntityTest extends TestCase
{
public function testValidatesCepEntityStructure()
{
test(
'validates cep entity structure',
function ($city, $cep, $street, $state, $uf, $neighborhood, $number, $complement, $ibge) {
$cepEntity = new CepEntity(
city : 'city',
cep : 'cep',
street : 'street',
state : 'state',
uf : 'uf',
neighborhood: 'neighborhood',
number : 'number',
complement : 'complement',
ibge : 'ibge'
city: $city,
cep: $cep,
street: $street,
state: $state,
uf: $uf,
neighborhood: $neighborhood,
number: $number,
complement: $complement,
ibge: $ibge
);

$this->assertIsArray($cepEntity->toArray());
expect($cepEntity->toArray())->toBeArray();
}

public function testValidatesCepEntityStructureWithIntegerNumber()
{
$cepEntity = new CepEntity(
city : 'city',
cep : 'cep',
street : 'street',
state : 'state',
uf : 'uf',
neighborhood: 'neighborhood',
number : 1000,
complement : 'complement',
ibge : 'ibge'
);

$this->assertIsArray($cepEntity->toArray());
}

public function testValidatesCepEntityStructureWhenComplementAndNumberAreNull()
{
$cepEntity = new CepEntity(
city : 'city',
cep : 'cep',
street : 'street',
state : 'state',
uf : 'uf',
neighborhood: 'neighborhood',
number : null,
complement : null,
ibge : null
);

$this->assertIsArray($cepEntity->toArray());
}
}
)->with([
'default cep entity structure' => [
'city', 'cep', 'street', 'state', 'uf', 'neighborhood', 'number', 'complement', 'ibge'
],
'cep entity structure with integer number' => [
'city', 'cep', 'street', 'state', 'uf', 'neighborhood', 1000, 'complement', 'ibge'
],
'cep entity structure when complement, number and ibge are null' => [
'city', 'cep', 'street', 'state', 'uf', 'neighborhood', null, null, null
],
]);
24 changes: 9 additions & 15 deletions tests/Unit/CepNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

namespace LSNepomuceno\LaravelBrazilianCeps\Tests\Unit;

use Illuminate\Support\Facades\Http;
use LSNepomuceno\LaravelBrazilianCeps\Exceptions\CepNotFoundException;
use LSNepomuceno\LaravelBrazilianCeps\Services\CepService;
use LSNepomuceno\LaravelBrazilianCeps\Tests\TestCase;

class CepNotFoundExceptionTest extends TestCase
{
/**
* @throws CepNotFoundException
*/
public function testValidateCepNotFoundException()
{
$this->expectException(CepNotFoundException::class);

config(['brazilian-ceps.throw_not_found_exception' => true]);
test('valida exceção CepNotFoundException', function () {
Http::fake([
"*" => Http::response([], 404)
]);

$cepService = new CepService();
config(['brazilian-ceps.throw_not_found_exception' => true]);

$cepService->get('66666666');
}
}
expect(fn() => (new CepService())->get('66666666'))
->toThrow(CepNotFoundException::class);
});
86 changes: 26 additions & 60 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,34 @@

namespace LSNepomuceno\LaravelBrazilianCeps\Tests\Unit;

use LSNepomuceno\LaravelBrazilianCeps\Tests\TestCase;

class ConfigTest extends TestCase
{
public function testValidatesIfTheValuesAreInTheConfigFile()
{
test(
'the defined settings must follow the initial pattern',
function ($key, $expectedType, $expectedValue = null) {
$configValues = config('brazilian-ceps');

$this->assertIsArray($configValues);

$this->assertArrayHasKey('cache_results', $configValues);
$this->assertIsBool($configValues['cache_results']);

$this->assertArrayHasKey('cache_lifetime_in_days', $configValues);
$this->assertIsNumeric($configValues['cache_lifetime_in_days']);

$this->assertArrayHasKey('throw_not_found_exception', $configValues);
$this->assertIsBool($configValues['throw_not_found_exception']);

$this->assertArrayHasKey('enable_api_consult_cep_route', $configValues);
$this->assertIsBool($configValues['enable_api_consult_cep_route']);

$this->assertArrayHasKey('api_route_middleware', $configValues);
$this->assertIsArray($configValues['api_route_middleware']);
expect($configValues)->toBeArray()
->and($configValues)->toHaveKey($key);

$this->assertArrayHasKey('not_found_message', $configValues);
$this->assertIsString($configValues['not_found_message']);
}

public function testValidatesIfTrueAsDesfaultCacheResultValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals(true, $configValues['cache_results']);
}
if ($expectedType) {
expect($configValues[$key])->toBeType($expectedType);
}

public function testValidatesIf_30AsDesfaultCacheLifetimeInDaysValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals(30, $configValues['cache_lifetime_in_days']);
}

public function testValidatesIfFalseAsDesfaultThrowNotFoundExceptionValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals(false, $configValues['throw_not_found_exception']);
}

public function testValidatesIfTrueAsDesfaultEnableApiConsultCepRouteValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals(true, $configValues['enable_api_consult_cep_route']);
}

public function testValidatesIfGuestADefaultApiRouteMiddlewareValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals(['guest'], $configValues['api_route_middleware']);
}

public function testValidatesIfCepNaoEncontradoAsDesfaultNotFoundMessageValue()
{
$configValues = config('brazilian-ceps');
$this->assertEquals('CEP não encontrado.', $configValues['not_found_message']);
if (!is_null($expectedValue)) {
expect($configValues[$key])->toBe($expectedValue);
}
}
}
)->with([
'cache_results é booleano' => ['cache_results', 'bool'],
'cache_lifetime_in_days é numérico' => ['cache_lifetime_in_days', 'numeric'],
'throw_not_found_exception é booleano' => ['throw_not_found_exception', 'bool'],
'enable_api_consult_cep_route é booleano' => ['enable_api_consult_cep_route', 'bool'],
'api_route_middleware é array' => ['api_route_middleware', 'array'],
'not_found_message é string' => ['not_found_message', 'string'],

'cache_results valor padrão é true' => ['cache_results', 'bool', true],
'cache_lifetime_in_days valor padrão é 30' => ['cache_lifetime_in_days', 'numeric', 30],
'throw_not_found_exception valor padrão é false' => ['throw_not_found_exception', 'bool', false],
'enable_api_consult_cep_route valor padrão é true' => ['enable_api_consult_cep_route', 'bool', true],
'api_route_middleware valor padrão é guest' => ['api_route_middleware', 'array', ['guest']],
'not_found_message valor padrão é CEP não encontrado' => ['not_found_message', 'string', 'CEP não encontrado.'],
]);
Loading