jarektkaczyk / eloquence-validable

Eloquence Validable extension for Eloquent ORM
MIT License
24 stars 17 forks source link

Got an error when writing tests #1

Open ghost opened 6 years ago

ghost commented 6 years ago

I don't know what's the problem, but it only appears when using this package.

When testing, i have the following code:

$user = factory(User::class)->make([
    'email' => 'notAnEmail'
]);

$this->assertFalse($user->isValid());

This works fine, but, if i change my test to not assert, like:

$user = factory(User::class)->make([
    'email' => 'john@example.com'
]);

$this->assertFalse($user->isValid());

Then i get the following error on console:

Error: Cannot use object of type Illuminate\Support\Facades\Config as array
jarektkaczyk commented 6 years ago

Hi, provide context so we can reproduce the error. Laravel version, your code etc.

ghost commented 6 years ago

I'm using a brand new installation of Laravel 5.6. (created using the "laravel new MyProject" command).

Then i installed your package using composer:

(composer.json)

"require": {
  "php": "^7.1.3",
  "doctrine/dbal": "^2.6",
  "fideloper/proxy": "^4.0",
  "laravel/framework": "5.6.*",
  "laravel/tinker": "^1.0",
  "sofa/eloquence": "^5.6"
},

Then i updated my User class as described in docs:

User.php

<?php namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Sofa\Eloquence\Eloquence; // base trait
use Sofa\Eloquence\Validable; // extension trait
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;

class User extends Authenticatable implements ValidableContract, CleansAttributes {

  use Notifiable, SoftDeletes, Eloquence, Validable;

  protected $fillable = [
    'email', 'password',
  ];

  protected $hidden = [
    'password', 'remember_token',
  ];

  protected $dates = [
     'deleted_at'
   ];

  protected $rules = [
    'email'    => 'email|required|unique',
    'password' => 'required'
   ];
}

Then i created a test using the php artisan make:test UserTest command:

tests/Feature/UserTest.php

<?php

namespace Tests\Feature;

use App\User;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function testUserRegistration()
    {
        $created = factory(User::class)->create();

        $this->assertInstanceOf(User::class, $created);
    }

    public function testUserValidation()
    {
        $user = factory(User::class)->make();

        $this->assertTrue($user->isValid()); // and here i got the error
    }
}

If i change the testUserValidation method to perform an invalid request, then the assertion happens ok. Like:

    public function testUserValidation()
    {
            $user = factory(User::class)->make([
                'email' => null
            ]);

        $this->assertTrue($user->isValid()); // no errors, only testing fails, so its ok.
    }