baopham / laravel-dynamodb

Eloquent syntax for DynamoDB
https://packagist.org/packages/baopham/dynamodb
MIT License
484 stars 128 forks source link

Laravel 11: Call to a member function getMarshaler() on null #285

Closed SarahTeoh closed 4 days ago

SarahTeoh commented 1 week ago

Describe the bug

I used this package to define my model class. When I run my unit test, I got this:

Call to a member function getMarshaler() on null

  at vendor/baopham/dynamodb/src/DynamoDbModel.php:115
    111▕     }
    112▕ 
    113▕     protected function setupDynamoDb()
    114▕     {
  ➜ 115▕         $this->marshaler = static::$dynamoDb->getMarshaler();
    116▕         $this->attributeFilter = static::$dynamoDb->getAttributeFilter();
    117▕     }
    118▕ 
    119▕     public function newCollection(array $models = [], $index = null)

I have added service provider like this in boostrap/providers.php:

 <?php

return [
    BaoPham\DynamoDb\DynamoDbServiceProvider::class,
    App\Providers\AppServiceProvider::class,
];

I tried to register the BaoPham\DynamoDb\DynamoDbServiceProvider::class, before AppServiceProvider too but couldn't work.

This is my model class:

<?php

namespace App\Models;

use BaoPham\DynamoDb\DynamoDbModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class MyModel extends DynamoDbModel
{
    use HasFactory;

    /**
     * The composite key of the table in form of ['hash_key', 'range_key'].
     *
     * @var array
     */
    protected $compositeKey = ['key', 'timestamp'];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = ['key', 'timestamp', 'value'];
}

The test that I ran:

<?php

use App\Models\MyModel;

it('can fill key, timestamp, and value attributes', function () {
    $attributes = [
        'key' => 'test-key',
        'timestamp' => time(),
        'value' => 'test-value',
    ];

    $keyValue = new KeyValue($attributes);

    expect($keyValue->key)->toBe($attributes['key']);
    expect($keyValue->timestamp)->toBe($attributes['timestamp']);
    expect($keyValue->value)->toBe($attributes['value']);
});

Schema

protected $compositeKey = ['key', 'timestamp'];

Version info

SarahTeoh commented 4 days ago

solved by adding

uses(
    Tests\TestCase::class,
)->in('Unit');

in Pest.php