Download the PHP package xepozz/internal-mocker without Composer

On this page you can find all versions of the php package xepozz/internal-mocker. 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 internal-mocker

Introduction

The package helps mock internal php functions as simple as possible. Use this package when you need mock such functions as: time(), str_contains(), rand, etc.

Latest Stable Version Total Downloads phpunit

Table of contents

Installation

Usage

The main idea is pretty simple: register a Listener for PHPUnit and call the Mocker extension first.

Register a PHPUnit Extension

PHPUnit 9

  1. Create new file tests/MockerExtension.php
  2. Paste the following code into the created file:

  3. Register the hook as extension in phpunit.xml.dist

PHPUnit 10 and higher

  1. Create new file tests/MockerExtension.php
  2. Paste the following code into the created file:

  3. Register the hook as extension in phpunit.xml.dist

Here you have registered extension that will be called every time when you run ./vendor/bin/phpunit.

By default, all functions will be generated and saved into /vendor/bin/xepozz/internal-mocker/data/mocks.php file.

Override the first argument of the Mocker constructor to change the path:

Register mocks

The package supports a few ways to mock functions:

  1. Runtime mocks
  2. Pre-defined mocks
  3. Mix of two previous ways

Runtime mocks

If you want to make your test case to be used with mocked function you should register it before.

Back to the created MockerExtension::executeBeforeFirstTest and edit the $mocks variable.

This mock will proxy every call of time() under the namespace App\Service through a generated wrapper.

When you want to mock result in tests you should write the following code into needed test case:

You may also use a callback to set the result of the function:

So your test case will look like the following:

See full example in \Xepozz\InternalMocker\Tests\Integration\DateTimeTest::testRun2

Pre-defined mock

Pre-defined mocks allow you to mock behaviour globally.

It means that you don't need to write MockerState::addCondition(...) into each test case if you want to mock it for whole project.

Keep in mind that the same functions from different namespaces are not the same for Mocker.

So back to the created MockerExtension::executeBeforeFirstTest and edit the $mocks variable.

After this variant each App\Service\time() will return 150.

You can add a lot of mocks. Mocker compares the arguments values with arguments of calling function and returns needed result.

Mix of two previous ways

Mix means that you can use Pre-defined mock at first and Runtime mock after.

State

If you use Runtime mock you may face the problem that after mocking function you still have it mocked in another test cases.

MockerState::saveState() and MockerState::resetState() solves this problem.

These methods save "current" state and unload each Runtime mock mock that was applied.

Using MockerState::saveState() after Mocker->load($mocks) saves only Pre-defined mocks.

Tracking calls

You may track calls of mocked functions by using MockerState::getTraces() method.

$traces will contain an array of arrays with the following structure:

Function signature stubs

All internal functions are stubbed to be compatible with the original ones. It makes the functions use referenced arguments (&$file) as the originals do.

They are located in the src/stubs.php file.

If you need to add a new function signature, override the second argument of the Mocker constructor:

Global namespaced functions

Internal functions

The way you can mock global functions is to disable them in php.ini: https://www.php.net/manual/en/ini.core.php#ini.disable-functions

The best way is to disable them only for tests by running a command with the additional flags:

If you are using PHPStorm you may set the command in the Run/Debug Configurations section. Add the flag -ddisable_functions=${functions} to the Interpreter options field.

You may keep the command in the composer.json file under the scripts section.

Replace ${functions} with the list of functions that you want to mock, separated by commas, e.g.: time,rand.

So now you can mock global functions as well.

Internal function implementation

When you disable a function in php.ini you cannot call it anymore. That means you must implement it by yourself.

Obviously, almost all functions are implemented in PHP looks the same as the Bash ones.

The shortest way to implement a function is to use syntax:

Keep in mind that leaving a global function without implementation will cause a recourse call of the function, that will lead to a fatal error.

Restrictions

Data Providers

Sometimes you may face unpleasant situation when mocked function is not mocking without forced using namespace

That all because of PHPUnit 9.5 and lower event management system. Data Provider functionality starts to work before any events, so it's impossible to mock the function at the beginning of the runtime.


All versions of internal-mocker with dependencies

PHP Build Version
Package Version
Requires yiisoft/var-dumper Version ^1.2
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 xepozz/internal-mocker contains the following files

Loading the files please wait ....