Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Quick reminder that you can get away with not injecting Facades. They can be swapped out with mocks quite easily.
<?php
class MyMailer {
public function deliver()
{
// This facade doesn't need to be injected into the class.
Mail::send('emails.welcome', [], function($m)
{
$m->to('jeffrey@foo.com')
->subject('Welcome to the site')
->setCharset('UTF-8');
});
}
}
class ExampleTest extends TestCase {
public function tearDown()
{
Mockery::close();
}
public function testBasicExample()
{
// mock it
Mail::shouldReceive('send')->once();
(new MyMailer)->deliver();
}
}
@mikedfunk

This comment has been minimized.

Show comment Hide comment
@mikedfunk

mikedfunk Apr 15, 2013

This is fantastic! Can this even be done with PHPUnit's getMock? I didn't realize you could mock a static class!

This is fantastic! Can this even be done with PHPUnit's getMock? I didn't realize you could mock a static class!

@mikedfunk

This comment has been minimized.

Show comment Hide comment
@mikedfunk

mikedfunk Apr 16, 2013

This worked with the Mail class but not with a model. I am confused as to why. When I tried the same thing with a model, PHPUnit said the method shouldReceive was not defined.

This worked with the Mail class but not with a model. I am confused as to why. When I tried the same thing with a model, PHPUnit said the method shouldReceive was not defined.

@asakurayoh

This comment has been minimized.

Show comment Hide comment
@asakurayoh

asakurayoh Apr 22, 2013

It's not the Mail class, but the Mail Facade. Only facade got this magic.

It's not the Mail class, but the Mail Facade. Only facade got this magic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment