Skip to content

Commit

Permalink
feat: aad refersh method on legacy model
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielgomes94 committed Oct 18, 2023
1 parent 012fe23 commit 3412f2d
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/LegacyRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -399,4 +399,13 @@ public function bsonUnserialize(array $data): void

$this->syncOriginalDocumentAttributes();
}

/**
* Query model on database to retrieve an updated version of its attributes.
* @return self
*/
public function refresh(): self
{
return $this->first($this->_id);
}
}
22 changes: 22 additions & 0 deletions tests/Integration/LegacyRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,26 @@ public function testShouldOverrideSetAttributeMethods(): void

$this->assertSame($expected, $entity->getAttributes());
}

public function testRefreshModel(): void
{
// Set
$entity = new LegacyRecordUser();
$entity->dynamic = true;
$entity->fill([
'name' => 'John Doe',
]);
$entity->save();

// Actions
$entity->name = 'Jane Doe';
$entity = $entity->refresh();

// Assertions
/**
* In this test, User must have his old name after refresh because its model wasn't persisted after setting its name to Jane Doe.
*/
$this->assertInstanceOf(LegacyRecordUser::class, $entity);
$this->assertSame('John Doe', $entity->name);
}
}
27 changes: 27 additions & 0 deletions tests/Unit/LegacyRecordTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,4 +437,31 @@ public function testShouldGetSetWriteConcernInLegacyRecordClass(): void
$this->entity->setWriteConcern(0);
$this->assertEquals(0, $this->entity->getWriteConcern());
}

public function testShouldRefreshModels(): void
{
// Set
$id = 'some-id-value';
$entity = m::mock(LegacyRecord::class.'[getDataMapper]');
$this->setProtected($entity, 'collection', 'mongolid');
$entity->_id = $id;
$dataMapper = m::mock();
Container::instance(get_class($entity), $entity);

// Expectations
$entity->shouldReceive('getDataMapper')
->andReturn($dataMapper);

$dataMapper->shouldReceive('first')
->once()
->with('some-id-value', [], false)
->andReturn($entity);

// Actions
$result = $entity->refresh();

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

}

0 comments on commit 3412f2d

Please sign in to comment.