Skip to content

Commit

Permalink
ARES country "upgrade" (#244)
Browse files Browse the repository at this point in the history
I don't understand why but for this 'res' source, the country code is returned as "1", yes a string, instead of "cz", so let's deal with that. This wasn't happening when I've build the support in #225.

It could be easily removed in the future, and there's a test for it (the one for the company with id 00256081), so if it is removed and it shouldn't be, I should know about it rather soon.

Fix #242

The test querying the service is marked as "skipped" because it uses the Internet to actually query the live service and I don't want to query it when I'm running my tests in dev env for example. But I'd like to run the tests someday, so I've introduced a way to run all skipped tests, just define an environment variable `TEST_CASE_RUNNER_INCLUDE_SKIPPED=1` and run the test case again. Will also do it automatically with GitHub Actions.
  • Loading branch information
spaze authored Oct 16, 2023
2 parents dc7fbf3 + 522660c commit 8b77e23
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 14 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/php-tester-include-skipped.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: PHP tests, including skipped tests

on:
schedule:
- cron: '45 23 * * 4'
workflow_dispatch:

jobs:
tester-include-skipped:
runs-on: ubuntu-latest
strategy:
matrix:
php-version:
- "8.2"
- "8.3"
steps:
- uses: actions/checkout@v4
- uses: shivammathur/setup-php@v2
with:
coverage: pcov
php-version: ${{ matrix.php-version }}
- name: Create symlink in /srv/www
run: |
sudo mkdir --parents /srv/www
sudo ln --symbolic $GITHUB_WORKSPACE /srv/www
- run: make --directory=site tester-include-skipped
- name: Failed test output, if any
if: failure()
run: for i in $(find ./site/tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
- name: Upload test code coverage
uses: actions/upload-artifact@v3
if: success()
with:
name: Test code coverage (PHP ${{ matrix.php-version }})
path: 'site/temp/coverage.html'
retention-days: 5
8 changes: 6 additions & 2 deletions site/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: test audit cs-fix check-file-patterns check-makefile lint-php lint-latte lint-neon lint-xml lint-xml-auto-install phpcs phpstan phpstan-latte-templates phpstan-vendor psalm tester
.PHONY: test audit cs-fix check-file-patterns check-makefile lint-php lint-latte lint-neon lint-xml lint-xml-auto-install phpcs phpstan phpstan-latte-templates phpstan-vendor psalm tester tester-include-skipped

test: audit check-file-patterns check-makefile lint-php lint-latte lint-neon lint-xml phpcs phpstan tester psalm phpstan-vendor

Expand Down Expand Up @@ -45,4 +45,8 @@ psalm:
vendor/bin/psalm.phar

tester:
vendor/nette/tester/src/tester -c tests/php-unix.ini --colors 1 --coverage temp/coverage.html --coverage-src app/ tests/
vendor/nette/tester/src/tester -s -c tests/php-unix.ini --colors 1 --coverage temp/coverage.html --coverage-src app/ tests/

tester-include-skipped:
TEST_CASE_RUNNER_INCLUDE_SKIPPED=1 \
$(MAKE) tester
21 changes: 19 additions & 2 deletions site/app/CompanyInfo/CompanyRegisterAres.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ public function getDetails(string $companyId): CompanyInfoDetails
'psc' => Expect::int(),
'kodStatu' => Expect::string(),
])->otherItems(),
'primarniZdroj' => Expect::string(),
])->otherItems();
/** @var object{ico:string, dic:string|null, obchodniJmeno:string, sidlo:object{nazevObce:string, nazevUlice:string, cisloDomovni:int, cisloOrientacni:int, cisloOrientacniPismeno:string, psc:int, kodStatu:string}} $data */
/** @var object{ico:string, dic:string|null, obchodniJmeno:string, sidlo:object{nazevObce:string, nazevUlice:string, cisloDomovni:int, cisloOrientacni:int, cisloOrientacniPismeno:string, psc:int, kodStatu:string}, primarniZdroj:string|null} $data */
$data = $this->schemaProcessor->process($schema, Json::decode($content));
} catch (JsonException | ValidationException $e) {
throw new CompanyInfoException($e->getMessage(), previous: $e);
Expand All @@ -84,7 +85,7 @@ public function getDetails(string $companyId): CompanyInfoDetails
$streetAndNumber,
$data->sidlo->nazevObce,
(string)$data->sidlo->psc,
strtolower($data->sidlo->kodStatu),
$this->resolveCountryCode($data->primarniZdroj, $data->sidlo->kodStatu, $companyId),
);
}

