Skip to content

Commit

Permalink
refactor: changed interfaces and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoaoFerrazfs committed Oct 2, 2024
1 parent c494dbd commit 34000bd
Show file tree
Hide file tree
Showing 11 changed files with 32 additions and 85 deletions.
4 changes: 2 additions & 2 deletions src/Model/HasAttributesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function cleanDocumentAttribute(string $key): void;
* @param string $key name of the attribute to be set
* @param mixed $value value to be set
*/
public function setDocumentAttribute(string $key, mixed $value): mixed;
public function setDocumentAttribute(string $key, mixed $value): void;

/**
* Stores original attributes from actual data from attributes
Expand All @@ -52,7 +52,7 @@ public function setDocumentAttribute(string $key, mixed $value): mixed;
* Ideally should be called once right after retrieving data from
* the database.
*/
public function syncOriginalDocumentAttributes(): mixed;
public function syncOriginalDocumentAttributes(): void;

/**
* Retrieve original attributes.
Expand Down
12 changes: 3 additions & 9 deletions tests/Stubs/EmbeddedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@

class EmbeddedUser extends AbstractModel
{
/**
* @var string
*/
protected $collection = 'users';

/**
* @var bool
*/
protected $timestamps = true;
protected ?string $collection = 'users';

protected bool $timestamps = true;

public function parent()
{
Expand Down
5 changes: 1 addition & 4 deletions tests/Stubs/PolymorphedReferencedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@

class PolymorphedReferencedUser extends ReferencedUser
{
/**
* {@inheritdoc}
*/
protected $fillable = [
protected array $fillable = [
'type',
'new_field',
];
Expand Down
5 changes: 1 addition & 4 deletions tests/Stubs/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@

class Price extends AbstractModel
{
/**
* @var string
*/
protected $collection = 'prices';
protected ?string $collection = 'prices';
}
5 changes: 1 addition & 4 deletions tests/Stubs/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@

class Product extends AbstractModel
{
/**
* @var string
*/
protected $collection = 'products';
protected ?string $collection = 'products';
}
27 changes: 10 additions & 17 deletions tests/Stubs/ReferencedUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,42 @@

use Mongolid\Model\AbstractModel;
use Mongolid\Model\PolymorphableModelInterface;
use Mongolid\Model\Relations\ReferencesMany;
use Mongolid\Model\Relations\ReferencesOne;

class ReferencedUser extends AbstractModel implements PolymorphableModelInterface
{
/**
* @var string
*/
protected $collection = 'users';
protected ?string $collection = 'users';

/**
* @var bool
*/
protected $timestamps = false;
protected bool $timestamps = false;

/**
* {@inheritdoc}
*/
protected $fillable = [
protected array $fillable = [
'type',
'exclusive',
'other_exclusive',
];

public function parent()
public function parent(): ReferencesOne
{
return $this->referencesOne(ReferencedUser::class);
}

public function siblings()
public function siblings(): ReferencesMany
{
return $this->referencesMany(ReferencedUser::class);
}

public function son()
public function son(): ReferencesOne
{
return $this->referencesOne(ReferencedUser::class, 'arbitrary_field', 'code');
}

public function grandsons()
public function grandsons(): ReferencesMany
{
return $this->referencesMany(ReferencedUser::class, null, 'code');
}

public function invalid()
public function invalid(): string
{
return 'I am not a relation!';
}
Expand Down
15 changes: 3 additions & 12 deletions tests/Stubs/ReplaceCollectionModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@

class ReplaceCollectionModel extends AbstractModel
{
/**
* {@inheritdoc}
*/
protected $timestamps = false;
protected bool $timestamps = false;

/**
* {@inheritdoc}
*/
protected $collection = 'models';
protected ?string $collection = 'models';

/**
* @var Collection
*/
protected $rawCollection;
protected Collection $rawCollection;

public function setCollection(Collection $collection): void
{
Expand Down
22 changes: 5 additions & 17 deletions tests/Unit/Model/AbstractModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ protected function setUp(): void
parent::setUp();
$this->model = new class() extends AbstractModel
{
/**
* {@inheritdoc}
*/
protected $collection = 'mongolid';
protected ?string $collection = 'mongolid';

public function unsetCollection()
public function unsetCollection(): void
{
unset($this->collection);
}
Expand Down Expand Up @@ -482,10 +479,7 @@ public function testShouldCheckIfMutatedAttributeIsSet(): void
// Set
$model = new class() extends AbstractModel
{
/**
* {@inheritdoc}
*/
public $mutable = true;
public bool $mutable = true;

public function getNameDocumentAttribute(): string
{
Expand Down Expand Up @@ -524,10 +518,7 @@ public function testShouldGetAttributeFromMutator(): void
// Set
$model = new class() extends AbstractModel
{
/**
* {@inheritdoc}
*/
public $mutable = true;
public bool $mutable = true;

public function getShortNameDocumentAttribute(): string
{
Expand Down Expand Up @@ -571,10 +562,7 @@ public function testShouldSetAttributeFromMutator(): void
// Set
$model = new class() extends AbstractModel
{
/**
* {@inheritdoc}
*/
protected $mutable = true;
protected bool $mutable = true;

public function setShortNameDocumentAttribute($value): string
{
Expand Down
7 changes: 2 additions & 5 deletions tests/Unit/Model/HasAttributesTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,9 @@ public function testShouldCheckIfMutatedAttributeIsSet(): void
// Set
$model = new class() extends AbstractModel
{
/**
* {@inheritdoc}
*/
public $mutable = true;
public bool $mutable = true;

public function getNameDocumentAttribute()
public function getNameDocumentAttribute(): string
{
return 'John';
}
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Model/HasLegacyRelationTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public function testReferenceOneShouldNotHitCache($fieldValue, array $expectedQu
// Set
$model = new Product();
$model->_id = $fieldValue;
$expected = new Price();
$priceModel = $this->instance(Price::class, m::mock(Price::class)->makePartial());
$cacheComponent = $this->instance(
CacheComponentInterface::class,
Expand All @@ -33,13 +32,13 @@ public function testReferenceOneShouldNotHitCache($fieldValue, array $expectedQu

$priceModel->expects('first')
->with($expectedQuery, [], true)
->andReturn($expected);
->andReturnSelf();

// Actions
$result = $model->price();

// Assertions
$this->assertSame($expected, $result);
$this->assertSame($priceModel, $result);
}

/**
Expand Down
10 changes: 2 additions & 8 deletions tests/Unit/Query/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,12 @@ public function testShouldUpdateUnsettingFields(): void

$model = new class() extends ReplaceCollectionModel
{
/**
* {@inheritdoc}
*/
public $fillable = [
public array $fillable = [
'name',
'unchanged',
];

/**
* {@inheritdoc}
*/
protected $dynamic = false;
protected bool $dynamic = false;
};
$collection = m::mock(Collection::class);
$operationResult = m::mock();
Expand Down

0 comments on commit 34000bd

Please sign in to comment.