/**
  * @param callable $callback
  * @param string $expectedException
  * @param null $expectedCode
  * @param null $expectedMessage
  * @author VladaHejda
  */
 public static function assertException(callable $callback, $expectedException = 'Exception', $expectedCode = null, $expectedMessage = null)
 {
     $self = new SapphireTest();
     if (!ClassInfo::exists($expectedException)) {
         $self->fail(sprintf('An exception of type "%s" does not exist.', $expectedException));
     }
     try {
         $callback();
     } catch (\Exception $e) {
         $class = ClassInfo::class_name($e);
         $message = $e->getMessage();
         $code = $e->getCode();
         $errorMessage = 'Failed asserting the class of exception';
         if ($message && $code) {
             $errorMessage .= sprintf(' (message was %s, code was %d)', $message, $code);
         } elseif ($code) {
             $errorMessage .= sprintf(' (code was %d)', $code);
         }
         $errorMessage .= '.';
         $self->assertInstanceOf($expectedException, $e, $errorMessage);
         if ($expectedCode !== null) {
             $self->assertEquals($expectedCode, $code, sprintf('Failed asserting code of thrown %s.', $class));
         }
         if ($expectedMessage !== null) {
             $self->assertContains($expectedMessage, $message, sprintf('Failed asserting the message of thrown %s.', $class));
         }
         return;
     }
     $errorMessage = 'Failed asserting that exception';
     if (strtolower($expectedException) !== 'exception') {
         $errorMessage .= sprintf(' of type %s', $expectedException);
     }
     $errorMessage .= ' was thrown.';
     $self->fail($errorMessage);
 }
Ejemplo n.º 2
0
 public function testClassName()
 {
     $this->assertEquals('ClassInfoTest', ClassInfo::class_name($this));
     $this->assertEquals('ClassInfoTest', ClassInfo::class_name('ClassInfoTest'));
     $this->assertEquals('ClassInfoTest', ClassInfo::class_name('CLaSsInfOTEsT'));
     // This is for backwards compatiblity and will be removed in 4.0
     $this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist'));
 }
 /**
  * Returns true if given class has its own table. Uses the rules for whether the table should exist rather than
  * actually looking in the database.
  *
  * @param string $dataClass
  * @return bool
  */
 public static function has_own_table($dataClass)
 {
     if (!is_subclass_of($dataClass, 'DataObject')) {
         return false;
     }
     $dataClass = ClassInfo::class_name($dataClass);
     if (!isset(DataObject::$cache_has_own_table[$dataClass])) {
         if (get_parent_class($dataClass) == 'DataObject') {
             DataObject::$cache_has_own_table[$dataClass] = true;
         } else {
             DataObject::$cache_has_own_table[$dataClass] = Config::inst()->get($dataClass, 'db', Config::UNINHERITED) || Config::inst()->get($dataClass, 'has_one', Config::UNINHERITED);
         }
     }
     return DataObject::$cache_has_own_table[$dataClass];
 }
 /**
  * Safely get a DataObject from a client-supplied ID and ClassName, checking: argument
  * validity; existence; and canView permissions.
  *
  * @param int $id The ID of the DataObject
  * @param string $class The Class of the DataObject
  * @return DataObject The referenced DataObject
  * @throws SS_HTTPResponse_Exception
  */
 protected function getObject($id, $class)
 {
     $id = (int) $id;
     $class = ClassInfo::class_name($class);
     if (!$class || !is_subclass_of($class, 'SilverStripe\\ORM\\DataObject') || !Object::has_extension($class, 'SilverStripe\\ORM\\Versioning\\Versioned')) {
         $this->editForm->httpError(400, _t('AddToCampaign.ErrorGeneral', 'We apologise, but there was an error'));
         return null;
     }
     $object = DataObject::get($class)->byID($id);
     if (!$object) {
         $this->editForm->httpError(404, _t('AddToCampaign.ErrorNotFound', 'That {Type} couldn\'t be found', '', ['Type' => $class]));
         return null;
     }
     if (!$object->canView()) {
         $this->editForm->httpError(403, _t('AddToCampaign.ErrorItemPermissionDenied', 'It seems you don\'t have the necessary permissions to add {ObjectTitle} to a campaign', '', ['ObjectTitle' => $object->Title]));
         return null;
     }
     return $object;
 }
 public function testNonClassName()
 {
     $this->setExpectedException('ReflectionException', 'Class IAmAClassThatDoesNotExist does not exist');
     $this->assertEquals('IAmAClassThatDoesNotExist', ClassInfo::class_name('IAmAClassThatDoesNotExist'));
 }
 public function testGetCache()
 {
     $cache = CacheHelper::get_cache();
     $this->assertEquals(ClassInfo::class_name($cache), 'Zend_Cache_Frontend_Function');
 }
 /**
  * Gets all extensions directly on this class that extend FluentExtension
  *
  * @param Config $config
  * @param string $class
  * @return array
  */
 protected function getDirectExtensions($config, $class)
 {
     $extensions = $config->get($class, 'extensions', Config::UNINHERITED);
     $found = array();
     if ($extensions) {
         foreach ($extensions as $extension) {
             $extensionClass = ClassInfo::class_name(Extension::get_classname_without_arguments($extension));
             if ($extensionClass === 'FluentExtension' || is_subclass_of($extensionClass, 'FluentExtension')) {
                 $found[] = $extensionClass;
             }
         }
     }
     return $found;
 }