Bloomanity's avatar

How can I test that a json is correctly stored?

/** @test */
    public function it_successfuly_registers_for_a_delivery()
    {
        list($user, $program) = $this->arrangeTheWorld();

        $address = 'Full Address';

        $this->visit('program/register')
             ->type($user->first_name, 'first_name')
             ->type($user->last_name, 'last_name')
             ->type($user->email, 'email')
             ->type($user->phone, 'phone')
             ->select($program->id, 'program_id')
             ->select('deliver', 'shipping_method')
             ->type($address, 'shipping_address')
             ->press('Register')
             ->see('Congrats ' . $user->first_name);

        $this->seeInDatabase('users', $user->toArray());

        $this->checkAttendees($program, [
            'shipping_method' => 'deliver',
            'shipping_address' => $address
        ]);

        $this->checkInvoices($program);
    }

/**
     * @return array
     */
    protected function arrangeTheWorld()
    {
        $user = factory(User::class)->make();

        $programs = factory(Program::class, 10)->create([
            'name' => 'Detox5',
            'type' => 'detox5',
            'price' => 65000
        ]);

        $program = $programs->filter(function($program) {
            return $program->start_time > Carbon::now()->addDays(2);
        })[0];

        return [$user, $program];
    }

/**
     * Check the attendees table
     *
     * @param  Program $program
     * @param  array   $extra
     * @return void
     */
    protected function checkAttendees($program, $extra)
    {
        $this->seeInDatabase('attendees', [
            'user_id' => 1,
            'program_id' => $program->id,
            'program_type' => $program->type,
            // 'extra' => json_encode($extra)
        ]);
    }

If I check for an array in the DB or json encode it it still doesn't return a green test. With it commented out it gives me a green test.

0 likes
4 replies
Flambe's avatar

How does the data actually get stored in the DB? It may not be stored as an array or json object.

Bloomanity's avatar

It's json encoded from an array.

I have the extra attribute casted

/**
     * @var array
     */
    protected $casts = [
        'extra' => 'array'
    ];
Bloomanity's avatar

I found the culprit. The extra array had 2 other empty valued keys in there.

My question now is... how can I test that I have in that array the keys and values I want, without checking for the empty ones as well.

the call is now

$this->checkAttendees($program, [
            'shipping_method' => 'pickup',
            'shipping_address' => '',
            'extra_details' => ''
        ]);
Flambe's avatar

An easy way would be to merge the empty fields in if they aren't passed through. (I haven't tested this code, may need a few changes)

protected function checkAttendees($program, $extra)
{
    $merged = array_merge([
        'shipping_address' => '',
        'extra_details' => '',
    ], $extra);

    $this->seeInDatabase('attendees', [
        'user_id' => 1,
        'program_id' => $program->id,
        'program_type' => $program->type,
        'extra' => json_encode($merged)
    ]);
}

Please or to participate in this conversation.