546 questions
0
votes
0
answers
60
views
How to mock usager of Laravel scopes for unit testing?
I'm using Laravel 12 along with Pest (using Mockery), and I have this method :
public function destroy(array $ids): array
{
$data= Item::findAllByFieldIn('id', $ids)->get();
// ...
1
vote
1
answer
74
views
Laravel spies not calling implementation?
I have two classes, Foo and Bar:
class Foo {
private Bar $bar;
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
public function foo()
{
return $...
0
votes
2
answers
166
views
How to Mock Objects in PHP to Work Correctly with Null Coalescing Operator (??)?
I'm trying to write a PHPUnit test using Mockery, and I keep running into issues when using the null coalescing operator (??) with mocked properties. Specifically, ?? seems to always return null, ...
1
vote
1
answer
73
views
Laravel - how to assert with phpunit and mockery what is passed as constructor argument to another service when it is created?
For example I have classes:
class ServiceOne{
public function testOne($name){
app(ServiceTwo::class, ['name' => $name])->testTwo();
}
}
class ServiceTwo{
__construct(private string $...
0
votes
1
answer
96
views
PHP Mock a class that implements an interface
I am trying to test a factory that it returns the correct instance. But ApiAuthRequest class has to implement a specific interface in order for the test to succeed. How I can do it?
I have tried to ...
0
votes
1
answer
46
views
Why is my filtered relations collection empty despite mocking a valid HasMany relation?
I'm writing a unit test to filter out invalid relations from a model. The goal is to only keep relations that actually exist in the model. However, my test is failing because the filtered relations ...
0
votes
1
answer
65
views
Change mocked object class name in mockery
I am mocking the laravel request object and I need to modify the mocked object name from Mockery_3_API_V2_Custom_Requests_DestroyRequest to DestroyRequest. How is it possible?
protected function ...
2
votes
1
answer
345
views
Laravel 10 - how to mock static function in other class:
in CommonHelper class:
class CommonHelper {
public static function getValue(string $category, string $name)
{
}
}
in MyService class:
public function getContentOrtherService(): ...
0
votes
1
answer
261
views
Laravel - Mock single method in a command using PHP UNIT
I'm trying to test a command where there's a method I want to mock because it makes a call to an external service using Guzzle, but no matter what I try, I can't seem to mock it successfully. It ...
1
vote
0
answers
86
views
Confusion about mock reuse in phpunit
i'm writing some unit tests and became stuck at what i first thought was a bug, but after much frustration seems to be intended (but impossible to find documentation/mention of) behaviour/usage.
The ...
-1
votes
2
answers
78
views
PhpUnit test use Mockery - Bypass exception of method from another class
I have a laravel project and I got a problem with using PhpUnit test use Mockery.
This is the fist time I use Mockery.
Below, I have some pseudo code:
class Mail
{
public static function send($pin){...
1
vote
1
answer
102
views
Validating argument at specific mocked method call index? PHPunit/Mockery
So a very simple test: a method persisting some objects gets an array of data.
class A
{
public function __construct(private BucketRepository $repository){}
public function saveObjects(array $...
0
votes
1
answer
71
views
PHPUnit test using mockery is failing to intercept a method call on a mocked class
Here is my PHPUnit test (PHPUnit 10.5.2 Mockery 1.6.11)
public function testMergedDocumentsWithSuccessfulInit() {
$params = new \Buan\Request\Parameters([], [], [], []);
$...
2
votes
0
answers
570
views
Laravel Pest Framework / Mockery - How to run in separate process?
I have a Livewire component where i instantiate a new class called new Connector(). I want to mock this Connector with Mockery. As i understand, if you want to mock hard dependencies you can use '...
0
votes
1
answer
87
views
Mockery - Partial mock of public internally called method not work
I am not able to mock a public method 'substract' of a class who is called internally inside another method 'remove' (This is an example code but with same issue).
public class Inventory {
public ...
2
votes
0
answers
141
views
Mock Laravel Cashier's checkout method on the Billable trait
In my API I am getting the checkout url from Stripe using Laravel 11 Cashier 15 and
returning it to the front end to redirect the user.
$response = $user->newSubscription($name, $price)
->...
1
vote
1
answer
109
views
Mocking inherited class where new object is created or how to unsmell my class
Sorry for the messy title, I am trying to add Unit Tests to my (probably) smelly codebase.
I have the something like the following class:
namespace App\Service;
use App\Messages\ItemMessage;
use App\...
0
votes
0
answers
116
views
How to use different mocks for different subtests when using table driven tests in Golang?
I have the following table-driven test in go:
func Test_funcTest(t *testing.T) {
mockStr := "str"
type args struct {
arg1 String
...
1
vote
0
answers
532
views
Typed property Symfony\Bundle\FrameworkBundle\Controller\AbstractController::$container must not be accessed before initialization error
I'm encountering an error in my Symfony application related to accessing the $container property of Symfony\Bundle\FrameworkBundle\Controller\AbstractController. The error message I'm receiving is:
...
1
vote
1
answer
80
views
Does Mockery clone __get method? [duplicate]
I am using Laravel 9 together with mockery/mockery version 1.6.6
I have some strange behavior when I mock an Eloquent model.
The eloquent Model has a __get() method defined as :
public function ...
2
votes
1
answer
298
views
Mockery Throws Error When Mocking Redis Connection: Mockery\Exception\BadMethodCallException
I have a command and some methods related to Redis connection are running in it. I want to test some of these methods and for this I want to mock the Redis connection. I approached it like this:
...
0
votes
1
answer
185
views
How to Mock $wpdb->prefix using WP_Mock and Mockery?
🗺️ My Environment
PHP
7.4
WordPress
6.2
WP_Mock
1.0
PHPUnit
9.6.11
Mockery
1.6.6
🖥️ Code: TestCase
use PHPUnit\Framework\TestCase;
use WP_Mock;
use Mockery;
class WPAB_Database_Service_Test extends ...
1
vote
0
answers
38
views
How to mock a constructor class using Mockery [duplicate]
I attempted to mock the StripeClient class in Laravel using Mockery and the app() helper. While I managed to mock the balance->retrieve method and simulate responses successfully, I encountered ...
0
votes
0
answers
100
views
Laravel Test Case Exception while using Mockery
User model
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
const TYPE_MERCHANT = 'merchant';
const TYPE_AFFILIATE = 'affiliate';
/**
* The ...
3
votes
1
answer
374
views
Writing Tests in Go by Receiving Interfaces and Returning Structs
I'm having some issues implementing interface substitution for the Go code. Previously the code was not written with unit tests in mind and I am going back and adding support for testing (Testify and ...
1
vote
1
answer
210
views
Mocked protected method not returning value set in mock
I have a class called ModuleManifest with this constructor:
/**
* Create a new instance of the ModuleManifest class.
*
* @param string $vendor The vendor of the module.
* @param string $package ...
0
votes
1
answer
1k
views
Using Mockery with laravel, i got the error "No matching handler found for Mockery"
I am a beginner in testing in laravel. I tried for the first time mocking today and i am a bit lost.
English is not my native language too ^^:
Does someone know how to solve my problem ? Thank you !
...
0
votes
1
answer
443
views
Pass parameters into a mocked class' constructor in PHP/Mockery
This feels simple but the answer is eluding me behind internet pages and questions/answers, some of which I'm not confident enough to consider definitive.
My requirement is simple. I have a PHP class ...
1
vote
0
answers
65
views
Mocking the user in Laravel controller
In a Controller I have code that relies on the user id
public function getMyStuff(Request $request)
{
$user = Auth::user();
var_dump($user->getId()); // Debug code, yields 'string(0) "...
1
vote
1
answer
717
views
Php Unit getMockBuilder vs Mockery
Working a Laravel project that uses PHPUnit\Framework\TestCase class to run unit tests. In order to create mocks it sometimes uses the built in PHPUnit Api. e.g.
$this->mock = $this->...
1
vote
0
answers
86
views
Laminas/Zend Test that user is redirected if form is valid
I'm trying to write a test that checks if a user is redirected if the form is valid. In order to do this, I mock my form and make it return true when isValid is called.
But I get the following errors:
...
3
votes
2
answers
3k
views
Mocking static method in same Class (Mockery, Laravel9)
I am writing a unittest for a Laravel project.
I would like to test the public method testMethod() in the following class.
class Foo extends Model
{
public static function staticMethod($arg)
{
...
1
vote
0
answers
568
views
@runTestsInSeparateProcesses annotation leads to error (Laravel)
I am writing some tests that require me to use class aliased mocks. As a result, I need the app to revert back to the use of the class I am mocking once I am done with the mock (after running each of ...
1
vote
0
answers
360
views
Mocking static methods from the same class as the one that is being tested (Laravel)
I am trying to write tests for a Laravel application and I can't seem to figure out how to test static methods that call other static methods from the same class. Say I have this class:
class Foo
{
...
0
votes
1
answer
1k
views
How to override mocked calls expectations in table driven tests
In working on a table-driven test where I'm using some mocks generated by mockery and set some method-call expectations that depend on the data provided in the data set for each test case. I'm facing ...
0
votes
0
answers
135
views
How do I setup Mockery to return values based on previous method calls?
I'm using Laravel, Pest for my tests and Mockery for mocking. I have a simple action that looks like so:
public function handle(): array
{
$sites = [];
$envDir = dirname(__FILE__, 3).'/env';
...
0
votes
0
answers
56
views
Testing attribute set within component constructor
I have a very simple Laravel Component which sets a public property of $categories:
public function __construct(Category $category)
{
$this->categories = $category->children;
}
What I would ...
0
votes
1
answer
899
views
How to test twilio send sms with mockery?
I am writing a test case to send a sms using twilio sdk in php in a laravel application.
I created a mock of the Client class, and I expect that the client will receive the messages then create ...
0
votes
1
answer
224
views
Extend Mockery::on assertions for common Use Cases
Is extending Mockery for commonly used assertions possible?
Asserting that a mocked function receives an instance of a class AND matching an identifier is cumbersome.
Mockery::on(
fn ($arg) => $...
2
votes
0
answers
564
views
Testing gRPC server with golang and mockery fails with a timeout when mock fails
I am trying to test a gRPC service locally.
Here is the sample code
type TestGrpcServer struct {
t *testing.T
Server *grpc.Server
Lis *bufconn.Listener
Conn *grpc.ClientConn
}...
0
votes
0
answers
165
views
How to mock cashier defaultPaymentMethod() while unit testing in laravel?
i am not being able to mock cashier's defaultPaymentMethod method while testing. i have a controller named PaymentController and there is method called exec() as below:
public function exec()
{
$...
-1
votes
1
answer
74
views
Why I am unable to assert that method has been called on my mocked service?
I have made a simple class named MyService
namespace App\Services;
class MyService
{
private $anotherService;
public function setService(AnotherService $anotherService)
{
$this-&...
0
votes
1
answer
896
views
Mockery/PhpUnit: Mocking a response from a parent method of the same name
I have a situation like the example classes below where I have a child class that overrides a parent method. In certain situations it will return the original result from the parent class.
How do I go ...
0
votes
0
answers
344
views
Mockery\Exception\BadMethodCallException: Method Mockery_0__does not exist on this mock object
When running phpunit I get the following error:
Mockery\Exception\BadMethodCallException: Method Mockery_0__Tests_Unit_App_Utils_Blueprints_BlueprintRuleUtil::executeActiveBlueprintRules() does not ...
1
vote
1
answer
339
views
Mockery Laravel Elequent chained queries
I have a question regarding mockery in combination with Laravel.
I have sucessfully created a mockery object to mock all the public static methods such as where and find on the elequent model instance....
1
vote
1
answer
601
views
How to use Mockery v0.0.0-dev in Golang?
I am trying to generate mocks in Golang using mockery, and the repo requires v0.0.0-dev.
I ran brew install mockery but that only installs v2.15.0, and thus cannot generate mocks with v0.0.0-dev. How ...
2
votes
1
answer
3k
views
Mock a method in enum using PHPunit Mockery in PHP
I have run into a problem when mocking Enums using Mockery + PHPUnit in PHP 8.1 (im using the laravel framework but I'm not sure this is relevant).
I have defined an enum:
<?php namespace ...
-1
votes
1
answer
571
views
PHPUnit/Mockery - Partially mock a function
Say i have this function. In this function i create a Mollie payment using the Mollie Laravel API Wrapper. The function $molliePayment needs to be mocked as i don't want to call the API during testing....
0
votes
1
answer
587
views
Laravel Cache testing issue
I'm trying to the the following piece of code:
public function remember(string $key, \Closure $callback, int $ttl = null): mixed
{
return \Cache::remember($key, $ttl, $callback);
}
with:
public ...
0
votes
1
answer
362
views
How to Mock environment in Laravel 5.7
I'm trying to implement a unit test in Laravel 5.7 I want to mock APP_ENV testing to production or any else as I want.
Unit test code:
App::shouldReceive('environment')->with('APP_ENV')->once()-&...