becklyn / test-helpers
Common test helpers
Installs: 1 070
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 4
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- ext-json: *
- doctrine/orm: ^2.11.1
- symfony/process: ^v5.4.3 || ^v6.0.3
- symfony/translation: ^v5.4.3 || ^v6.0.3
Requires (Dev)
- becklyn/rad: ^8.6.0
- phpunit/phpunit: ^9.5.13
- roave/security-advisories: dev-master
- symfony/phpunit-bridge: ^v5.4.3 || ^v6.0.3
README
Installation
composer require --dev becklyn/test-helpers
Usage
The bundle contains various base tests, that can be used in bundles / libraries.
Schema Validation Test
This test receives a list of directories and tests that there are no mapping errors in doctrine annotations.
Usage
Extend it in your tests
directory:
tests/ValidateEntitySchemaTest
:
use Becklyn\TestHelpers\Doctrine\SchemaValidationTest; class ValidateEntitySchemaTest extends SchemaValidationTest { }
There are two extension points:
-
getRootDir(): string
must return the absolute path to the root of your app. By default it assumes that your test is directly in the tests directory top level. Change it, if that isn't the case. -
getEntityDirs(): string[]
must return the list of absolute paths, where files with mapping info are stored. By default set togetRootDir()/src/Entity
.
No Missing Translations Test
This tests checks that there are no translations used that are missing in the library.
You need to install the package
becklyn/translations-extractor
, preferably via thecomposer-bin-plugin
:composer bin test require becklyn/translations-extractor
Translations will be extracted from:
- Validation constraint annotations in entities (on the class itself and its properties)
- Form field options in
AbstractType
s - Usages of the
BackendTranslator
(Mayd-specific) - Usages of
ExecutionContext(Interface)
in@Callback
methods - Symfony:
$this->get('translator')->trans('foobar')
in controllers - Symfony:
$this->get('translator')->transChoice('foobar')
in controllers - Symfony:
$this->addFlash()
and$this->getFlashBag()->add()
in controllers - Symfony: Form type choices
- Defaults messages from custom validation constraint message properties
- Specific constructor parameters of special classes
Usage
Extend it in your tests
directory:
tests/ValidateEntitySchemaTest
:
use Becklyn\PhpCs\Testing\NoMissingTranslationsTest; class ValidateTranslationsTest extends NoMissingTranslationsTest { }
There are several extension points:
getLocales(): string[]
returns the list of locales to validate. By default set to["de", "en"]
.getDirectoriesWithUsages(): string[]
returns the absolute paths to where there is code that is using translations (like forms, entities, templates, etc..). By default set to["getRootDir()/src"]
.getDirectoriesWithTranslations(): string[]
: returns the absolute paths to where the translations are stored. The translations files are expected to be in theyaml
format. By default set to["getRootDir()/src/Resources/translations"]
.getIgnoredKeys[]: (complex)
: returns the list of keys that should be ignored when validating for missing keys. See below for details. By default set to[]
.getRootDir(): string[]
: must return the absolute path to the root of your app. By default it assumes that your test is directly in the tests directory top level. Change it, if that isn't the case.getMockedFunctions(): string[]
: the names of the twig functions that are defined by this project / library. They need to be mocked, as otherwise Twig will throw exceptions.getMockedFilters(): string[]
: the names of the twig filters that are defined by this project / library. They need to be mocked, as otherwise Twig will throw exceptions.getMockedTests(): string[]
: the names of the twig tests that are defined by this project / library. They need to be mocked, as otherwise Twig will throw exceptions.
Defining Ignored Keys
The ignored keys are an indexed array, where
- the key is a valid RegEx pattern. *Be sure to make it as specific as possible (e.g. by using
^
and$
). - the value is the list of domains (
string[]
) where this ignore should apply. Instead of an array you can passtrue
to ignore it in any domain.
return [ "~^example\\..*$~" => ["form", "validators"], // ignore example.* in "form" and "validators" domain only // (i.e. will still be reported for "messages" for example) "~^second\\..*$~" => true, // ignore second.* in every domain ];