Source of file TestContentServices.php
Size: 3,259 Bytes - Last Modified: 2021-12-23T10:29:05+00:00
/var/www/docs.ssmods.com/process/src/code/tests/TestContentServices.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 | <?php /** * * @author marcus@symbiote.com.au * @license BSD License http://silverstripe.org/bsd-license/ */ class TestContentServices extends SapphireTest { /** * @var ContentService */ protected $contentService; protected $testDir; public function setUp() { parent::setUp(); $dir = Director::baseFolder().'/testcontent'; if (file_exists($dir)) { Filesystem::removeFolder($dir); } $this->contentService = new ContentService(); // set some injector properties $injector = Injector::inst(); $injector->load(array( 'TestContentReader' => array( 'class' => 'FileContentReader', 'type' => 'prototype', 'properties' => array( 'basePath' => $dir, ) ), 'TestContentWriter' => array( 'class' => 'FileContentWriter', 'type' => 'prototype', 'properties' => array( 'basePath' => $dir, ) ) )); $this->contentService->setStores(array( 'File' => array( 'ContentReader' => 'TestContentReader', 'ContentWriter' => 'TestContentWriter', ) )); $injector->registerService($this->contentService); $this->testDir = $dir; } public function testContentWriter() { $writer = $this->contentService->getWriterFor(); $this->assertTrue($writer instanceof FileContentWriter); try { $reader = $writer->getReader(); } catch (Exception $e) { $this->assertTrue(strpos($e->getMessage(), 'Null content identifier') !== false); } } /** * * @return FileContentWriter */ protected function writeDummy() { $writer = $this->contentService->getWriterFor(); $this->assertTrue($writer instanceof FileContentWriter); $writer->write(new RawContentReader("dummy content"), 'dummy.txt'); return $writer; } public function testWriteContent() { $writer = $this->writeDummy(); $id = $writer->getContentId(); $this->assertNotNull($id); $this->assertTrue(strpos($id, 'dummy.txt') > 0); $this->assertTrue(strpos($id, 'File:') === 0); } public function testWriteFileContent() { $writer = $this->contentService->getWriterFor(); $this->assertTrue($writer instanceof FileContentWriter); $writer->write(new FileContentReader(__FILE__), 'test_file.php'); $id = $writer->getContentId(); $this->assertNotNull($id); $this->assertTrue(strpos($id, 'test_file.php') > 0); $this->assertTrue(strpos($id, 'File:') === 0); } public function testReadContent() { $writer = $this->writeDummy(); $id = $writer->getContentId(); $reader = $this->contentService->getReader($id); $this->assertNotNull($reader); $this->assertTrue($reader instanceof FileContentReader); $text = $reader->read(); $this->assertEquals('dummy content', $text); } public function testListFolder() { if (file_exists($this->testDir)) { Filesystem::removeFolder($this->testDir); } mkdir($this->testDir); file_put_contents($this->testDir.'/testfile.txt', 'dummy_data'); $reader = $this->contentService->getReader('File:||' . $this->testDir); $list = $reader->getList(); $this->assertEquals(1, count($list)); $file = $list[0]; $writer = $file->getWriter(); $writer->write('new contents'); $this->assertEquals('new contents', file_get_contents($this->testDir.'/testfile.txt')); } } |