Download the PHP package open-dxp/test-foundation without Composer

On this page you can find all versions of the php package open-dxp/test-foundation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package test-foundation

OpenDXP Test Foundation

The test foundation gives an OpenDXP bundle or project what it needs to run tests:

Tests are written with Pest. Test data is created with the Foundry factories that OpenDXP ships in OpenDxp\Test. The transaction comes from DAMADoctrineTestBundle.

Testing in the documentation of OpenDXP describes how you write tests. It also describes the test API of OpenDXP with its factories and expectations.

Set up a bundle

These steps take a bundle from no tests to its first green test. The examples use a bundle called Acme\BlogBundle.

1. Require the package

Give the tests a namespace in composer.json:

2. Add phpunit.xml.dist

The paths in this file are relative to the application that runs the tests. Your bundle is a dependency there.

The extension of DAMA rolls back the transaction of every test. The extension of Foundry starts Foundry for every test. Without it, no factory works. InstallDefinitions installs the data object classes of the package, as Data object classes describes.

Add <directory>tests/Unit</directory> once the bundle has unit tests. PHPUnit stops when a directory it names does not exist.

3. Add the test kernel

A bundle has no application of its own. The foundation builds one around it, and tests/Application/TestKernel.php registers the bundle in it:

4. Assign the test case

tests/Pest.php assigns the test case:

5. Write the first test

tests/Feature/Application/BootTest.php:

Set up a project

A project is its own application. Its kernel lives in tests/TestKernel.php. It extends the project's kernel and uses the Testable trait:

phpunit.xml.dist and tests/Pest.php are the same as for a bundle. KERNEL_CLASS names App\Tests\TestKernel. A project has no tests/Application/ directory.

[!NOTE] opendxp-test install runs the project's own migrations after it creates the data object classes. Migrations in vendor/ or in a bundle belong to a package. A package with an installer marks them as executed. The migrations of a bundle without an installer run on the installation.

Run the tests

With the testkit

The OpenDXP testkit builds the application, installs OpenDXP and runs the tests of a bundle or a project. Your checkout stays untouched:

Without the testkit

A project runs its tests in its own environment. DATABASE_URL in .env.test or .env.test.local names an empty database:

A bundle has no application of its own. You need PHP and an empty database:

In CI

The reusable workflows run exactly these commands. The testkit runs them as well. A test that passes with the testkit therefore also passes in CI.

Directory structure

Writing Tests describes the rest of the layout and where each piece of test code goes.

Configuration of the test application

The test kernel reads its own configuration from the directory config/ beside its class file. For a bundle that is tests/Application/config/, and for a project tests/config/. The kernel loads the configuration in this order:

  1. config/security.yaml beside the kernel
  2. the configuration of the application
  3. the configuration of the foundation
  4. config/<environment>.yaml beside the kernel, which is config/test.yaml by default

Symfony accepts new firewalls only from the first file that names any. A bundle that needs a firewall of its own, like a frontend login, therefore lists all firewalls in config/security.yaml.

The configuration of the foundation adapts OpenDXP to tests:

Test cases

Test case What it does
TestCase boots the application and handles requests
BrowserTestCase the same, plus a real browser
EnvironmentTestCase the same, in another configuration of the application
WithoutRollbackTestCase the same, but restores a database snapshot instead of rolling back

Every test runs in a database transaction. The transaction is rolled back after the test.

Static state

Before every test, the test case sets the timezone from the system settings. It also sets up OpenDXP the way the frontend sees it:

After every test, the test case resets what OpenDXP keeps in statics: the runtime cache, the system configuration, the admin mode, the current site and the current static route. A test restores any other static state it changes.

Tests without rollback

Some tests need committed data:

These tests extend WithoutRollbackTestCase. It takes a snapshot of the database before the first of these tests and restores it after each test. The snapshot also undoes the changes made by the processes of Concurrency::run(). A test therefore never cleans up after itself.

The snapshot is stored in tables prefixed with snapshot_ in the same database. The database user needs no additional privileges. Files on disk are not restored, such as the files of versions and assets. Schema changes are not restored either.

To combine the snapshot with another test case, call DatabaseSnapshot in your own test case:

Concurrency

Concurrent requests that change the same data can leave it in an inconsistent state. For example, two customers redeem the same voucher code at the same time. Concurrency::run() runs closures in parallel and returns their results in the order of the closures.

Concurrency tests need committed data. Their folder extends WithoutRollbackTestCase in tests/Pest.php:

The test sends the same request twice at the same time:

Each closure runs in a separate process. Every process behaves like a new request with its own kernel, database connection and session. The processes only see committed data. For this reason, Concurrency::run() throws an exception in a test with rollback.

A race condition only shows up when the processes reach the critical section at the same time. A test double usually keeps that section open, for example by waiting one second between loading the voucher and saving it. Make sure the test fails when you remove the protection it covers.

A closure must return a serializable value, such as a status code. If a process throws an exception, its result is empty and the expectations of the test fail. Models loaded before Concurrency::run() do not reflect the changes made by the processes. Reload them with ['force' => true] or query the database.

Another configuration

Some behaviour only shows in another configuration, for example with the full page cache switched on. EnvironmentTestCase boots the application in a Symfony environment of its own:

The kernel then loads config/full_page_cache.yaml instead of config/test.yaml. The file imports test.yaml, so the environment adds to the test configuration:

Services

Container hands out the services of the running application:

Every service whose class the package under test or its tests define is public in the test application. Symfony would otherwise remove a private service that nothing injects, and a test could not fetch it. The services of other packages stay as they are.

A service definition that only works because Symfony removes it shows up as an error here. Fix it in the package's own configuration.

Test data

Factories

