I have been reading the PHPUnit docs online and have been using the mock object matchers but I am not sure when you should or shouldn’t use them. For the example below, let’s say I have a class Foo I am writing a test for: Foo class My Test My question is should I be using $this->once() or not in
Tag: mocking
How to test create model service?
I decided to create CreateClassroomService to separte logic in my controller method. I am trying to test this service as part of learning unit testing, but I don’t know how. I don’t know how to mock the classroom object to control what the method should return. Does this mean that creating this service was not a good idea because I
How to “mock” Sentry client in the PHPUnit tests
In case if you need to test PHP application error handlers, you have to “mock” or just disable sending errors on remote servers in the Sentry client. What is the right way to do this?
How to make Laravel’s Notification throw an Exception in test
I have a few laravel commands that inherit from my own class to send slack messages if they fail. However if the slack notification fails I still want the original exception thrown so that the error still ends up in the logs if slack is unavailable or misconfigured. I have this and it works, but I can’t figure out how
How to mock properly IlluminateHttpRequest class from Laravel
I’m trying to test a method from a class which contains a method using the request() helper from Laravel. This is the method: Category class The test should make this helper to catch properly the URI when doing getRequestUri() but it’s actually returning an empty string. This is one of the thousand tries that I gave to the test. Test
PHP Mocking Final Class
I am attempting to mock a php final class but since it is declared final I keep receiving this error: PHPUnit_Framework_Exception: Class “DoctrineORMQuery” is declared “final” and cannot be mocked. Is there anyway to get around this final behavior just for my unit tests without introducing any new frameworks? Answer Since you mentioned you don’t want to use any other
Mockery: how does a passive partial mock differ from the default mock?
In the very last paragraphs of this (great) quick reference to Mockery, the author explains some behaviour modifiers for a mock which are not default, but may be useful. These include the makePartial() call and the shouldDeferMissing() call. How do these differ from the default behaviour? When you create a mock (Mockery::mock(‘myClass’)), and don’t add any method expectations, all method
PHPUnit assert no method is called
I have a ClassA that uses a ServiceB. In a certain case, ClassA should end up not invoking any methods of ServiceB. I now want to test this and verity no methods are indeed called. This can be done …
PHPUnit: expects method meaning
When I create a new mock I need to call the expects method. What exactly it does? What about its arguments? I can’t find the reason anywhere (I’ve tried docs). I’ve read the sources but I can’t understand it. Answer expects() – Sets how many times you expect a method to be called: If you know, that method is called
Mock private method with PHPUnit
I have a question about using PHPUnit to mock a private method inside a class. Let me introduce with an example: How can I stub the result of the private method to test the some more code part of the public function. Solved partially reading here Answer Usually you just don’t test or mock the private & protected methods directy.