Download the PHP package pkly/phpunit-service-create-trait without Composer

On this page you can find all versions of the php package pkly/phpunit-service-create-trait. 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 phpunit-service-create-trait

PHPUnit Service Create Trait

A helper trait for PHPUnit 12+ for easier creation of services with dependencies in unit testing

Packagist Downloads

Installation

Simply run

Compatible with PHPUnit 12 and 13, requires PHP 8.4+

Usage

In any of your PHPUnit test cases simply

Any dependencies in the constructor as well as methods marked with Symfony's #[Required] attribute will be automatically plugged in with test doubles. This allows you to write complex tests without wasting time updating your construct calls each time you modify something.

Stubs by default, mocks on demand

PHPUnit is separating mocks from stubs and complains about every mock object that has no expectations configured (No expectations were configured for the mock object for X ...). Creating a mock for every single dependency would drown your test run in those notices, so every dependency is created as a double that PHPUnit does not know about - it is never verified and never complained about. Fetching one is what hands it over to the test:

The service itself is constructed immediately, so the order does not matter - expectations can be declared before or after you use it:

Several dependencies of the same type

Every parameter gets its own double, so a service depending on the same type twice receives two distinct ones. Fetching such a type without saying which parameter you mean throws a LogicException - pass the parameter name as the second argument:

Several services in one test

getMockedService() and getStubbedService() use the most recently created service by default. Pass the service class as the third argument to address any other one:

Okay, but what if I need to use something custom?

Simply assign the proper parameter name in either $constructor or $required in the appropriate methods. That will use your object instead of creating one for you, keep in mind you cannot retrieve it via $this->getMockedService().

Some parameters always have to be provided that way:

Internal classes (\DateInterval and friends) are handed to PHPUnit like any other type - most of them double just fine, and the ones that do not produce a precise error from PHPUnit telling you to provide it.

A nullable dependency (?Foo) still receives a double, pass null explicitly if that is what your test needs.

Partial objects?

Sure, works the same, just use createRealPartialMockedServiceInstance instead of createRealMockedServiceInstance, in that case you must also specify the methods to override in your mock. Returned instance is T&MockObject.

Its dependencies behave exactly like the ones of a normal service - unregistered until you fetch them. The partial mock itself is a regular PHPUnit mock though, so the methods you override are subject to the usual expectation rules.

Feature requests?

Sure, hit me up with an issue if you wish.


All versions of phpunit-service-create-trait with dependencies

PHP Build Version
Package Version
Requires php Version >=8.4
phpunit/phpunit Version ^12 || ^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 pkly/phpunit-service-create-trait contains the following files

Loading the files please wait ...