mockery

Laravel: how to mock dependency injection class methods

て烟熏妆下的殇ゞ 提交于 2020-02-24 10:17:30
问题 I'm using the GitHub API through a Laravel API Wrapper. I've created a dependency injection class. How can I mock the exists method within the App\Http\GitHub.php class? App\Http\GitHub.php : use GrahamCampbell\GitHub\GitHubManager; class Github { public $username; public $repository; public function __construct($username, $repository, GitHubManager $github) { $this->username = $username; $this->repository = $repository; $this->github = $github; } public static function make($username,

Mock method from the same class that tested method is using

瘦欲@ 提交于 2020-01-24 14:34:40
问题 I have following code: class Foo() { public function someMethod() { ... if ($this->otherMethod($lorem, $ipsum)) { ... } ... } } and I'm trying to test the someMethod(), I don't want to test otherMethod() since it's quite complex and I have dedicated tests - here I would only like to mock it and return specific values. So I tried to: $fooMock = Mockery::mock(Foo::class) ->makePartial(); $fooMock->shouldReceive('otherMethod') ->withAnyArgs() ->andReturn($otherMethodReturnValue); and in test I'm

Mock method from the same class that tested method is using

会有一股神秘感。 提交于 2020-01-24 14:33:13
问题 I have following code: class Foo() { public function someMethod() { ... if ($this->otherMethod($lorem, $ipsum)) { ... } ... } } and I'm trying to test the someMethod(), I don't want to test otherMethod() since it's quite complex and I have dedicated tests - here I would only like to mock it and return specific values. So I tried to: $fooMock = Mockery::mock(Foo::class) ->makePartial(); $fooMock->shouldReceive('otherMethod') ->withAnyArgs() ->andReturn($otherMethodReturnValue); and in test I'm

laravel 4 mockery mock model relationships

 ̄綄美尐妖づ 提交于 2020-01-04 02:50:21
问题 say I have two models that extend from Eloquent and they relate to each other. Can I mock the relationship? ie: class Track extends Eloquent { public function courses() { return $this->hasMany('Course'); } } class Course extends Eloquent { public function track() { return $this->belongsTo('Track'); } } in MyTest, I want to create a mock of course, and return an instance of track, by calling the track property, not the track instance ( I don't want the query builder ) use \Mockery as m; class

Mocking models with a relationship in Laravel

核能气质少年 提交于 2019-12-24 17:01:37
问题 I'm attempting to create a Mockery of CustomObject then chain the retrieval of OtherObject onto it using something identical to $this->CustomObject->with('OtherObject')->get(); I can't seem to figure out how to mock this ->get() at the end there. I'm mocking both of those models in my constructor method ['Eloquent', 'OtherObject', 'CustomObject'] . If I remove the ->get() everything runs smoothly and my tests pass (aside from the php errors the view is then giving me, but those don't matter

Testing Laravel facades with mockery always passes, even when it should fail

喜欢而已 提交于 2019-12-24 00:57:31
问题 I'm trying to mock some facades in Laravel during unit testing, but it seems that the tests always pass no matter what. For example, this example taken from the Laravel docs here: Event::shouldReceive('fire')->once()->with('foo', array('name' => 'Dayle')); It seems I can put that in any of the test methods and they always pass even though nothing of the sort has been done with the Event facade. Here is the test class: class SessionsControllerTest extends TestCase { public function test

Laravel 5 - Using Mockery to mock Eloquent model

被刻印的时光 ゝ 提交于 2019-12-22 12:16:09
问题 Been looking all over the internet but don't seem to find an answer to my problem. I've been diving into testing controllers in Laravel using PHPUnit and Mockery. However, I don't seem to get my Eloquent based models mocked correctly. I did manage to mock my Auth::user() the same way, although this is not used in the test below. Function in AddressController that needs to be tested: public function edit($id) { $user = Auth::user(); $company = Company::where('kvk', $user->kvk)->first();

Laravel 5 - Using Mockery to mock Eloquent model

蓝咒 提交于 2019-12-22 12:16:09
问题 Been looking all over the internet but don't seem to find an answer to my problem. I've been diving into testing controllers in Laravel using PHPUnit and Mockery. However, I don't seem to get my Eloquent based models mocked correctly. I did manage to mock my Auth::user() the same way, although this is not used in the test below. Function in AddressController that needs to be tested: public function edit($id) { $user = Auth::user(); $company = Company::where('kvk', $user->kvk)->first();

Unittesting Laravel 5 Mail using Mock

瘦欲@ 提交于 2019-12-22 06:16:22
问题 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'); $this->app['mailer']->setSwiftMailer($mock); ...some more codes here... $mock->shouldReceive('send')->once() ->andReturnUsing(function($msg) { $this->assertEquals('My subject', $msg->getSubject()); $this->assertEquals('foo@bar.com', $msg->getTo()); $this->assertContains('Some string', $msg->getBody

Error when mocking interfaces in PHP using Mockery

坚强是说给别人听的谎言 提交于 2019-12-22 04:46:22
问题 I have run into a problem when mocking Interfaces using Mockery in PHP (im using the laravel framework but I'm not sure this is relevant. I have defined an interface <?php namespace MyNamespace\SomeSubFolder; interface MyInterface { public function method1(); } And I have a class that typehints that interface on one of the methods... <?php namespace MyNamespace\SomeSubFolder; use MyNamespace\SomeSubFolder\MyInterface; class MyClass { public function execute(MyInterface $interface) { //does