@@ -33,5 +33,41 @@ public function testCacheRead(): void
3333 {
3434 $ watermeter = new Cache ();
3535 $ this ->assertEquals ("1189.2345 " , $ watermeter ->getValue ());
36+ $ this ->assertIsNumeric ($ watermeter ->getLastUpdate ());
37+ $ this ->assertGreaterThan (0 , $ watermeter ->getLastUpdate ());
38+ }
39+
40+ public function testCacheUpdate (): void
41+ {
42+ $ cacheFile = __DIR__ . '/../src/config/lastValue.txt ' ;
43+ $ backupFile = $ cacheFile . '.bak.test ' ;
44+ copy ($ cacheFile , $ backupFile );
45+
46+ try {
47+ file_put_contents ($ cacheFile , "1234.5678 " );
48+ touch ($ cacheFile , time () - 100 );
49+
50+ $ cache = new Cache ();
51+ $ this ->assertEquals ("1234.5678 " , $ cache ->getValue ());
52+ $ this ->assertEquals (time () - 100 , $ cache ->getLastUpdate (), '' , 2 );
53+ } finally {
54+ rename ($ backupFile , $ cacheFile );
55+ }
56+ }
57+
58+ public function testCacheNonExistent (): void
59+ {
60+ $ cacheFile = __DIR__ . '/../src/config/lastValue.txt ' ;
61+ $ backupFile = $ cacheFile . '.bak.test ' ;
62+ copy ($ cacheFile , $ backupFile );
63+ unlink ($ cacheFile );
64+
65+ try {
66+ $ cache = new Cache ();
67+ $ this ->assertEquals (0 , $ cache ->getValue ());
68+ $ this ->assertEquals (0 , $ cache ->getLastUpdate ());
69+ } finally {
70+ rename ($ backupFile , $ cacheFile );
71+ }
3672 }
3773}
0 commit comments