Questions tagged [prophecy]

Prophecy is a highly opinionated yet very powerful and flexible PHP object mocking framework.

Prophecy was originally created for PHPspec2 and is available in PHPUnit as of version 4.5.

Useful links

36 questions
10
votes
2 answers

PHPUnit prophesize a method without exact arguments

I'm mocking a UserRepository class using prophecy to ensure that when a POST request to /user is sent, that the create() method on UserRepository is fired. $repository =…
dargue3
  • 2,802
  • 2
  • 14
  • 23
7
votes
3 answers

How to mock a method from the class you are testing with Prophecy?

I want to use Prophecy ("phpspec/prophecy-phpunit") for the first time to create unit tests for my classes. I want to test a function that calls another function in the same service, here's the code: class UserManager { private $em; private…
alexandra
  • 1,182
  • 13
  • 24
5
votes
3 answers

PHPUnit - Mocking a trait

I have a trait that is used by multiple classes, i.e class SomeClass { use TimeoutTrait; function handle() { $this->traitFunction() // can this be mocked? } } PHP unit is capable to mock the traitFunction()?. Thanks in…
Pablo Rodriguez V.
  • 358
  • 1
  • 4
  • 9
4
votes
2 answers

How to mock the same method in Prophecy so it returns different response in each of its calls

In pure PHPUnit mocking I can do something like this: $mock->expects($this->at(0)) ->method('isReady') ->will($this->returnValue(false)); $mock->expects($this->at(1)) ->method('isReady') ->will($this->returnValue(true)); I was not…
Marcelo
  • 1,702
  • 2
  • 24
  • 42
3
votes
1 answer

TypeError: Return value of class::functionName() must be an instance of /Interface, null returned

How to specify a return type of function (an instance of Interface) using php prophecy. method is like:- class classname() { public $var; public functionName : Interface { return $this->var; } } IS it possible to do something like this?…
mevr
  • 1,075
  • 3
  • 14
  • 32
3
votes
1 answer

Disabling constructor in Prophecy

I have a Symfony class I am trying to mock using Prophecy. However, when I reveal the class it executes the constructor. Below is example code: $mock = $this->prophesize('Symfony\Component\HttpFoundation\File\UploadedFile'); $mock->reveal(); which…
Marinus
  • 521
  • 2
  • 15
2
votes
1 answer

Prophecy - Expect that no method will be called on an object

right now I'm using the following code in PHPUnit to expect that no method is called on a mock: $object = $this->createMock(ClassA:class); $object->expects($this->never())->method($this->anything()); So far I haven't found a way to achieve the same…
crossi100
  • 88
  • 1
  • 11
2
votes
0 answers

Unit testing guzzle http promises callbacks

I have the following code in a class: private function makeRequest(DetailedPayload $detailedPayload, $request): void { $this->httpClient ->sendAsync($request) ->then( function (ResponseInterface $response) use…
zambliner
  • 436
  • 2
  • 6
  • 21
2
votes
1 answer

How to provide properties for PHPUnit's prophecy objects?

I'm mocking an API with Prophecy. A call on the api object to payments() will return an object that has a get($id) method, which returns another object which has a few methods and properties. One of the properties is ID and I want to test that…
artfulrobot
  • 20,637
  • 11
  • 55
  • 81
2
votes
1 answer

Only this and nothing more with Prophecy?

So I have this in a PhpUnit test: $alias_manager = $this->prophesize(AliasManagerInterface::class); $alias_manager->cacheClear($source)->shouldBeCalledTimes(1); And I would like to tell Prophecy that this is all the alias manager should be called…
chx
  • 11,270
  • 7
  • 55
  • 129
1
vote
0 answers

PHP Prophecy test void method should be called

I'm writing some Unit Tests using PHP Prophecy as Mocking Framework. I'm trying to make a test to assure a void method is called, the code looks something like this: private Prophet $prophet; protected function setUp() { $this->prophet = new…
Skkrimon
  • 11
  • 2
1
vote
0 answers

PHP Prophecy mocking not existing class

I'm writing a package that I would like to write tests in Prophecy instead of Mockery. The package class Bar has a method sayName that requires an object implementing FooInterface as a parameter. with Mockery there is no problem for creating a mock…
Jimmix
  • 5,644
  • 6
  • 44
  • 71
1
vote
1 answer

I do not know how to test the create query builder

I'm trying to test the all method of a repository, but I do not know how to do the test, I want to use Profecy. I'm using DoctrineMongoDBBundle. Entity class UsuarioEntidade{ /** * @MongoDB\Id */ protected $usuId; /** *…
Rafael
  • 37
  • 4
1
vote
1 answer

How to make a Test Double for the RequestHandlerInterface class in Zend Expressive using Prophesy?

I'm trying to make a unit test for the process() method of a middleware in a Zend Expressive application. To do this, I need to mock the out the $delegate parameter for the method which is of type RequestHandlerInterface and will have the method…
Knight
  • 576
  • 7
  • 19
1
vote
1 answer

PHP unit failure testing

I have to test this class for failure condition
Kiren S
  • 3,037
  • 7
  • 41
  • 69
1
2 3