Expand Down Expand Up @@ -122,4 +123,20 @@ private function formatStreet(?string $city, ?string $street, ?int $houseNumber,
return $result;
}


/**
* @throws CompanyInfoException
*/
private function resolveCountryCode(?string $source, string $code, string $companyId): string
{
if ($source === 'res') {
if ($code === '1') {
return 'cz';
} else {
throw new CompanyInfoException("Invalid country code {$code} from source {$source} for company {$companyId}");
}
}
return strtolower($code);
}

}
15 changes: 15 additions & 0 deletions site/app/Test/TestCaseRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@
use Nette\Utils\Type;
use ReflectionException;
use ReflectionMethod;
use Tester\Environment;
use Tester\TestCase;

class TestCaseRunner
{

private const ENV_VAR_NAME = 'TEST_CASE_RUNNER_INCLUDE_SKIPPED';
private const ENV_VAR_VALUE = '1';
public const ENV_VAR = self::ENV_VAR_NAME . '=' . self::ENV_VAR_VALUE;


/**
* @param class-string<TestCase> $test
* @return void
Expand Down Expand Up @@ -50,4 +56,13 @@ public static function run(string $test): void
(new $test(...$params))->run();
}


public static function skip(string $message): void
{
if (getenv(self::ENV_VAR_NAME) === self::ENV_VAR_VALUE) {
return;
}
Environment::skip($message);
}

}
6 changes: 6 additions & 0 deletions site/disallowed-calls.neon
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ parameters:
-
function: 'setcookie()'
message: 'use methods from MichalSpacekCz\Http\Cookies'
disallowedStaticCalls:
-
method: 'Tester\Environment::skip()'
message: 'use TestCaseRunner::skip() instead, it can ignore skipping with an environment variable'
allowInMethods:
- 'MichalSpacekCz\Test\TestCaseRunner::skip()'
disallowedMethodCalls:
-
method:
Expand Down
5 changes: 5 additions & 0 deletions site/phpstan-vendor.neon
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,11 @@ parameters:
message: 'use logger instead, debug bar is not visible in production'
allowIn:
- vendor/tracy/tracy/src/Tracy/functions.php
-
method: 'Tester\Environment::skip()'
message: 'use TestCaseRunner::skip() instead, it can ignore skipping with an environment variable'
allowIn:
- vendor/nette/tester/src/Framework/TestCase.php
disallowedSuperglobals:
-
superglobal: '$_SERVER'
Expand Down
6 changes: 1 addition & 5 deletions site/tests/CompanyInfo/CompanyRegisterAresTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use MichalSpacekCz\Test\Http\Client\HttpClientMock;
use MichalSpacekCz\Test\TestCaseRunner;
use Nette\Schema\Processor;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -32,10 +31,7 @@ class CompanyRegisterAresTest extends TestCase

public function testGetDetails(): void
{
if (getenv(Environment::VariableRunner)) {
$file = basename(__FILE__);
Environment::skip("The test uses the Internet, to not skip the test run it with `php {$file}`");
}
TestCaseRunner::skip('The test uses the Internet, to not skip the test case run it with `' . TestCaseRunner::ENV_VAR . '`');
$expected = new CompanyInfoDetails(
200,
'OK',
Expand Down
6 changes: 1 addition & 5 deletions site/tests/CompanyInfo/CompanyRegisterRegisterUzTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use MichalSpacekCz\CompanyInfo\Exceptions\CompanyNotFoundException;
use MichalSpacekCz\Http\Client\HttpClient;
use MichalSpacekCz\Test\TestCaseRunner;
use Tester\Assert;
use Tester\Environment;
use Tester\TestCase;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -28,10 +27,7 @@ class CompanyRegisterRegisterUzTest extends TestCase

public function testGetDetails(): void
{
if (getenv(Environment::VariableRunner)) {
$file = basename(__FILE__);
Environment::skip("The test uses the Internet, to not skip the test run it with `php {$file}`");
}
TestCaseRunner::skip('The test uses the Internet, to not skip the test case run it with `' . TestCaseRunner::ENV_VAR . '`');
$expected = new CompanyInfoDetails(
200,
'OK',
Expand Down

0 comments on commit 8b77e23

Please sign in to comment.