Skip to content

Commit

Permalink
Use cache version as string instead of number
Browse files Browse the repository at this point in the history
This helps to prevent PHP from encoding the version as a high precision
floating point number in the cache file, e.g. 1.399999999999 instead of
1.4.
  • Loading branch information
alexander-nitsche committed Apr 20, 2023
1 parent 2e6ddc6 commit f3841ad
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Syllable.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Syllable
/**
* Version string, used to recalculate language caches if needed.
*/
const CACHE_VERSION = 1.4;
const CACHE_VERSION = '1.4';

/**
* @var Cache
Expand Down
33 changes: 30 additions & 3 deletions tests/src/SyllableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public function dataCache()
'json',
'json/syllable.en-us.json',
'{'.
'"version":1.4,'.
'"version":"1.4",'.
'"patterns":{%a},'.
'"max_pattern":9,'.
'"hyphenation":{%a},'.
Expand All @@ -131,7 +131,7 @@ public function dataCache()
'serialized',
'serialized/syllable.en-us.serialized',
'a:6:{'.
's:7:"version";d:1.4;'.
's:7:"version";s:3:"1.4";'.
's:8:"patterns";a:4939:{%a}'.
's:11:"max_pattern";i:9;'.
's:11:"hyphenation";a:15:{%a}'.
Expand Down Expand Up @@ -169,14 +169,41 @@ public function testCache($cacheClass, $language, $cacheDirectory, $cacheFile, $

$this->assertFileExists($cacheFile);
$this->assertStringMatchesFormat($expectedCacheContent, file_get_contents($cacheFile));
$this->assertSame(1.4, $this->object->getCache()->__get('version'));
$this->assertSame('1.4', $this->object->getCache()->__get('version'));
$this->assertNotEmpty($this->object->getCache()->__get('patterns'));
$this->assertGreaterThan(0, $this->object->getCache()->__get('max_pattern'));
$this->assertNotEmpty($this->object->getCache()->__get('hyphenation'));
$this->assertInternalType('int', $this->object->getCache()->__get('left_min_hyphen'));
$this->assertInternalType('int', $this->object->getCache()->__get('right_min_hyphen'));
}

public function dataCacheVersionMatchesCacheFileVersionIsRelaxed()
{
return [
'Cache file version is float.' => [1.4],
'Cache file version is string.' => ['1.4'],
'Cache file version is float with unexpected precision.' => [1.3999999999999999],
'Cache file version is string with unexpected precision.' => ['1.3999999999999999'],
];
}

/**
* Some PHP versions have the Syllable cache version number 1.4 encoded in 1.39999999999999
* instead of 1.4 in the cache files. To fix this, the internal representation of
* the cache version is changed from float to string. This test shows that the user's existing
* cache files are still valid after this change and do not need to be recreated.
*
* @dataProvider dataCacheVersionMatchesCacheFileVersionIsRelaxed
*
* @return void
*/
public function testCacheVersionMatchesCacheFileVersionIsRelaxed($cacheFileVersion)
{
$cacheVersion = '1.4';

$this->assertTrue($cacheVersion == $cacheFileVersion);
}

/**
* @todo Implement testSetSource().
*/
Expand Down

0 comments on commit f3841ad

Please sign in to comment.