How can be any service injected into WebTestCase subclass in Symfony? - unit-testing

Maybe I am missing something... doh, I think so, but could not find an answer to that.
WebTestCase generates this constructor sample:
public function __construct(?string $name = null, array $data = [], string $dataName = '')
{
parent::__construct($name, $data, $dataName);
}
Was trying to add my service as the first or last argument - Symfony throws an error:
Type error: Too few arguments to function Tests\AppBundle\Manager\ContactManagerTest::__construct(), 0 passed in /Library/WebServer/Documents/HEPT/vendor/bin/.phpunit/phpunit-5.7/src/Framework/TestSuite.php on line 568 and at least 1 expected in /Library/WebServer/Documents/HEPT/tests/AppBundle/Manager/ContactManagerTest.php:22
Should I somehow use container directly? Why is autowiring not working for WebTestCase classes if there is a bridge class?

WebTestCase are used in the context of PHPUnit (which has nothing to do with Symfony and its dependency injection).
They actually generate the kernel and its container, see this piece of code extracted from Symfony source code:
protected static function createClient(array $options = array(), array $server = array())
{
$kernel = static::bootKernel($options);
$client = $kernel->getContainer()->get('test.client');
$client->setServerParameters($server);
return $client;
}
This means that you can easily access the container like this:
$kernel = static::bootKernel($options);
$container = $kernel->getContainer();
Please note also that static::$kernel->getContainer() is available as soon as you created your client to make your test.

Related

How can I test a Yii2 model in a library project?

I'm trying to implement an adapter that is using a Yii model object extending yii\db\ActiveRecord. The object is passed as constructor arg to the adapter class.
My issue is now that I still couldn't figure out how to get this to work properly. I've even tried mocking it but got stuck because Yii is using lots of static methods to get it's objects. Sure, I could now try to mock them... But there must be a better way?
public function testSuccessFullFind(): void
{
$connection = (new Connection([
'dsn' => 'sqlite:test'
]))
->open();
$queryBuilder = new \yii\db\sqlite\QueryBuilder($connection);
$app = $this->createMock(Application::class);
\Yii::$app = $app;
$app->expects($this->any())
->method('getDb')
->willReturn($this->returnValue($connection));
$userModel = new UserModel();
$resovler = new Yii2Resolver($userModel);
$result = $resolver->find(['username' => 'test', 'password' => 'test']);
// TBD asserts for the result
}
The UserModel is used to find a user record internally.
This results in:
1) Authentication\Test\Identifier\Resolver\Yii2ResolverTest::testSuccessFullFind
Error: Call to a member function getDb() on null
vendor\yiisoft\yii2-dev\framework\db\ActiveRecord.php:135
vendor\yiisoft\yii2-dev\framework\db\ActiveQuery.php:312
vendor\yiisoft\yii2-dev\framework\db\Query.php:237
vendor\yiisoft\yii2-dev\framework\db\ActiveQuery.php:133
tests\TestCase\Identifier\Resolver\Yii2ResolverTest.php:31
The code above is obviously the WIP of a test case.
So how can I configure a test connection and get my ActiveRecord object to use it?
You can pass connection as argument of all() method:
$results = UserModel::find()->where(['id' => 1])->all($connection);

How to mock a Doctrine Repository in Zend Framework 3 using PHPUnit

