From 34000bdada2b2e37b8c6e4ad87b38024a76b8b3c Mon Sep 17 00:00:00 2001 From: JoaoFerrazfs Date: Wed, 2 Oct 2024 10:42:25 -0300 Subject: [PATCH] refactor: changed interfaces and tests --- src/Model/HasAttributesInterface.php | 4 +-- tests/Stubs/EmbeddedUser.php | 12 +++------ tests/Stubs/PolymorphedReferencedUser.php | 5 +--- tests/Stubs/Price.php | 5 +--- tests/Stubs/Product.php | 5 +--- tests/Stubs/ReferencedUser.php | 27 +++++++------------ tests/Stubs/ReplaceCollectionModel.php | 15 +++-------- tests/Unit/Model/AbstractModelTest.php | 22 ++++----------- tests/Unit/Model/HasAttributesTraitTest.php | 7 ++--- .../Unit/Model/HasLegacyRelationTraitTest.php | 5 ++-- tests/Unit/Query/BuilderTest.php | 10 ++----- 11 files changed, 32 insertions(+), 85 deletions(-) diff --git a/src/Model/HasAttributesInterface.php b/src/Model/HasAttributesInterface.php index 1b166ba3..6411b6a5 100644 --- a/src/Model/HasAttributesInterface.php +++ b/src/Model/HasAttributesInterface.php @@ -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 @@ -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. diff --git a/tests/Stubs/EmbeddedUser.php b/tests/Stubs/EmbeddedUser.php index 0d82cef4..e6fb6d56 100644 --- a/tests/Stubs/EmbeddedUser.php +++ b/tests/Stubs/EmbeddedUser.php @@ -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() { diff --git a/tests/Stubs/PolymorphedReferencedUser.php b/tests/Stubs/PolymorphedReferencedUser.php index d6a0c8a0..e3871493 100644 --- a/tests/Stubs/PolymorphedReferencedUser.php +++ b/tests/Stubs/PolymorphedReferencedUser.php @@ -3,10 +3,7 @@ class PolymorphedReferencedUser extends ReferencedUser { - /** - * {@inheritdoc} - */ - protected $fillable = [ + protected array $fillable = [ 'type', 'new_field', ]; diff --git a/tests/Stubs/Price.php b/tests/Stubs/Price.php index e079222a..c44aec57 100644 --- a/tests/Stubs/Price.php +++ b/tests/Stubs/Price.php @@ -5,8 +5,5 @@ class Price extends AbstractModel { - /** - * @var string - */ - protected $collection = 'prices'; + protected ?string $collection = 'prices'; } diff --git a/tests/Stubs/Product.php b/tests/Stubs/Product.php index f905b581..96b5b8c8 100644 --- a/tests/Stubs/Product.php +++ b/tests/Stubs/Product.php @@ -5,8 +5,5 @@ class Product extends AbstractModel { - /** - * @var string - */ - protected $collection = 'products'; + protected ?string $collection = 'products'; } diff --git a/tests/Stubs/ReferencedUser.php b/tests/Stubs/ReferencedUser.php index 71cc5a04..ad693a7f 100644 --- a/tests/Stubs/ReferencedUser.php +++ b/tests/Stubs/ReferencedUser.php @@ -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!'; } diff --git a/tests/Stubs/ReplaceCollectionModel.php b/tests/Stubs/ReplaceCollectionModel.php index c40ecf2a..9245b3d0 100644 --- a/tests/Stubs/ReplaceCollectionModel.php +++ b/tests/Stubs/ReplaceCollectionModel.php @@ -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 { diff --git a/tests/Unit/Model/AbstractModelTest.php b/tests/Unit/Model/AbstractModelTest.php index 4eb4b280..a6e2a71f 100644 --- a/tests/Unit/Model/AbstractModelTest.php +++ b/tests/Unit/Model/AbstractModelTest.php @@ -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); } @@ -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 { @@ -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 { @@ -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 { diff --git a/tests/Unit/Model/HasAttributesTraitTest.php b/tests/Unit/Model/HasAttributesTraitTest.php index 87955de4..29faa979 100644 --- a/tests/Unit/Model/HasAttributesTraitTest.php +++ b/tests/Unit/Model/HasAttributesTraitTest.php @@ -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'; } diff --git a/tests/Unit/Model/HasLegacyRelationTraitTest.php b/tests/Unit/Model/HasLegacyRelationTraitTest.php index c02c5725..0ccd73ef 100644 --- a/tests/Unit/Model/HasLegacyRelationTraitTest.php +++ b/tests/Unit/Model/HasLegacyRelationTraitTest.php @@ -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, @@ -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); } /** diff --git a/tests/Unit/Query/BuilderTest.php b/tests/Unit/Query/BuilderTest.php index 413e995a..031dd55a 100644 --- a/tests/Unit/Query/BuilderTest.php +++ b/tests/Unit/Query/BuilderTest.php @@ -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();