nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
552 stars 11 forks source link

Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, object given #84

Closed Kalec closed 6 years ago

Kalec commented 6 years ago

I am getting several instances of this error both in my own code and also when cloning directly from this repo.

Tests\Features\Backstage\ViewPublishedConcertOrdersTest::a_promoter_can_view_the_orders_of_their_own_published_concert Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, object given, called in /ticketbeast-master/vendor/laravel/framework/src/Illuminate/Database/Query/Grammars/Grammar.php on line 250

I have narrowed it down to the "Hack" in the concert model to establish the relationship with the orders, but I can't figure out how to solve it.

currently the code is

public function orders()  
{
    return Order::whereIn('id',$this->tickets()->pluck('order_id'))->get();
}

Changing the implementation of the "order" relationship to a standard "belongsToMany" like this

public function orders() 
{
    return $this->belongsToMany(Order::class, 'tickets');
}

fixes all but the "calculating_the_revenue_in_dollars" test in Unit\ConcertTest. My guess is that whereIn is returning a Collection, but an array was expected in the controller. Anyone can see if I'm on the right track? and if so, can you suggest a way to fix it?

To reproduce, I cloned this repo, added .env, .env.dusk.local, .env.testing, phpunit.xml, then composer install, npm install, gulp and phpunit. Running on PHP 7.1.8

adamwathan commented 6 years ago

Hmm can you share a repo that demonstrates this? Even after a fresh composer update (I even tried on Laravel 5.6 as well) I still get a passing test suite without any changes:

image

adamwathan commented 6 years ago

One difference I noticed is that in the repo, the code for the orders method is this:

    public function orders()
    {
        return Order::whereIn('id', $this->tickets()->pluck('order_id'));
    }

...with no get() on the end like in your example.

Kalec commented 6 years ago

Adam, yes, sorry, I was trying things and I copied the wrong line. You are right, the code should be without the get() however, it still produces the errors.

I don't understand though, I just cloned this repo, and have the same errors. Must be something in my local environment then?

Kalec commented 6 years ago

In case anyone is interested, I managed to fix the problem. I had PHPUnit installed globally on my machine, and it seems that this was the source of the problems. Once I removed the global version of PHPUnit (even though it was the exact same version that was running locally with the project) all the tests went straight back to green. I reinstalled PHPUnit globally to confirm and sure enough, the errors returned. I came to the conclusion that it's not worth having PHPUnit installed globally.

FatBoyXPC commented 6 years ago

For anybody reading - it's usually best to use the version of the tools provided in the project. It solves this problem, along with many others (such as people who don't have it installed globally).