hjerichen / prophecy-php
Prophesize PHP Functions.
Installs: 11 603
Dependents: 3
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:testing
Requires
- php: ^8.2
- phpspec/prophecy: ^1.8
- phpunit/php-text-template: ^2.0 | ^3.0 | ^4.0
Requires (Dev)
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^9.6.21 | ^10.0 | ^11.0
- psalm/plugin-phpunit: ^0.19.2
- roave/security-advisories: dev-latest
- vimeo/psalm: ^6.1
README
Prophecy-PHP
Mock build-in PHP functions for PHPUnit in Prophecy Style.
Installation
Use Composer:
composer require --dev hjerichen/prophecy-php
Usage
Use the trait PHPProphetTrait in PHPUnit Test Cases.
<?php namespace Some\Space; use PHPUnit\Framework\TestCase; use HJerichen\ProphecyPHP\PHPProphetTrait; use HJerichen\ProphecyPHP\NamespaceProphecy; class SomeTest extends TestCase { use PHPProphetTrait; /** @var NamespaceProphecy */ private $php; public function setUp(): void { parent::setUp(); $this->php = $this->prophesizePHP(__NAMESPACE__); } public function testSomething(): void { $this->php->time()->willReturn(2); $this->php->reveal(); self::assertEquals(2, time()); } }
Everything works like you know it from Prophecy:
<?php $this->php->time()->willReturn(1234, 1235, 1236); $this->php->date('Y', 1234)->willReturn('1970'); $this->php->date('Y', 1234000)->willReturn('1971'); $this->php->file_put_contents('/to/foo.txt', 'some content')->shouldBeCalledOnce(); $this->php->file_put_contents('/to/foo.txt2', 'some content')->shouldBeCalledOnce(); $this->php->file_put_contents('/to/foo.txt', \Prophecy\Argument::any())->shouldNotBeCalled(); $this->php->reveal(); //Only with this call the above functions will be mocked.
Restrictions
Only unqualified function calls in a namespace context can be mocked.
Known Issues
Because auf the PHP Bug 64346 mocking may not work. This is because of calling the original function in the namesapce before mocking it. In this case, you could try to use "prepare" in the "setUp" method:
<?php $this->php->prepare('time', 'date'); //If you have problems with the time and date functions.
License and authors
This project is free and under the MIT Licence. Responsible for this project is Heiko Jerichen (heiko@jerichen.de).