nothingworksinc / ticketbeast

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

collection issues #53

Closed Gavrisimo closed 7 years ago

Gavrisimo commented 7 years ago

Here is my test:

<?php

namespace Tests\Endpoint\Api\V1;

use App\Models\Vessel;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Tests\TestCase;

class VesselsTest extends TestCase
{

    use DatabaseMigrations, DatabaseTransactions;

    /**
     * @test
     *
     * @group endpoint
     */
    function api_vessels_update_updates_email()
    {
        /** @var Vessel $vessel */
        $vessel = Vessel::where('id', 1)->with(['emails'])->first();

        // DOES NOT WORK
        // $vessel->emails
        // $vessel->emails->first()
        // $vessel->emails->first()->id

        // DOES WORK
        // $vessel->getAttribute('emails')
        // $vessel->getAttribute('emails')[0]
        // $vessel->getAttribute('emails')[0]['id']

        $this->json('PUT', route('api.vessels.update', ['vessel' => $vessel->id]), [
            'name'   => 'UPDATED NAME',
            'emails' => [
                [
                    'id'    => $vessel->getAttribute('emails')[0]['id'],
                    'email' => 'updated@foo.bar',
                ],
            ],
        ], $this->auth_headers)
            ->assertJson([
                'data' => [
                    'name'   => 'UPDATED NAME',
                    'emails' => [
                        'data' => [
                            [
                                'id'    => $vessel->getAttribute('emails')[0]['id'],
                                'email' => 'updated@foo.bar',
                            ],
                        ],
                    ],
                ],
            ])
            ->assertStatus(200);
    }

}

Basically I have no idea what is going on with that $vessel->emails not working... :(

Any idea?

My TestCase that every test is extending is this:

<?php

namespace Tests;

use App\Models\Token;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{

    /**
     * Token to be used for API authentication.
     *
     * @var string
     */
    protected $token;

    /**
     * Headers for API authentication.
     *
     * @var array
     */
    protected $auth_headers;

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }

    /**
     * Setup the test environment.
     *
     * @return void
     */
    public function setUp()
    {
        parent::setUp();

        $this->artisan('db:seed');

        $this->token = Token::first()->token;
        $this->auth_headers = ['Authorization' => "Bearer {$this->token}"];
    }

}
liefie commented 7 years ago

Show the source to the Vessel class please.

Gavrisimo commented 7 years ago
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;

class Vessel extends Model
{

    use Notifiable;

    protected $table = 'vessels';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'team_plan_id',
        'is_active',
    ];

    /**
     * Route notifications for the mail channel.
     *
     * @return string
     */
    public function routeNotificationForMail()
    {
        return $this->emails->first()->email;
    }

    public function owner()
    {
        return $this->belongsTo(User::class);
    }

    public function contacts()
    {
        return $this->hasMany(Contact::class, 'vessel_id');
    }

    public function disclosures()
    {
        return $this->morphMany(Disclosure::class, 'disclosurable');
    }

    public function employees()
    {
        return $this->hasMany(VesselEmployee::class);
    }

    public function images()
    {
        return $this->morphMany(Image::class, 'imagable');
    }

    public function campaigns()
    {
        return $this->hasMany(Campaign::class);
    }

    public function addresses()
    {
        return $this->morphMany(Address::class, 'addressable');
    }

    public function emails()
    {
        return $this->morphMany(Email::class, 'emailable');
    }

    public function phones()
    {
        return $this->morphMany(Phone::class, 'phoneable');
    }

    public function websites()
    {
        return $this->morphMany(Website::class, 'websiteable');
    }

    public function networks()
    {
        return $this->morphMany(Network::class, 'networkable');
    }

    public function activities()
    {
        return $this->morphMany(Activity::class, 'activatable');
    }

    public function audiences()
    {
        return $this->hasMany(Audience::class);
    }

    public function team_plan()
    {
        return $this->belongsTo(TeamPlan::class);
    }
}
adamwathan commented 7 years ago

Hey @Gavrisimo!

Please be conscious that when you open an issue on this repo, 2000+ people get an email about it 😬

Happy to try and help but I think somewhere like the Laracasts forum is a better place for general testing questions like this, vs. specific questions about something in the course.

If you don't mind reposting the question over there, I'm happy to jump in and see if I can figure it out 👍🏻

Gavrisimo commented 7 years ago

Sorry everyone... =D

I'm just heavily into this course these days and this was bugging me for quite some time now, so I just thought to try and get some help here. 🐑

I did open laracasts thread, if someone can jump in and help I'd be super grateful:

https://laracasts.com/discuss/channels/testing/using-collections-in-testing

👍