I am trying to test a ZF3 controller action which, in the process, selects a user from the database via a Doctrine ORM repository using a token given as a GET-Parameter. As I need to make sure that the User exists I need to create a mock of the repository returning the user object. How do I do this?
My setup is the following:
The class UserControllerFactory is instantiating a UserController class:
class UserControllerFactory implements FactoryInterface {
/**
* #param ContainerInterface $container Zend\ServiceManager\ServiceManager
* #param string $requestedName
* #param array|NULL $options
*
* #return UserController
*/
public function __invoke(ContainerInterface $container, $requestedName, Array $options = NULL) {
$entityManager = $container->get('doctrine.entitymanager.orm_default');
$userRepository = $entityManager->getRepository('User\Entity\User');
return new UserController($container, $entityManager, $userRepository);
}}
In the UserController the acton resetPassword is called. It gets the needed parameter from the route and selects a user from the database matching the token:
public function resetPasswordAction() {
$request = $this->getRequest();
$passwordResetToken = $this->params()->fromRoute('token');
if(strlen(trim($passwordResetToken))) {
$user = $this->userRepository->findOneBy(
[
'passwordResetToken' => $passwordResetToken
]
);
...
If no user is found. The action will redirect to user to a different action.
PHPUnit test case:
public function testResetPasswordActionCanBeAccessed() {
$passwordResetToken = 'testToken1234';
$this->dispatch("/user/resetPassword/$passwordResetToken", 'GET');
$this->assertNotRedirect();
}
As there is no user having the token is will be redirected.
To my knowledge I need to create a mock of the repository (userRepository), create a mock user and use the mock repository retrieve the mock user having the token.
I am not sure if this is the right approche as I tried a lot of tutorial and never got it to work. I don't know how to "replace" the, in the action called "userRepository" with the in the unit test created mock object.
I am happy to provide more details if needed.
EDIT
As suggested by #DonCallisto (thank you) I changed my test case code to:
...
$mockedEm = $this->createMock(EntityManager::class);
$mockedUserRepository = $this->createMock('Core\Repository\EntityRepository');
$mockedEm->method('getRepository')->willReturn($mockedUserRepository);
$mockedUserRepository->method('findOneBy')->willReturn($mockedUser);
$this->dispatch("/$this->_lang/user/resetPassword/$passwordResetToken", 'GET');
...
but after calling the "dispatch" in the test case my controller call
$user = $this->userRepository->findOneBy(...)
will still return NULL instead of the mocked user object given in the test. If I debug the $mockedUserRepository, my $mockedUser is assigned correctly.
I also tried the suggested:
$mockedUserRepository->findOneBy([arrayWithParams])->willReturn($mockedUser);
But this will through an error due to the fact that $mockedUserRepository->findOneBy() is returning NULL.

Test JSON-returning controller method without MissingViewError

I am testing a Controller method that has only a JSON view. My method runs as expected, but the test method only returns "MissingViewException". Is there a solution to avoiding this exception in the unit test (besides inserting an empty file at View/People/map_leads.ctp)?
PeopleController.php
public function mapLeads($territory_id = null) {
$leads = $this->Person->getPeople([
'territory_id' => $territory_id
]);
$this->set('leads', $leads);
}
AppController.php
public $components = ['RequestHandler'];
routes.php
Router::parseExtensions('json');
PeopleControllerTest.php
public function testMapLeads() {
$id = 40;
$result = $this->testAction('/people/mapLeads/' . $id, array('return' => 'vars'));
}
View/People/json/map_leads.ctp exists and is properly utilized by CakePHP; it is only the test that wants to see View/People/map_leads.ctp.
I checked at CakePHP: calling testAction to a json-returning method causes missing view exception reminding about adding RequestHandler to $components. This does not resolve the exception.
You aren't issuing a JSON request/accessing a JSON endpoint, as neither your request URL does contain the .json extension, nor does your request send an appropriate Accept header (I don't remember whether the latter is possible with the 2.x controller test case class at all).
Use the .json extension and you should be good.
$this->testAction('/people/mapLeads/' . $id . '.json', array('return' => 'vars'));
Write this code inside your action.
$this->autoLayout = false;
$this->autoRender = false;
$this->response->type('application/javascript');

Issue testing Laravel Controller with Mockery | trying to get property of non-object

I'm very new to testing controllers and I'm running into a problem with a method(). I believe I'm either missing something in my test or my Controller / Repository is designed incorrectly.
The application I'm writing is basically one of those secure "one time" tools. Where you create a note, the system provides you with a URL, once that url is retrieved the note is deleted. I actually have the application written but I am going back to write tests for practice (I know that's backwards).
My Controller:
use OneTimeNote\Repositories\NoteRepositoryInterface as Note;
class NoteController extends \Controller {
protected $note;
public function __construct(Note $note)
{
$this->note = $note;
}
public function getNote($url_id, $key)
{
$note = $this->note->find($url_id, $key);
if (!$note) {
return \Response::json(array('message' => 'Note not found'), 404);
}
$this->note->delete($note->id);
return \Response::json($note);
}
...
I've injected my Note interface in to my controller and all is well.
My Test
use \Mockery as M;
class OneTimeNoteTest extends TestCase {
public function setUp()
{
parent::setUp();
$this->mock = $this->mock('OneTimeNote\Repositories\EloquentNoteRepository');
}
public function mock($class)
{
$mock = M::mock($class);
$this->app->instance($class, $mock);
return $mock;
}
public function testShouldReturnNoteObj()
{
// Should Return Note
$this->mock->shouldReceive('find')->once()->andReturn('test');
$note = $this->call('GET', '/note/1234567890abcdefg/1234567890abcdefg');
$this->assertEquals('test', $note->getContent());
}
}
...
The error I'm getting
1) OneTimeNoteTest::testShouldReturnNoteObj
ErrorException: Trying to get property of non-object
/Users/andrew/laravel/app/OneTimeNote/Controllers/NoteController.php:24
Line 24 is in reference to this line found in my controller:
$this->note->delete($note->id);
Basically my abstracted repository method delete() obviously can't find $note->id because it really doesn't exist in the testing environment. Should I create a Note within the test and try to actually deleting it? Or would that be something that should be a model test? As you can see I need help, thanks!
----- Update -----
I tried to stub the repository to return a Note object as Dave Marshall mentioned in his answer, however I'm now receiving another error.
1) OneTimeNoteTest::testShouldReturnNoteObj
BadMethodCallException: Method Mockery_0_OneTimeNote_Repositories_EloquentNoteRepository::delete() does not exist on this mock object
I do have a delete() method in my repository and I know it's working when I test my route in the browser.
public function delete($id)
{
Note::find($id)->delete();
}
You are stubbing the note repository to return a string, PHP is then trying to retrieve the id attribute of a string, hence the error.
You should stub the repository to return a Note object, something like:
$this->mock->shouldReceive('find')->once()->andReturn(new Note());
Building upon Dave's answer, I was able to figure out what my problem is. I wasn't mocking the delete() method. I didn't understand the need to mock each individual method in my controller that would be called.
I just added this line:
$mock->shouldReceive('delete')->once()->andReturnNull();
Since my delete method is just deleting the note after it is found, I went ahead and mocked it but set it to return null.

