1

I'm trying to write a test but I can't authenticate with a fake test user, I'm using mongodb, jenssegers/laravel-mongodb and jwt-auth

this is the factory I've created

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use Faker\Generator as Faker;
use Illuminate\Support\Str;

$factory->define(\App\User::class, function (Faker $faker) {
    return [
        'username' => $faker->name(),
        'password' => $faker->password()
       
    ];
});

phpunit.xml file

<php>
  <server name="APP_ENV" value="testing"/>
  <server name="BCRYPT_ROUNDS" value="4"/>
  <server name="CACHE_DRIVER" value="array"/>
  <server name="DB_CONNECTION" value="mongodb"/>
  <server name="DB_DATABASE" value=":memory:"/>
  <server name="MAIL_DRIVER" value="array"/>
  <server name="QUEUE_CONNECTION" value="sync"/>
  <server name="SESSION_DRIVER" value="array"/>
</php>

this is the test

p

ublic function test_if_can_customer(){
        $this->withoutExceptionHandling();
        $user = User::factory()->make();

        $url = '/customer/12345646';
        $response = $this->actingAs($user,'api')
                         ->json('GET', $url);
    
        $response
            ->assertStatus(200)
            ->assertJson([
                'meta'=>[
                    'success'=>true
            ]
       ]);
}

and I get this error:

1) Tests\Feature\LeadTest::test_if_can_get_customer
MongoDB\Driver\Exception\AuthenticationException: Authentication failed.
 

UPDATE:

If I use sqlite instead of mongodb

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/>
        <server name="DB_DATABASE" value=":memory:"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
    </php>

I get this error:

TypeError: Argument 1 passed to Jenssegers\Mongodb\Query\Builder::__construct() must be an instance of Jenssegers\Mongodb\Connection, instance of Illuminate\Database\SQLiteConnection given, called in /home/myuser/myproject/vendor/jenssegers/mongodb/src/Jenssegers/Mongodb/Eloquent/Model.php on line 415
af_159623
  • 197
  • 10
  • For genetal testing, you should use the SQLite connection. In case you want to actually interact with your Mongo driver, you could create a separate database and specify it in your phpunit.xml – Kenny Horna Apr 18 '21 at 16:35
  • Your test never run because the connection to the database is failing. (MongoDB needs a username and password but you didn't specify it in your phpunit.xml). I agree with the comment above, you should use SQLite for testing instead of mongoDB in your phpunit.xml, then you don't need a username/password. – LobsterBaz Apr 18 '21 at 21:57

1 Answers1

0

Your factory and test code never run because the connection to the test database is failing. It's failing because MongoDB needs a username and password but you didn't specify it in your phpunit.xml.

However, like Kenny Horna said in his comment above, you should use SQLite for testing instead of mongoDB.

In your phpunit.xml, change your DB_CONNECTION to sqlite:

  <server name="DB_CONNECTION" value="sqlite"/>
LobsterBaz
  • 1,752
  • 11
  • 20
  • I get an error I use `sqlite` I've updated the question – af_159623 Apr 19 '21 at 16:45
  • Looks like an issue with your package `Jenssegers\Mongodb`. Did you properly configure your `config/database.php` and `.env` files with your credentials? – LobsterBaz Apr 19 '21 at 22:29
  • I think its correct becaus te app works , the problem its only ton testing using sqlite – af_159623 Apr 20 '21 at 16:24
  • The problem is your testing setup is not properly configured. It's calling `Jenssegers\Mongodb` without the proper credentials. Instead it should call SQLite. Check you configuration files. – LobsterBaz Apr 20 '21 at 22:41