Skip to content

Commit fa98f9a

Browse files
committed
Add flags for persist data w/ db.
1 parent 0a15097 commit fa98f9a

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/NomadicMigration.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,29 @@ public function setProperty($key, $value)
5252

5353
/**
5454
* Syncs up w/ the database and returns the property.
55-
* @param string $key The column name of the migration.
55+
* @param string $key The column name of the migration.
56+
* @param bool $persist By default, we get the property from the db.
5657
* @return mixed
5758
*/
58-
public function getProperty($key)
59+
public function getProperty($key, $persist = true)
5960
{
60-
$this->syncWithDb();
61+
if ($persist) {
62+
$this->syncWithDb();
63+
}
6164
return $this->properties[$key];
6265
}
6366

6467
/**
6568
* Returns all properties of the migration.
66-
* @return array
69+
* @param bool $persist Whether to sync with the db first.
70+
* @return mixed
6771
*/
68-
public function getProperties()
72+
public function getProperties($persist = false)
6973
{
70-
$this->syncWithDb();
71-
return $this->properties;
74+
if ($persist) {
75+
$this->syncWithDb();
76+
}
77+
return $this->properties;
7278
}
7379

7480
/**

tests/NomadicMigrationTest.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ public function setUp()
1717

1818
$this->repo->expects($this->once())
1919
->method('getProperties')
20-
->willReturn($this->mockProperties());
20+
->withAnyParameters()
21+
->willReturn($this->mockDbProperties());
2122

2223
$this->migration = $this->getMockForAbstractClass(
2324
NomadicMigration::class,
@@ -29,12 +30,14 @@ public function setUp()
2930
public function testSetAndGetProperty()
3031
{
3132
$this->migration->setProperty('author', 'Chris');
32-
$this->assertEquals('Chris', $this->migration->getProperty('author'));
33+
$this->assertEquals('Chris', $this->migration->getProperty('author', false));
34+
$this->assertEquals('DB Chris', $this->migration->getProperty('author'));
3335
}
3436

3537
public function testGetProperties()
3638
{
37-
$this->assertEquals($this->mockProperties(), $this->migration->getProperties());
39+
$this->assertEquals([], $this->migration->getProperties());
40+
$this->assertEquals($this->mockDbProperties(), $this->migration->getProperties(true));
3841
}
3942

4043
public function tearDown()
@@ -43,12 +46,12 @@ public function tearDown()
4346
unset($this->migration);
4447
}
4548

46-
protected function mockProperties()
49+
protected function mockDbProperties()
4750
{
4851
return [
4952
'migration' => '2018_04_21_Migration',
5053
'batch' => 1,
51-
'author' => 'Chris'
54+
'author' => 'DB Chris'
5255
];
5356
}
5457
}

tests/files/2018_04_04_000000_NomadicMockMigration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use ChrisHalbert\LaravelNomadic\NomadicMigration;
44

55
class NomadicMockMigration extends NomadicMigration {
6-
public function getProperties()
6+
public function getProperties($syncWithDb = false)
77
{
88
return ['property' => 'value'];
99
}

0 commit comments

Comments
 (0)