public function testCache() { $theme = Theme::load('test'); $themePath = $theme->getPath(); $filePath = $themePath .= '/temporary/test.htm'; if (file_exists($filePath)) { @unlink($filePath); } $this->assertFileNotExists($filePath); file_put_contents($filePath, '<p>Test content</p>'); /* * First try - the object should be loaded from the file */ $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); $this->assertFalse($obj->isLoadedFromCache()); $this->assertEquals('<p>Test content</p>', $obj->getContent()); $this->assertEquals('test.htm', $obj->getFileName()); $this->assertEquals(filemtime($filePath), $obj->mtime); /* * Second try - the object should be loaded from the cache */ CmsObject::clearInternalCache(); $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); $this->assertTrue($obj->isLoadedFromCache()); $this->assertEquals('<p>Test content</p>', $obj->getContent()); $this->assertEquals('test.htm', $obj->getFileName()); $this->assertEquals(filemtime($filePath), $obj->mtime); /* * Modify the file. The object should be loaded from the disk and re-cached. */ sleep(1); // Sleep a second in order to have the update file modification time file_put_contents($filePath, '<p>Updated test content</p>'); clearstatcache(); // The filemtime() function caches its value within a request, so we should clear its cache. CmsObject::clearInternalCache(); $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); $this->assertFalse($obj->isLoadedFromCache()); $this->assertEquals('<p>Updated test content</p>', $obj->getContent()); $this->assertEquals(filemtime($filePath), $obj->mtime); CmsObject::clearInternalCache(); $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); $this->assertTrue($obj->isLoadedFromCache()); $this->assertEquals('<p>Updated test content</p>', $obj->getContent()); $this->assertEquals(filemtime($filePath), $obj->mtime); /* * Delete the file. The loadCached() should return null */ @unlink($filePath); $this->assertFileNotExists($filePath); CmsObject::clearInternalCache(); $obj = TestTemporaryCmsObject::loadCached($theme, 'test.htm'); $this->assertNull($obj); }
public function testCache() { $theme = Theme::load('test'); $themePath = $theme->getPath(); /* * Prepare the test file */ $srcPath = $themePath . '/testobjects/compound.htm'; $this->assertFileExists($srcPath); $testContent = file_get_contents($srcPath); $this->assertNotEmpty($testContent); $filePath = $themePath .= '/temporary/testcompound.htm'; if (file_exists($filePath)) { @unlink($filePath); } $this->assertFileNotExists($filePath); file_put_contents($filePath, $testContent); /* * Load the test object to initialize the cache */ $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm'); $this->assertFalse($obj->isLoadedFromCache()); $this->assertEquals($testContent, $obj->getContent()); $this->assertEquals('testcompound.htm', $obj->getFileName()); $this->assertEquals('<p>This is a paragraph</p>', $obj->markup); $this->assertInternalType('array', $obj->settings); $this->assertArrayHasKey('var', $obj->settings); $this->assertEquals('value', $obj->settings['var']); $this->assertArrayHasKey('components', $obj->settings); $this->assertInternalType('array', $obj->settings['components']['section']); $this->assertArrayHasKey('version', $obj->settings['components']['section']); $this->assertEquals(10, $obj->settings['components']['section']['version']); $this->assertEquals('value', $obj->var); $this->assertInternalType('array', $obj->settings['components']['section']); $this->assertArrayHasKey('version', $obj->settings['components']['section']); $this->assertEquals(10, $obj->settings['components']['section']['version']); /* * Load the test object again, it should be loaded from the cache this time */ CmsObject::clearInternalCache(); $obj = TestTemporaryCmsCompoundObject::loadCached($theme, 'testcompound.htm'); $this->assertTrue($obj->isLoadedFromCache()); $this->assertEquals($testContent, $obj->getContent()); $this->assertEquals('testcompound.htm', $obj->getFileName()); $this->assertEquals('<p>This is a paragraph</p>', $obj->markup); $this->assertInternalType('array', $obj->settings); $this->assertArrayHasKey('var', $obj->settings); $this->assertEquals('value', $obj->settings['var']); $this->assertArrayHasKey('components', $obj->settings); $this->assertInternalType('array', $obj->settings['components']['section']); $this->assertArrayHasKey('version', $obj->settings['components']['section']); $this->assertEquals(10, $obj->settings['components']['section']['version']); $this->assertEquals('value', $obj->var); $this->assertInternalType('array', $obj->settings['components']['section']); $this->assertArrayHasKey('version', $obj->settings['components']['section']); $this->assertEquals(10, $obj->settings['components']['section']['version']); }