PHPunit mock - call a function in a returned mock

I'm pretty new to phpunit and mocking, and I want to test a Listener in my symfony2 project, what is a kernel exception listener.
This is the class I want to test:
public function onKernelException(GetResponseForExceptionEvent $event)
{
$code = $event->getException()->getCode();
if($code == 403)
{
$request = $event->getRequest();
$session = $request->getSession();
$session->getFlashBag()->add('notice', 'message');
$session->set('hardRedirect', $request->getUri());
}
}
And first I just wanted to test, so nothing happens if the code is 404, this is the test I wrote:
public function testWrongStatusCode()
{
$exceptionMock = $this->getMock('Exception')
->expects($this->once())
->method('getCode')
->will($this->returnValue('404'));
$eventMock = $this->getMockBuilder('Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent')
->disableOriginalConstructor()
->getMock();
$eventMock->expects($this->once())
->method('getException')
->will($this->returnValue($exceptionMock));
//here call the listener
}
but PHPunit say, getCode function was never called.
You can't use "chaining" as you've tried. The reason is that methods getMock and will return different objects. That's why you lose your real mock object. Try this instead:
$exceptionMock = $this->getMock('\Exception');
$exceptionMock->expects($this->once())
->method('getCode')
->will($this->returnValue('404'));
Edit
Ok. The problem is you cannot mock getCode method because it's final and it's impossible to mock final and private methods with PHPUnit.
My suggestion is: just prepare an exception object you want, and pass it as returned value to event mock:
$exception = new \Exception("", 404);
(...)
$eventMock->expects($this->once())
->method('getException')
->will($this->returnValue($exception));
This is how I mock the getCode() function. It actually gets called from the ResponseInterface::getStatusCode() function, so that is what you need to mock:
$guzzle->shouldReceive('get')
->once()
->with(
$url
)
->andThrows(new ClientException(
"",
Mockery::mock(RequestInterface::class),
Mockery::mock(ResponseInterface::class, [
'getStatusCode' => 404,
]),
));
You can use mockery library with PHPUnit, which is great tool and makes life easier.
$exceptionMock = \Mockery::mock('GetResponseForExceptionEvent');
$exceptionMock->shouldReceive('getException->getCode')->andReturn('404');
Check out documentation for more... and I hope you will love it.