diff --git a/core/lib/Drupal/Core/Asset/Exception/LockedObjectException.php b/core/lib/Drupal/Core/Asset/Exception/LockedObjectException.php new file mode 100644 index 0000000..192928c --- /dev/null +++ b/core/lib/Drupal/Core/Asset/Exception/LockedObjectException.php @@ -0,0 +1,14 @@ + 'Drupal\\Core\\Asset\\FileAsset', 'external' => 'Drupal\\Core\\Asset\\ExternalAsset', @@ -69,56 +95,18 @@ public function __construct(AssetCollectionInterface $collection = NULL) { } /** - * Adds an asset to the contained AssetBag. - * - * It is not necessary to call this method on assets that were created via the - * create() method. - * - * @param AssetInterface $asset - * The asset to add to the contained collection. + * {@inheritdoc} */ public function add(AssetInterface $asset) { if (empty($this->collection)) { - throw new \Exception('No collection is currently attached to this collector.'); + throw new \RuntimeException('No collection is currently attached to this collector.'); } $this->collection->add($asset); return $this; } /** - * Creates an asset, stores it in the collector's collection, and returns it. - * - * TODO flesh out these docs to be equivalent to drupal_add_css/js() - * - * @param string $asset_type - * A string indicating the asset type - 'css' or 'js'. - * @param string $source_type - * A string indicating the source type - 'file', 'external' or 'string'. - * @param string $data - * A string containing data that defines the asset. Appropriate values vary - * depending on the source_type param: - * - 'file': the relative path to the file, or a stream wrapper URI. - * - 'external': the absolute path to the external asset. - * - 'string': a string containing valid CSS or Javascript to be injected - * directly onto the page. - * @param array $options - * (optional) An array of metadata to explicitly set on the asset. These - * will override metadata defaults that are injected onto the asset at - * creation time. - * @param array $filters - * (optional) An array of filters to apply to the object - * TODO this should, maybe, be removed entirely - * @param bool $keep_last - * (optional) Whether or not to retain the created asset for automated - * ordering purposes. Only applies to CSS. Note that passing FALSE will not - * prevent a CSS asset that is being created from automatically being - * after() the existing lastCss asset, if one exists. For that, - * @see clearLastCss(). - * - * @return \Drupal\Core\Asset\AssetInterface - * - * @throws \InvalidArgumentException - * Thrown if an invalid asset type or source type is passed. + * {@inheritdoc} */ public function create($asset_type, $source_type, $data, $options = array(), $filters = array(), $keep_last = TRUE) { // TODO this normalization points to a deeper modeling problem. @@ -155,43 +143,46 @@ public function create($asset_type, $source_type, $data, $options = array(), $fi } /** - * Clears the asset stored in lastCss. - * - * Ordinarily, using the create() factory to generate a CSS asset object will - * automatically set up an ordering relationship between that asset and the - * previous CSS asset that was created. This is intended to facilitate the - * rigid ordering that authors likely expect for CSS assets declared together - * in a contiguous series. - * - * This method clears the last stored CSS asset. It should be called when the - * end of such a contiguous series is reached, or by the asset creator - * themselves if they want to avoid the creation of the ordering relationship. - * - * @return AssetCollector - * The current AssetCollector instance, for easy chaining. + * {@inheritdoc} */ public function clearLastCss() { unset($this->lastCss); return $this; } + /** + * {@inheritdoc} + */ public function setCollection(AssetCollectionInterface $collection) { if ($this->isLocked()) { - throw new \Exception('The collector instance is locked. A new collection cannot be attached to a locked collector.'); + throw new LockedObjectException('The collector instance is locked. A new collection cannot be attached to a locked collector.'); } $this->collection = $collection; } + /** + * {@inheritdoc} + */ public function clearCollection() { if ($this->isLocked()) { - throw new \Exception('The collector instance is locked. Collections cannot be cleared on a locked collector.'); + throw new LockedObjectException('The collector instance is locked. Collections cannot be cleared on a locked collector.'); } $this->collection = NULL; } + /** + * {@inheritdoc} + */ + public function hasCollection() { + return $this->collection instanceof AssetCollectionInterface; + } + + /** + * {@inheritdoc} + */ public function lock($key) { if ($this->isLocked()) { - throw new \Exception('Collector is already locked.', E_WARNING); + throw new LockedObjectException('Collector is already locked.', E_WARNING); } $this->locked = TRUE; @@ -199,13 +190,16 @@ public function lock($key) { return TRUE; } + /** + * {@inheritdoc} + */ public function unlock($key) { if (!$this->isLocked()) { - throw new \Exception('Collector is not locked', E_WARNING); + throw new LockedObjectException('Collector is not locked', E_WARNING); } if ($this->lockKey !== $key) { - throw new \Exception('Attempted to unlock Collector with incorrect key.', E_WARNING); + throw new LockedObjectException('Attempted to unlock Collector with incorrect key.', E_WARNING); } $this->locked = FALSE; @@ -213,13 +207,22 @@ public function unlock($key) { return TRUE; } + /** + * {@inheritdoc} + */ public function isLocked() { return $this->locked; } + /** + * {@inheritdoc} + * + * @throws \InvalidArgumentException + * Thrown if an invalid metadata type is provided (i.e., not 'css' or 'js'). + */ public function setDefaultMetadata(AssetMetadataBag $metadata) { if ($this->isLocked()) { - throw new \Exception('The collector instance is locked. Asset defaults cannot be modified on a locked collector.'); + throw new LockedObjectException('The collector instance is locked. Asset defaults cannot be modified on a locked collector.'); } $type = $metadata->getType(); @@ -236,19 +239,7 @@ public function setDefaultMetadata(AssetMetadataBag $metadata) { } /** - * Gets a clone of the metadata bag for a given asset type. - * - * Clones are returned in order to ensure there is a unique metadata object - * for every asset, and that the default metadata contained in the collector - * cannot be modified externally. - * - * @param $type - * A string, 'css' or 'js', indicating the type of metadata to retrieve. - * - * @return AssetMetadataBag - * - * @throws \InvalidArgumentException - * Thrown if a type other than 'css' or 'js' is provided. + * {@inheritdoc} */ public function getMetadataDefaults($type) { if ($type === 'css') { @@ -263,19 +254,14 @@ public function getMetadataDefaults($type) { } /** - * Restores metadata default bags to their default state. - * - * This simply creates new instances of CssMetadataBag and JsMetadataBag, as - * those classes have the normal defaults as hardmapped properties. - * - * @throws \Exception - * Thrown if the collector is locked when this method is called. + * {@inheritdoc} */ public function restoreDefaults() { if ($this->isLocked()) { - throw new \Exception('The collector instance is locked. Asset defaults cannot be modified on a locked collector.'); + throw new LockedObjectException('The collector instance is locked. Asset defaults cannot be modified on a locked collector.'); } $this->defaultCssMetadata = new CssMetadataBag(); $this->defaultJsMetadata = new JsMetadataBag(); } } + diff --git a/core/lib/Drupal/Core/Asset/Factory/AssetCollectorInterface.php b/core/lib/Drupal/Core/Asset/Factory/AssetCollectorInterface.php new file mode 100644 index 0000000..2c5a817 --- /dev/null +++ b/core/lib/Drupal/Core/Asset/Factory/AssetCollectorInterface.php @@ -0,0 +1,210 @@ + 'Asset aggregate tests', + 'description' => 'Unit tests on BaseAggregateAsset', + 'group' => 'Asset', + ); + } + + public function getAggregate($defaults = array()) { + $mockmeta = $this->getMockForAbstractClass('\\Drupal\\Core\\Asset\\Metadata\\AssetMetadataBag', $defaults); + $this->aggregate = $this->getMockForAbstractClass('\\Drupal\\Core\\Asset\\Aggregate\\BaseAggregateAsset'); + } + + public function testId() { + + } + + public function testGetAssetType() { + + } + + public function testGetMetadata() { + + } + + public function testContains() { + + } + + public function testGetById() { + + } + + public function testIsPreprocessable() { + + } + + public function testAll() { + + } + + public function testEnsureFilter() { + + } + + public function testGetFilters() { + + } + + public function testClearFilters() { + + } + + public function testGetContent() { + + } + + public function testSetContent() { + + } + + public function testGetSourceRoot() { + + } + + public function testGetSourcePath() { + + } + + public function testGetTargetPath() { + + } + + public function testSetTargetPath() { + + } + + public function testGetLastModified() { + + } + + public function testGetIterator() { // ?? + + } + + public function testIsEmpty() { + + } +} diff --git a/core/tests/Drupal/Tests/Core/Asset/Collection/AssetCollectionTest.php b/core/tests/Drupal/Tests/Core/Asset/Collection/AssetCollectionTest.php index 1c12dcd..22edcc2 100644 --- a/core/tests/Drupal/Tests/Core/Asset/Collection/AssetCollectionTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/Collection/AssetCollectionTest.php @@ -23,7 +23,7 @@ class AssetCollectionTest extends AssetUnitTest { public static function getInfo() { return array( 'name' => 'Asset collection tests', - 'description' => 'Unit tests on AssetBag', + 'description' => 'Unit tests on AssetCollection', 'group' => 'Asset', ); } diff --git a/core/tests/Drupal/Tests/Core/Asset/Factory/AssetCollectorTest.php b/core/tests/Drupal/Tests/Core/Asset/Factory/AssetCollectorTest.php index f3b6d45..a853afe 100644 --- a/core/tests/Drupal/Tests/Core/Asset/Factory/AssetCollectorTest.php +++ b/core/tests/Drupal/Tests/Core/Asset/Factory/AssetCollectorTest.php @@ -72,7 +72,7 @@ public function testDefaultPropagation() { } /** - * @expectedException Exception + * @expectedException \RuntimeException */ public function testExceptionOnAddingAssetWithoutCollectionPresent() { $asset = $this->collector->create('css', 'string', 'foo'); @@ -100,15 +100,17 @@ public function testAddAssetExplicitly() { $this->assertContains($mock, $collection); } - /** - * @expectedException Exception - */ - public function testClearBag() { + public function testSetCollection() { $collection = new AssetCollection(); $this->collector->setCollection($collection); - $this->collector->clearCollection(); + $this->assertTrue($this->collector->hasCollection()); + } - $this->collector->add($this->collector->create('css', 'file', 'bar')); + public function testClearCollection() { + $collection = new AssetCollection(); + $this->collector->setCollection($collection); + $this->collector->clearCollection(); + $this->assertFalse($this->collector->hasCollection()); } public function testLock() { @@ -123,7 +125,7 @@ public function testUnlock() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testUnlockFailsWithoutCorrectSecret() { $this->collector->lock('foo'); @@ -131,14 +133,14 @@ public function testUnlockFailsWithoutCorrectSecret() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testUnlockFailsIfNotLocked() { $this->collector->unlock('foo'); } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testLockFailsIfLocked() { $this->collector->lock('foo'); @@ -146,7 +148,7 @@ public function testLockFailsIfLocked() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testLockingPreventsSettingDefaults() { $this->collector->lock($this); @@ -154,7 +156,7 @@ public function testLockingPreventsSettingDefaults() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testLockingPreventsRestoringDefaults() { $this->collector->lock($this); @@ -162,7 +164,7 @@ public function testLockingPreventsRestoringDefaults() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testLockingPreventsClearingCollection() { $this->collector->lock($this); @@ -170,7 +172,7 @@ public function testLockingPreventsClearingCollection() { } /** - * @expectedException Exception + * @expectedException \Drupal\Core\Asset\Exception\LockedObjectException */ public function testLockingPreventsSettingCollection() { $this->collector->lock($this); @@ -218,7 +220,7 @@ public function testMetadataTypeMustBeCorrect() { } /** - * @expectedException InvalidArgumentException + * @expectedException \InvalidArgumentException */ public function testGetNonexistentDefault() { $this->collector->getMetadataDefaults('foo');