OpenDXP ships factories for its own models in OpenDxp\Test\Factory, with base classes for the factories of your package. Factories and Stories explains them.

Data object classes

Put the definitions of your data object classes into tests/Fixtures/. The file name is the name of the definition:

The InstallDefinitions extension installs them once, before the first test. Classes refer to classification stores and field collections, and object bricks refer to classes. The extension therefore installs classification stores first, then field collections, classes and object bricks.

The installation changes the schema. A schema change inside a test would end the transaction of that test. That is why the installation runs before the tests.

The extension reads tests/Fixtures unless you name another directory:

OpenDXP ships definitions for tests in OpenDxp\Test: data object classes, an object brick, a field collection and a classification store. Each class has a factory in OpenDxp\Test\Factory. The extension installs these definitions before your own when you ask for them:

A project keeps its classes in var/classes, and opendxp-test install creates them from there.

Install something else before the first test

A PHPUnit extension of your own installs anything else that changes the schema. BootKernelTrait boots the kernel that KERNEL_CLASS names:

Register the extension in phpunit.xml.dist after the extensions of DAMA and Foundry.

Files

A test that cares about the size of a file and not about its content:

Visitors from another country

The test application uses a small GeoIP database. It knows one address in each of these countries: AT, BE, CH, DE, FR, HK, HU and US.

The database is set in the parameter opendxp.geoip.db_file. A bundle that needs other countries sets this parameter to a database of its own.

Repeat a failing run

Random values can make a test fail only once in a while. Foundry prints the seed of its random values after every run:

Set the environment variable FOUNDRY_FAKER_SEED to that number, for example in the <php> section of phpunit.xml.dist. Every factory then produces the same values again.

External services

A test against an external service reads its address from an environment variable:

Variable Example
OPENDXP_TEST_REDIS_DSN redis://redis:6379
OPENDXP_OPEN_SEARCH_HOST opensearch:9200

The testkit sets a variable when its configuration lists the service under services. The Pest workflow sets it with enable_redis_service or enable_opensearch_service. A test skips itself when its variable is missing, as Writing Tests shows.

The browser

visit() sends the request to the kernel directly, and no JavaScript runs. playwright() drives a real browser. A test that does this extends BrowserTestCase and is in the group browser.

A browser test never waits for a fixed time. The browser waits on its own, and a sleep hides a race.

A page as a person uses it

A frontend test uses Page. It finds a field by its label, a region by its ARIA name, and a button or a link by its text, so the test needs no selector:

notifications() reads every element with role="alert" or role="status". description() reads the text a field names with aria-describedby, like a help text. A bundle builds the page objects of its own pages on top of Page. Page::inBrowser() opens the page in a real browser.

The logged in user

A test that calls a service directly, without a request, names the logged in backend user with Admin:

A service that asks who is logged in is then handed this user.

Static analysis

PHPStan reads the container the test kernel writes. The phpstan.neon of the package lists the paths to analyse and points at that container:

Composer keys

The package needs it Key
to run require
for development, and everyone can install it require-dev
never, but users may want it suggest
only for its tests, and not everyone can install it extra.opendxp-test.optional

extra.opendxp-test.optional maps each package to a constraint, like require-dev:

Commands

vendor/bin/opendxp-test builds and checks the application. DATABASE_URL and DATABASE_SERVER_VERSION name the database. The PHP that runs the commands is the PHP the application uses.

Command What it does
bundle <path> requires the bundle with its require-dev and optional packages, and copies the application template and the tests
bundle <path> --opendxp <constraint> the same, with this version of open-dxp/opendxp
install installs OpenDXP, the bundles and the data object classes, and warms the cache
analyse <path> [check] runs lint, and phpstan, deptrac and phparkitect where the package configures them
analyse <path> --baseline writes the PHPStan baseline next to phpstan.neon

Lint checks the container, the YAML configuration and the Twig templates. It lints the templates of a bundle in the test application. A project lints them in the environment it is deployed to. That is prod or production, whichever the project configures under config/packages/.

Versions

The major version follows OpenDXP. Test foundation 1.x supports OpenDXP 1.x, and 2.x will support OpenDXP 2.x.

The test foundation follows semantic versioning. A major version comes when existing tests would break otherwise. That happens with a new major version of Pest, Foundry, PHPUnit or OpenDXP, or when the foundation changes its own API.

Test foundation OpenDXP PHP Pest
1.x 1.5+ 8.3, 8.4, 8.5 4 on PHP 8.3, 5 on PHP 8.4 and up

Composer picks the Pest version that fits the PHP version and the other dependencies of the package. A package whose development dependencies do not allow Pest 5 stays on Pest 4. A test therefore uses only what Pest 4 and Pest 5 both offer.


All versions of test-foundation with dependencies

PHP Build Version
Package Version
Requires php Version ~8.3.0 || ~8.4.0 || ~8.5.0
dama/doctrine-test-bundle Version ^8.6
mtdowling/jmespath.php Version ^2.8
open-dxp/opendxp Version ^1.5
pestphp/pest Version ^4.7 || ^5.0
playwright-php/playwright Version ^1.5.1
playwright-php/playwright-symfony Version ^0.11
spatie/fork Version ^1.2.8
symfony/config Version ^7.4
symfony/console Version ^7.4
symfony/dependency-injection Version ^7.4
symfony/dotenv Version ^7.4
symfony/filesystem Version ^7.4
symfony/finder Version ^7.4
symfony/framework-bundle Version ^7.4
symfony/http-foundation Version ^7.4
symfony/http-kernel Version ^7.4
symfony/process Version ^7.4 || ^8.1
symfony/runtime Version ^7.4
zenstruck/browser Version ^1.12
zenstruck/foundry Version ^2.13
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package open-dxp/test-foundation contains the following files

Loading the files please wait ...