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

Feature/modernize #21

Open
wants to merge 6 commits into
base: master
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
37 changes: 37 additions & 0 deletions .github/workflows/code_checks.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# .github/workflows/code_checks.yaml
name: Code_Checks

on: ["push", "pull_request"]

jobs:
tests:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: ['7.2', '7.3', '7.4', '8.0', '8.1']
stability: [ prefer-lowest, prefer-stable ]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} tests
steps:
# basically git clone
- uses: actions/checkout@v2

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}

# use PHP of specific version
- uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: pcov
coverage: pcov

- name: Install dependencies
run: composer update --${{ matrix.stability }} --prefer-dist --no-interaction

- name: Execute tests
run: vendor/bin/phpunit --verbose
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
vendor/
var/
composer.lock
Tests/Functional/app/cache
.idea
.phpunit.result.cache
.github/
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion DependencyInjection/DdeboerVatinExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
* To learn more see {@link https://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class DdeboerVatinExtension extends Extension
{
Expand Down
30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ $ composer require ddeboer/vatin-bundle

Then add the bundle to your application:

If you don't use [Symfony Flex](https://symfony.com/doc/current/setup/flex.html), you must enable the bundle manually in the application:

```php
// app/AppKernel.php
public function registerBundles()
{
return [
...
new Ddeboer\VatinBundle\DdeboerVatinBundle(),
...
];
}
// config/bundles.php
// in older Symfony apps, enable the bundle in app/AppKernel.php
return [
// ...
Ddeboer\VatinBundle\DdeboerVatinBundle::class => ['all' => true],
];
```


Usage
-----

Expand All @@ -48,7 +48,7 @@ class Company
```

Symfony’s validator will now check whether `$vatNumber` has a valid VAT number
format. For more information, see [Symfony’s documentation](http://symfony.com/doc/current/book/validation.html).
format. For more information, see [Symfony’s documentation](https://symfony.com/doc/current/book/validation.html).

### Validate number existence

Expand All @@ -61,8 +61,16 @@ Additionally, you can check whether the VAT number is in use:
protected $vatNumber;
```

```php
/**
* PHP 8 Attribute
*/
#[Vatin(checkExistence: true)]
protected $vatNumber;
```

The validator will now check the VAT number against the
[VAT Information Exchange System (VIES)](http://ec.europa.eu/taxation_customs/vies/faq.html)
[VAT Information Exchange System (VIES)](https://ec.europa.eu/taxation_customs/vies/faq.html)
SOAP web service. This service’s availability is rather unreliable, so it’s a
good idea to catch the case where it’s unreachable:

Expand Down
25 changes: 21 additions & 4 deletions Tests/Functional/ValidatorAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,49 @@ class ValidatorAnnotationTest extends WebTestCase
*/
private $validator;

protected function setUp()
protected function setUp(): void
{
static::bootKernel();
$container = static::$kernel->getContainer();
$this->validator = $container->get('validator');
}

/**
* @requires PHP 8.0
*/
public function testValid()
{
$model = new Model();
$errors = $this->validator->validate($model);
$this->assertEquals(0, count($errors));
$this->assertCount(0, $errors);

$model->vat = 'NL123456789B01';
$this->assertCount(0, $this->validator->validate($model));
}

/**
* @requires PHP 8.0
*/
public function testCheckExistence()
{
$model = new Model();
# invalid value
$model->vatCheckExistence = '123';
$this->assertCount(1, $this->validator->validate($model));
try {
$this->assertCount(0, $this->validator->validate($model));
} catch (ValidatorException $e) {
if (!$e->getPrevious() instanceof ViesException) {
throw $e;
}

// Ignore unreachable VIES service: at least the check was triggered
}


# valid value
$model->vatCheckExistence = 'NL123456789B01';
try {
$this->assertCount(1, $this->validator->validate($model));
$this->assertCount(0, $this->validator->validate($model));
} catch (ValidatorException $e) {
if (!$e->getPrevious() instanceof ViesException) {
throw $e;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Functional/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class AppKernel extends Kernel
{
public function registerBundles()
public function registerBundles(): array
{
return [
new \Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
Expand Down
9 changes: 1 addition & 8 deletions Tests/Functional/app/autoload.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/** @var ClassLoader $loader */
$loader = require __DIR__.'/../../../vendor/autoload.php';
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
require dirname(__DIR__).'/../../vendor/autoload.php';

require_once __DIR__.'/AppKernel.php';

return $loader;
7 changes: 1 addition & 6 deletions Tests/Validator/Constraints/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
use Ddeboer\Vatin\Validator;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

// BC for Symfony 2 and Symfony 4. To be removed when support for Symfony 2 is dropped
if (!class_exists('Symfony\Component\Validator\Test\ConstraintValidatorTestCase')) {
class_alias('Symfony\Component\Validator\Tests\Constraints\AbstractConstraintValidatorTest', 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase');
}

class ValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator()
protected function createValidator(): VatinValidator
{
return new VatinValidator(new Validator());
}
Expand Down
2 changes: 1 addition & 1 deletion Validator/Constraints/Vatin.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function __construct(
parent::__construct($options ?? [], $groups, $payload);
}

public function validatedBy()
public function validatedBy(): string
{
return 'ddeboer_vatin.validator';
}
Expand Down
28 changes: 14 additions & 14 deletions Validator/Constraints/VatinValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

/**
* Validate a VAT identification number using the ddeboer/vatin library
*
*/
class VatinValidator extends ConstraintValidator
{
Expand All @@ -23,8 +22,6 @@ class VatinValidator extends ConstraintValidator

/**
* Constructor
*
* @param Validator $validator VATIN validator
*/
public function __construct(Validator $validator)
{
Expand All @@ -40,23 +37,26 @@ public function validate($value, Constraint $constraint)
return;
}

if ($this->isValidVatin($value, $constraint->checkExistence)) {
return;
}
try {
#var_dump('Check '.$value. ' '.$constraint->checkExistence);
if ($this->isValidVatin($value, $constraint->checkExistence)) {
return;
}

$this->context->buildViolation($constraint->message)
->addViolation();
$this->context->buildViolation($constraint->message)->addViolation();
}
catch (\Exception $e)
{
#var_dump($e->getMessage());
#var_dump("error build violation for ".$value);
$this->context->buildViolation($constraint->message)->addViolation();
}
}

/**
* Is the value a valid VAT identification number?
*
* @param string $value Value
* @param bool $checkExistence Also check whether the VAT number exists
*
* @return bool
*/
private function isValidVatin($value, $checkExistence)
private function isValidVatin(string $value, bool $checkExistence): bool
{
try {
return $this->validator->isValid($value, $checkExistence);
Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,18 @@
}
],
"require": {
"ddeboer/vatin": "^2.0",
"symfony/framework-bundle": "^3.4.26 || ^4.1.12 || ^5.0",
"symfony/validator": "^3.0 || ^4.0 || ^5.0"
"php": "^7.2 || ^8.0",
"ddeboer/vatin": "^2.2.2",
"symfony/framework-bundle": "^4.4.31 || ^5.3",
"symfony/validator": "^4.4.31 || ^5.3"
},
"require-dev": {
"phpunit/phpunit": "^4.0 || ^5.0",
"doctrine/annotations": "^1.2",
"symfony/yaml": "^2.7 || ^3.0 || ^4.0 || ^5.0"
"phpunit/phpunit": "^8.5.21 || ^9.5",
"doctrine/annotations": "^1.13.2",
"symfony/yaml": "^4.4.31 || ^5.3",
"symfony/dependency-injection": "^4.4.31 || ^5.3",
"symfony/config": "^4.4.31 || ^5.3",
"symfony/error-handler": "^4.4.31 || ^5.3"
},
"autoload": {
"psr-4": {
Expand Down
52 changes: 26 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./Tests/Functional/app/autoload.php"
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./Tests/Functional/app/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
syntaxCheck="true">
<testsuites>
<testsuite name="ddeboer/vatin-bundle test suite">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>

<php>
<server name="KERNEL_DIR" value="./Tests/Functional/app"/>
<server name="KERNEL_CLASS" value="AppKernel"/>
<ini name="default_socket_timeout" value="3"/>
</php>
</phpunit>
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">./</directory>
</include>
<exclude>
<directory>./github</directory>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./var</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="ddeboer/vatin-bundle test suite">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<php>
<server name="KERNEL_DIR" value="./Tests/Functional/app"/>
<server name="KERNEL_CLASS" value="AppKernel"/>
<ini name="default_socket_timeout" value="3"/>
</php>
</phpunit>