public function getContentObject($contenttype, $values = []) { // Make sure $contenttype is an array, and not just the slug. if (!is_array($contenttype)) { $contenttype = $this->getContentType($contenttype); } // If the contenttype has a 'class' specified, and the class exists, // Initialize the content as an object of that class. if (!empty($contenttype['class']) && class_exists($contenttype['class'])) { $content = new $contenttype['class']($this->app, $contenttype, $values); // Check if the class actually extends \Bolt\Legacy\Content. if (!$content instanceof Content) { throw new \Exception($contenttype['class'] . ' does not extend \\Bolt\\Legacy\\Content.'); } } else { $content = new Content($this->app, $contenttype, $values); } return $content; }
public function testGetContentObject() { $app = $this->getApp(); $storage = new Storage($app); $content = $storage->getContentObject('pages'); $this->assertInstanceOf('Bolt\\Legacy\\Content', $content); $fields = $app['config']->get('contenttypes/pages/fields'); $mock = $this->getMock('Bolt\\Legacy\\Content', null, [$app], 'Pages'); $content = $storage->getContentObject(['class' => 'Pages', 'fields' => $fields]); $this->assertInstanceOf('Pages', $content); $this->assertInstanceOf('Bolt\\Legacy\\Content', $content); // Test that a class not instanceof Bolt\Legacy\Content fails $mock = $this->getMock('stdClass', null, [], 'Failing'); $this->setExpectedException('Exception', 'Failing does not extend \\Bolt\\Legacy\\Content.'); $content = $storage->getContentObject(['class' => 'Failing', 'fields' => $fields]); }