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
5
votes
1 answer

phpunit mockery No matching handler found

When I try to execute phpUnit with multiple test, someone fails with this output: No matching handler found [...] Either the method was unexpected or its arguments matched no expected argument list for this method. But if I execute the test alone,…
naxo8628
  • 116
  • 1
  • 7
5
votes
2 answers

Mockery: test if argument is an array containing a key/value pair

How can I use mockery and hamcrest to assert that when a method of a mock object is called, one of the arguments passed to it is an array containing a key/value pair? For example, my test code might look like this: $mock =…
Jodes
  • 14,118
  • 26
  • 97
  • 156
5
votes
3 answers

Unittesting Laravel 5 Mail using Mock

Is there a way to test Mail in Laravel 5? tried the only legit Mock example I see on the internet but it seems it only works on Laravel 4. current code below. $mock = Mockery::mock('Swift_Mailer'); …
bonbon.langes
  • 1,718
  • 2
  • 22
  • 37
5
votes
1 answer

How can I test a function that uses DateTime to get the current time?

Most of the answers I have seen on StackOverflow are without using the DateTime object, and are instead using the date() function. This makes them very dirty solutions (overriding date(), mocking a protected function of the subject under test,…
Parham Doustdar
  • 2,019
  • 3
  • 22
  • 41
5
votes
1 answer

How do i properly test Laravel events?

I'm writing tests for a model which fires an event when one of its properties is changed. Unfortunately, I can't figure out the best way to test this. The model code is: class Foo extends Eloquent { public function setNameAttribute($value) { …
nstapelbroek
  • 75
  • 1
  • 7
5
votes
1 answer

Mocking a call with chained methods and arguments

Im learning how to use mockery in order to run some unit test and Im not sure what to do to mock my database class. It consists of separate methods that can be chained like these two examples: $db->select('someTblName',['fieldName']) …
LouieV
  • 1,032
  • 2
  • 15
  • 28
5
votes
1 answer

Unit testing a closure in Laravel

The closure in the code below has made this code very difficult to test. How can I continue to eager load these items and maintain full testability? public function scopeWithCompanyPreferences(Builder $builder) { return $builder->with([ …
Ben
  • 60,438
  • 111
  • 314
  • 488
5
votes
2 answers

Laravel - input not passing over through unit test

I'm receiving the below error when running my unit tests. Seems that it doesn't like passing in the Input::get to the constructor, however when running the script within the browser the action works fine so I know it's not the controller code. If I…
Ashley Banks
  • 528
  • 5
  • 16
5
votes
2 answers

PHP Unit Testing and Mocking with Mockery

This could be a misunderstanding on my side about mocking. I would really appreciate an explanation regarding why mocking a nonexistent or existent class is a good thing? Example Scenario: Say we have one class dependent on the output of the second…
mulkave
  • 831
  • 1
  • 10
  • 22
4
votes
1 answer

How to mock a Validation Rule in Laravel

I want to mock a custom Validation rule (e.g. App\Rules\SomeRule). But when I run my test, it gives an Mockery\Exception\InvalidCountException: Method...should be called exactly 1 times but called 0 times. I've read Laravel's documentation on…
user11006952
4
votes
1 answer

How to use mock in Lumen using Mockery

How to use mock in lumen framework? I use the Lumen framework. The documents of lumen are very simple. I don't know how to use mockery or facades to mock Models. I tried some means, but no one worked. I want to mock two points of UserModel in…
yang li
  • 41
  • 2
4
votes
1 answer

Mocking Laravel Eloquent models - Mocking model that is used in AppServiceProvider

I am trying to mock an eloquent model using the alias directive as follows: $transporter = \Mockery::mock('alias:' . Transporter::class) I have added the following annotations to the class * @runTestsInSeparateProcesses * @preserveGlobalState…
Mazzy
  • 1,901
  • 2
  • 16
  • 36
4
votes
2 answers

How to mock a paypal transaction in laravel with phpunit?

While testing: While checkout items from my website, need to mock confirmation... so we can then continue processing the order. Where the testing can be done.. How would i swap out good code for a mock? such as: $gateway =…
Harry Bosh
  • 3,611
  • 2
  • 36
  • 34
4
votes
1 answer

Mocking Symfony Entities for Unit Testing using PHPUnit and Mockery

public function reportsToBeRun() { $reports = $this->repository->getReportsToBeRun(); foreach ($reports as $report) { $report->setUpdated(new \DateTime()); } return $reports; } I am learning test driven development in my…
Skyenar
  • 41
  • 1
  • 4
4
votes
1 answer

mockery overload class missing methods

Need a bit of help using Mockery - I want to overload a class, which is created using new HelperUtil() in a method. Using Mockery overload I can do this, but it is leaving me with an empty shell class. Which I then appear to have to create all…
OnIIcE
  • 811
  • 9
  • 27