Skip to content

Commit

Permalink
Properly fix version check helper, add tests for it
Browse files Browse the repository at this point in the history
  • Loading branch information
szeber committed Nov 7, 2019
1 parent ec172f9 commit c90ae1c
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ShoppinPal/Vend/DataObject/Entity/V2/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@ public function hasNewerVersion(string $entityType, ?int $lastRetrievedVersion,
throw new ParameterException('Invalid entity type: ' . $entityType);
}

return $lastRetrievedVersion > max($minimumVersion - 1, (int)$lastRetrievedVersion);
return $this->$entityType > max($minimumVersion - 1, (int)$lastRetrievedVersion);
}
}
74 changes: 74 additions & 0 deletions test/ShoppinPal/Vend/DataObject/Entity/V2/VersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace ShoppinPal\Vend\DataObject\Entity\V2;

use YapepBase\Exception\ParameterException;

class VersionTest extends \PHPUnit_Framework_TestCase
{
public function testHasNewerVersionWithNotSetValue(): void
{
$version = new Version(['outlets' => null]);

$this->assertFalse($version->hasNewerVersion('outlets', null));
}

public function testHasNewerVersionWithValueOverride(): void
{
$version = new Version(['outlets' => 20]);

$this->assertFalse($version->hasNewerVersion('outlets', null, 21));
$this->assertTrue($version->hasNewerVersion('outlets', null, 20));
}

public function testHasNewerVersionWithInvalidEntity(): void
{
$this->expectException(ParameterException::class);

$version = new Version(['outlets' => null]);

$version->hasNewerVersion('invalid', null);
}

/**
* @dataProvider hasNewerVersionDataProvider
*/
public function testHasNewerVersionVersionChecks(
string $entityType,
int $currentVersion,
?int $lastRetrievedVersion,
bool $expectedResult,
string $description
): void
{
$version = new Version(
[
$entityType => $currentVersion
],
Version::UNKNOWN_PROPERTY_THROW,
true
);

$this->assertSame(
$expectedResult,
$version->hasNewerVersion($entityType, $lastRetrievedVersion),
'Invalid result for test with ' . $description
);
}

public function hasNewerVersionDataProvider(): array
{
return [
// Old behaviour for no entity
['brands', 0, null, false, 'old behaviour for no entity for retailer'],
// New behaviour for no entity
['outlets', 1, null, false, 'new behaviour for no entity for retailer'],
['outlets', 9, null, false, 'just below default ignored minimum version'],
['outlets', 10, null, true, 'at default ignored minimum version'],
['outlets', 5000, null, true, 'no synced entity with NULL'],
['outlets', 5000, 0, true, 'no synced entity with 0'],
['outlets', 5000, 4999, true, 'synced entity with lover version'],
['outlets', 5000, 5000, false, 'fully synchronised'],
];
}
}

0 comments on commit c90ae1c

Please sign in to comment.