jgravois's avatar

Failing Test due to Coalescence

$co = CompanyCustomerAgreement::whereCompanyCode($code)->first();

if(!isset($co)) {
    $this->redirect('/expired');
} else {
    $this->lcStep = $co->last_completed_step ?: 1;
    $this->currentStep = $co->last_completed_step ?: 1;
} // end if

Shouldn't null coalescence kick in???

/** @test */
public function caf_loads_properly(): void
{
    $caf = CompanyCustomerAgreement::factory()->make([
        'last_completed_step' => null
    ]);

    Livewire::actingAs($this->user)
        ->test(CustomerWizard::class, ['code' => 'UAMI2'])
        ->assertSet('companyCode', 'UAMI2')
        ->assertSet('currentStep', 1)
        ->assertSet('lcStep', 1)
        ->assertOK();
} // end test
Failed asserting that null matches expected 1.

Fails on either ->assertSet('currentStep', 1) or ->assertSet('lcStep', 1)

0 likes
8 replies
bugsysha's avatar

That is not null coalescence operator, but ternary operator.

Have you tried

$caf = CompanyCustomerAgreement::factory()->make([
  'last_completed_step' => null,
  'code' => 'UAMI2',
]);
jgravois's avatar

'company_code' => 'UAMI2', is in the factory but I tried it anyway and it is still Failed asserting that null matches expected 1.

ternary or coalescence ... isn't that saying if $co->last_completed_step is null, make this one or else use $co->last_completed_step ???

guybrush_threepwood's avatar

There's a slight difference.

For example with ternary if $co->last_completed_step is 0, $this->lcStep will be set to 1.

$co->last_completed_step = null;

$this->lcStep = $co->last_completed_step ?: 1; // Will be set to 1
$this->lcStep = $co->last_completed_step ?? 1; // Will be set to 1
$co->last_completed_step = 0;

$this->lcStep = $co->last_completed_step ?: 1; // Will be set to 1
$this->lcStep = $co->last_completed_step ?? 1; // Will be set to 0
jgravois's avatar

Excellent explanation -- thank you!!!

This makes my original issue more mysterious.

if I use either of these, i still get Failed asserting that null matches expected 1.

$this->lcStep = $co->last_completed_step ?: 1; // Will be set to 1
$this->lcStep = $co->last_completed_step ?? 1; // Will be set to 1
1 like
guybrush_threepwood's avatar

You're welcome!

Yes, it quite baffling.

Could you paste the complete CompanyCustomerAgreement factory code?

Thanks.

PS: Edited due to misunderstanding on my part

jgravois's avatar
<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\CompanyCustomerAgreement;

class CompanyCustomerAgreementFactory extends Factory
{
    protected $model = CompanyCustomerAgreement::class;

    public function definition()
    {
        return [
            'company_id' => 1,
            'requester_id' => 1,
            'status_id' => 1,
            'company_code' => 'UAMI2',
            'last_agreement' => '2020',
            'last_completed_step' => 1,
            'verified_id' => null,
            'verified_date' => null,
            'approved_date' => null,
            'approved_id' => null,
            'is_favorited' => null,
            'reporter' => 'Jon Gravois',
            'reporter_title' => 'Technology Manager',
            'reporter_email' => '[email protected]',
            'signer' => null,
            'signer_title' => null,
            'signer_email' => null,
            'terms_initials' => null,
            'current_caf_invited' => true,
            'uploaded_quantum' => false,
            'is_airline' => false,
            'is_lessor' => false,
            'is_mro' => false,
            'is_mro_faa' => false,
            'is_mro_easa' => false,
            'is_mro_caac' => false,
            'is_oem' => false,
            'is_supplier' => false,
            'is_trader' => false,
            'is_other' => false,
            'other_fillin' => null,
        ];
    }
}
guybrush_threepwood's avatar
Level 33

Thanks @jgravois

You have to use the create() method in order to persist your CompanyCustomerAgreement to the database:

$caf = CompanyCustomerAgreement::factory()->create([
    'code' => 'UAMI2',
    'last_completed_step' => null
]);

Please or to participate in this conversation.