wottavm's avatar

My phpunit relation test fails

Hello everyone,

I am starting with testing and I am getting stuck at the relation part for my test.

Here is my code :

<?php

use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class CreateVersionTest extends TestCase
{

    use DatabaseTransactions;

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testCreateVersions()
    {

        $versions = factory(App\Version::class, 5)->make();

        if ($versions !== null) {
            $this->assertTrue(true);
        }
    }

    /**
     * Check if the versions are being made
     * @return void
     */
    public function testCreateUserVersion()
    {
        $users = factory(App\User::class, 2)
            ->create()
            ->each(function ($u) {
                $u->versions()->save(factory(App\Version::class)->make());
            });

        $versions = new App\Version();
        $versions->where('id', 1)->firstOrFail();

        if ($users->versions()->where('id', 1) !== null) {
            $this->assertTrue(true);
        }
    }
}

This test returns the following :

✘ ✝ ~/Desktop/project_meet  master±  php  ./vendor/bin/phpunit
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.

...E.

Time: 582 ms, Memory: 19.25MB

There was 1 error:

1) CreateVersionTest::testCreateUserVersion
Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_api_token_unique' (SQL: insert into `users` (`name`, `email`, `password`, `remember_token`, `updated_at`, `created_at`) values (Edison Lueilwitz MD, rebecca43@example.com, y$tG4PujkK1rl41WbKScx9v.4TMqbImq9pMQaHHoAsiQmaKbwG4P.aW, 6hdLpuitDS, 2016-08-04 11:28:19, 2016-08-04 11:28:19))

/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:725
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:681
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:454
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:410
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2055
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1423
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10864
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10864
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10853
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10804
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:90
/Users/************/Desktop/project_meet/tests/CreateVersionTest.php:34

Caused by
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_api_token_unique'

/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:718
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:681
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:454
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Connection.php:410
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php:32
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2055
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1423
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10864
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10864
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10853
/Users/************/Desktop/project_meet/bootstrap/cache/compiled.php:10804
/Users/************/Desktop/project_meet/vendor/laravel/framework/src/Illuminate/Database/Eloquent/FactoryBuilder.php:90
/Users/************/Desktop/project_meet/tests/CreateVersionTest.php:34

FAILURES!
Tests: 5, Assertions: 8, Errors: 1.
0 likes
6 replies
wottavm's avatar

instead or with the trait that I am using now ?

wottavm's avatar

It still fails unfortunately.

With the following error :

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' for key 'users_username_unique' (SQL: insert into `users` (`name`, `email`, `password`, `remember_token`, `updated_at`, `created_at`) values (Zetta Morissette, [email protected], yc9PDxyzHAuhufX6K8Ms5emb1tS5AktfiKGYrHeH03pQomlVBC95G, SduJmWS1aN, 2016-08-04 11:39:18, 2016-08-04 11:39:18))
wottavm's avatar

Ok got a bit further, but not there yet. When I am making data to test it doesn't give it an id...

Here is a dump on the object.

php vendor/bin/phpunit
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.

..App\User {#1047
  #fillable: array:12 [
    0 => "profile_picture"
    1 => "password_salt"
    2 => "user_role"
    3 => "skills_id"
    4 => "api_token"
    5 => "password"
    6 => "username"
    7 => "lastname"
    8 => "token"
    9 => "email"
    10 => "name"
    11 => "bio"
  ]
  #hidden: array:2 [
    0 => "password"
    1 => "remember_token"
  ]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:12 [
    "profile_picture" => "http://placehold.it/350x150"
    "password_salt" => "$2y$10$XCGJ5UN01LTs9NenZ5/SyOuwImyl2kHwo6BzMPhcnJLPxqmb42LD2"
    "user_role" => "owner"
    "skills_id" => 1
    "password" => "$2y$10$W107yWHeJy8hnhSpOvHoDOzXu1Gy/bmLfdmoMBnO2nQLpaSwiDl/C"
    "username" => "kaci78"
    "lastname" => "Boyle"
    "token" => "O0vwEJGtDk1wmRADo52O43zA63oDoBarHWF204SILtJXDZRbNGOIzji0Z8do"
    "email" => "[email protected]"
    "name" => "Rosalyn"
    "api_token" => "57a39d2b49dab8.17670083"
    "remember_token" => "fauvVwlOZ9"
  ]
  #original: []
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
  +wasRecentlyCreated: false
}
wottavm's avatar
wottavm
OP
Best Answer
Level 5

Got it working while doing the following :

<?php

    /**
     * Check if the versions are being made
     * @return void
     */
    public function testCreateUserVersion()
    {

        $user = factory(App\User::class)->create();

        $version = factory(App\Version::class)->create([
            'user_id' => $user->id
        ]);
        
        $version->user()->associate($user);
        $version->save();

        $check = App\Version::find(1)->user->name;


        $this->assertTrue($check == $user->name);
    }

?>

This also Checks if the user that is hooked on the version is the same as in the user table.

Please or to participate in this conversation.