DEV Community

Discussion on: How to write testable code

Collapse
 
prodigeris profile image
Arnas Kazlauskas

Why Mockery and not let's say Prophecy?

Collapse
 
ddarrko profile image
Daniel

The mock library used is not what is important in the article. If I had used Prophecy someone may very well have commented saying 'Why not Mockery' ?

Collapse
 
prodigeris profile image
Arnas Kazlauskas

I get it, no need to be defensive.
Don't get me wrong, I'm not saying that this is a bad choice.
I'm just curious why do you use Mockery as opposed to something that comes out of a box with PHPUnit. I've never tried Mockery so I'm eager to know its advantages.

Thread Thread
 
ddarrko profile image
Daniel

My apologies if it came across as defensive. It was not my intention. I have just used Mockery for such a long time I am used to it now.

I have never used Prophecy but taking a look at the differences there appear to be some subtle ones.

I will give it a try on my next project :) Thanks

Collapse
 
hopeseekr profile image
Theodore R. Smith • Edited

Instead of Mockery/Prophet, why do people not use anonymous classes? Oh, that's right. The vast majority of PHP developers have never bothered to learn them.

Take this:

   $account = Mockery::Mock(Account::class);
   $account->shouldReceive('setState')
       ->with('active')
       ->andReturn($account)
       ->once();
   $account->shouldReceive('save')
       ->andReturn($account)
       ->once();

It can be rewritten into this:

$account = new class extends Account {
    public function setState(string $state): self
    {
        return $this;
    }

    public function save(): self
    {
        return $this;
    }
};

These are largely the same. You can, if you need to, ensure that each of those functions are called only once (use static $runCount), then they would be largely identical.

Thread Thread
 
ddarrko profile image
Daniel

I use a mocking framework because it is consistent with what other developers I work alongside will use.

I would have no problems using an anonymous class but I don't see the benefit it offers over a mature, well tested and known library.