Source of file TestCase.php
Size: 3,978 Bytes - Last Modified: 2021-12-23T10:59:36+00:00
/var/www/docs.ssmods.com/process/src/tests/CropperTest/TestCase.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 | <?php namespace CropperTest; /** * TestCase * An abstract base from which Cropper tests can extend with some useful * helper functionality. */ use CropperField\CropperField; use Config; use i18n; use Form; use Controller; use FieldList; abstract class TestCase extends \SapphireTest { protected $extraDataObjects = array( 'CropObject', ); protected $cropObject; public function setUpOnce() { i18n::set_locale('en_GB'); Config::nest(); // Stop updating the file system, it's not helpful and causes issues with // the automatic fixture image loader Config::inst()->update('File', 'update_filesystem', false); parent::setUpOnce(); } public function tearDownOnce() { parent::tearDownOnce(); Config::unnest(); } public function setUp() { parent::setUp(); $this->setCropObject(); } /** * @return \CropperField\CropperField */ protected function createField() { return $this->getCropObject()->getCMSFields()->fieldByName('ThumbnailImage'); } /** * @return Form */ protected function createForm() { return new Form( new Controller(), 'TestForm', $this->getCropObject()->getCMSFields(), new FieldList() ); } /** * @param \CropObject $object (or null to clear) */ protected function setCropObject(\CropObject $object = null) { $this->cropObject = $object; return $this; } /** * @return \CropObject */ protected function getCropObject() { return $this->cropObject ?: singleton('CropObject'); } /** * Like objFromFixture but does some CropObject sugar for us * @return \CropObject */ protected function cropObjFromFixture($fixtureName) { $cropObject = $this->objFromFixture('CropObject', $fixtureName); $this->setCropObject($cropObject); return $cropObject; } /** * @return array */ protected function sampleCropData() { return array( 'x' => 30, 'y' => 60, 'width' => 100, 'height' => 200, ); } /** * The name of the test class, relative to the asset directory. * @return string */ protected function getClassTestDir() { $refClass = new \ReflectionClass(get_class($this)); return $refClass->getShortName(); } /** * The fully qualified test asset directory. * @return string */ protected function getTestAssetDir() { return ASSETS_DIR . DIRECTORY_SEPARATOR . $this->getClassTestDir() . '/cropperfield/tests/TestAssets'; } /** * Copy the file assets from the fixture distribution folder into assets/. */ protected function prepareFileAssets($fixtureClass = 'Image') { $fileIDs = $this->allFixtureIDs($fixtureClass); $sourceDir = CROPPERFIELD_PATH . '/tests/TestAssets'; $absPath = $this->getTestAssetDir(); $classTestDir = $this->getClassTestDir(); if(!file_exists($absPath)) { mkdir($absPath, 0777, true); } foreach($fileIDs as $fileID) { $file = $fixtureClass::get()->byId($fileID); if($file->Name == 'BROKENIMAGE') { continue; } $target = ASSETS_PATH.'/'.$classTestDir.'/'.$file->Filename; $source = $file->Filename; if(!copy($source, $target)) { throw new \LogicException( 'Could not copy fixture file' ); } $file->Filename = ASSETS_DIR.'/'.$classTestDir.'/'.$file->Filename; $file->write(); } } /** * Clean up the directory afterwards */ protected function cleanFileAssets($fixtureClass = 'Image') { $fileIDs = $this->allFixtureIDs($fixtureClass); foreach($fileIDs as $fileID) { $file = $fixtureClass::get()->byId($fileID); if(!$file || $file->Name == 'BROKENIMAGE') { continue; } if(!unlink($file->Filename)) { throw new \LogicException( 'Could not remove fixture file' ); } } static::rm_rf($this->getTestAssetDir()); } /** * PHP.net comments are the best */ private static function rm_rf($dir) { $files = array_diff(scandir($dir), array('.','..')); foreach ($files as $file) { (is_dir("$dir/$file")) ? static::rm_rf("$dir/$file") : unlink("$dir/$file"); } return rmdir($dir); } } |