Questions tagged [mockery]

Mockery is a PHP library used to create Mocks of Objects for testing purpose.

Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL).

https://github.com/padraic/mockery

529 questions
7
votes
1 answer

Unit testing a custom symfony constraint

This should be incredibly simple but it is driving me mad this afternoon, what is the correct way to unit test a custom symfony validator? All the articles I can find have exactly the same way as I am doing it class Foo extends Constraint { …
REBELinBLUE
  • 193
  • 2
  • 8
7
votes
2 answers

phpunit, laravel: Cannot use "parent" when current class scope has no parent

I'm using PHPUnit 6.5.13 and Laravel 5.5 on PHP 7.4. I recently upgraded from PHP 7.2 to 7.4. and it seems like that triggered the error. In my test I use $this->expectsEvents in order to test that an event is fired. The test class looks a little…
Nict
  • 3,066
  • 5
  • 26
  • 41
7
votes
1 answer

Testing overriden trait method execution

I have situation like this. I have some 3rd party trait (I don't want to test) and I have my trait that uses this trait and in some case runs 3rd party trait method (in below example I always run it). When I have code like this: use Mockery; use…
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
7
votes
1 answer

Test Queue functionality?

According to the Laravel Documentation, I can use Queue::fake(); prevent jobs from being queued. What is not clear how to test (PHPUnit) a few methods in the Job Class while it is not being queued. For example: class ActionJob extends Job { …
I'll-Be-Back
  • 10,530
  • 37
  • 110
  • 213
7
votes
1 answer

Mockery & PHPUnit: method does not exist on this mock object

Can you tell me where's the problem? I have a file GeneratorTest.php with the following tests:
PeraMika
  • 3,539
  • 9
  • 38
  • 63
7
votes
1 answer

Issue Mocking a Laravel Bootable Model Trait

I'm using bootable model trait to register certain events for the models using my Trait. However, I've run into an issue trying to mock models that are using the trait. Specifically, when a Mockery version of the model is instantiated, it's boot…
timbroder
  • 858
  • 1
  • 13
  • 25
7
votes
2 answers

How to mock Cache::remember in Laravel

Within a unit test method, I tried to mock a Cache::remember response like this: Cache::shouldReceive('remember') ->once() ->with('my_key', 120, function() {}) // There are 3 args in remember method ->andReturn([]); But I get this…
rap-2-h
  • 30,204
  • 37
  • 167
  • 263
7
votes
1 answer

Mockery: how to use shouldReceive with method_exists?

In my application code, I've got a method_exists check to authorize some hooking in a create process: // Note: $myClass is implementing a ListItemFactory interface. if ($isCreate) { $methodName = "create{$attr}ListItem"; if…
Philippe
  • 668
  • 8
  • 17
7
votes
6 answers

How do I mock a method called within the constructor in a PHP unit test?

I'm having trouble unit testing a class that has a method called within the constructor. I don't understand how to mock this. Perhaps I should use the 'setUp' method of phpUnit? I'm using the Mockery library. Is there is a better tool than…
Fabrizio Fenoglio
  • 5,767
  • 14
  • 38
  • 75
7
votes
2 answers

Laravel facade class not found when mocked in a unit test

I might be missing something here but I have a very simple helper class that creates a directory: // Helper class
Russ Back
  • 903
  • 2
  • 15
  • 27
6
votes
2 answers

How to mock an aliased class with a method that returns an instance of itself?

I've been successfully using Mockery with PHPUnit tests lately. Yet, there is a dependency in a project I'm currently working that uses static method calls to interact with an API. I'm struggling to test one particular use case and it feels like…
Gustavo Straube
  • 3,744
  • 6
  • 39
  • 62
6
votes
1 answer

How To Test Artisan Commands with ask() in Laravel 5.4

Trying to write a test for laravel php artisan command with ask() function. I have never used mockery before, but when i try to run test, it freezes, so i guess, i'm doing something wrong. MyCommand.php: public function handle() { …
Linas Lg
  • 199
  • 2
  • 17
6
votes
1 answer

PHPUnit - disable original constructor in Mockery

I want use Mockery and change this: $mockFoo = $this->getMockBuilder('Foo') ->disableOriginalConstructor() ->getMock(); to this: $mockFoo = m::mock('Foo'); But I don't know how disable original constructor in Mockery. Please help…
Marcin Zaremba
  • 253
  • 1
  • 3
  • 11
6
votes
2 answers

How to mock Laravel's eloquent accessor attribute

I'm implementing unit tests into my Laravel 4 application but I'm stuck on mocking accessor attributes. I have an Eloquent model that has an Accessor attribute in it. I'm trying to mock this model and return a value when this accessor attribute is…
Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
6
votes
1 answer

Cannot set public property on Mockery mock object

I'm trying to use Mockery to create a mock object that mimics PHP's internal ZipArchive class. I have something like the following PHP code: $zipMock = Mockery::mock('ZipArchive'); $zipMock->numFiles = 10; echo 'NUMBER OF FILES:…
FixMaker
  • 3,759
  • 3
  • 26
  • 44
1 2
3
35 36