浏览代码

Add test case and clearer exception message.

mscherer 6 年之前
父节点
当前提交
cde92a50ba

+ 8 - 5
src/Model/Behavior/SluggedBehavior.php

@@ -45,7 +45,7 @@ class SluggedBehavior extends Behavior {
 	 *     OR pass it a callable as custom method to be invoked
 	 *     OR pass it a callable as custom method to be invoked
 	 * - separator: The separator to use
 	 * - separator: The separator to use
 	 * - length:
 	 * - length:
-	 *  Set to 0 for no length. Will be auto-detected if possible via schema.
+	 *     Set to 0 for no length. Will be auto-detected if possible via schema.
 	 * - overwrite: has 2 values
 	 * - overwrite: has 2 values
 	 *     false - once the slug has been saved, do not change it (use if you are doing lookups based on slugs)
 	 *     false - once the slug has been saved, do not change it (use if you are doing lookups based on slugs)
 	 *     true - if the label field values change, regenerate the slug (use if you are the slug is just window-dressing)
 	 *     true - if the label field values change, regenerate the slug (use if you are the slug is just window-dressing)
@@ -144,18 +144,21 @@ class SluggedBehavior extends Behavior {
 				if (strpos($field, '.')) {
 				if (strpos($field, '.')) {
 					list($alias, $field) = explode('.', $field);
 					list($alias, $field) = explode('.', $field);
 					if (!$this->_table->$alias->hasField($field)) {
 					if (!$this->_table->$alias->hasField($field)) {
-						throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->$alias->getAlias() . ' is missing the field ' . $field .
-							' (specified in the setup for model ' . $this->_table->getAlias() . ') ');
+						throw new RuntimeException('(SluggedBehavior::setup) model `' . $this->_table->$alias->getAlias() . '` is missing the field `' . $field .
+							'` (specified in the setup for table `' . $this->_table->getAlias() . '`) ');
 					}
 					}
 				} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->getEntityClass(), '_get' . Inflector::classify($field))) {
 				} elseif (!$this->_table->hasField($field) && !method_exists($this->_table->getEntityClass(), '_get' . Inflector::classify($field))) {
-					throw new RuntimeException('(SluggedBehavior::setup) model ' . $this->_table->getAlias() . ' is missing the field ' . $field . ' specified in the setup.');
+					throw new RuntimeException('(SluggedBehavior::setup) model `' . $this->_table->getAlias() . '` is missing the field `' . $field .
+						'` (specified in the setup for entity `' . $this->_table->getEntityClass() . '`.');
 				}
 				}
 			}
 			}
 		}
 		}
 	}
 	}
 
 
 	/**
 	/**
-	 * SluggedBehavior::findSlugged()
+	 * Customn finder exposed as
+	 *
+	 * ->find('slugged')
 	 *
 	 *
 	 * @param \Cake\ORM\Query $query
 	 * @param \Cake\ORM\Query $query
 	 * @param array $options
 	 * @param array $options

+ 26 - 1
tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -2,6 +2,7 @@
 
 
 namespace Tools\Test\TestCase\Model\Behavior;
 namespace Tools\Test\TestCase\Model\Behavior;
 
 
+use App\Model\Entity\SluggedArticle;
 use Cake\Core\Configure;
 use Cake\Core\Configure;
 use Cake\ORM\Entity;
 use Cake\ORM\Entity;
 use Cake\ORM\TableRegistry;
 use Cake\ORM\TableRegistry;
@@ -626,7 +627,7 @@ class SluggedBehaviorTest extends TestCase {
 	 */
 	 */
 	public function testSlugGenerationWithVirtualField() {
 	public function testSlugGenerationWithVirtualField() {
 		$this->articles->removeBehavior('Slugged');
 		$this->articles->removeBehavior('Slugged');
-		$this->articles->setEntityClass('\App\Model\Entity\SluggedArticle');
+		$this->articles->setEntityClass(SluggedArticle::class);
 		$this->articles->addBehavior('Tools.Slugged', [
 		$this->articles->addBehavior('Tools.Slugged', [
 			'label' => [
 			'label' => [
 				'title',
 				'title',
@@ -643,6 +644,30 @@ class SluggedBehaviorTest extends TestCase {
 	}
 	}
 
 
 	/**
 	/**
+	 * Tests slug generation fails with invalid entity config.
+	 *
+	 * @expectedException \RuntimeException
+	 * @expectedExceptionMessage (SluggedBehavior::setup) model `SluggedArticles` is missing the field `specialNonExistent` (specified in the setup for entity `App\Model\Entity\SluggedArticle`.
+	 *
+	 * @return void
+	 */
+	public function testSlugGenerationWithVirtualFieldInvalidField() {
+		$this->articles->removeBehavior('Slugged');
+		$this->articles->setEntityClass(SluggedArticle::class);
+		$this->articles->addBehavior('Tools.Slugged', [
+			'label' => [
+				'specialNonExistent',
+			],
+		]);
+
+		$data = [
+			'title' => 'Some Article 12345',
+		];
+		$article = $this->articles->newEntity($data);
+		$this->articles->save($article);
+	}
+
+	/**
 	 * Test slug generation works with new slugger.
 	 * Test slug generation works with new slugger.
 	 *
 	 *
 	 * @return void
 	 * @return void

+ 7 - 0
tests/test_app/Model/Entity/SluggedArticle.php

@@ -7,6 +7,13 @@ use Cake\ORM\Entity;
 class SluggedArticle extends Entity {
 class SluggedArticle extends Entity {
 
 
 	/**
 	/**
+	 * @var string[]
+	 */
+	protected $_virtual = [
+		'special',
+	];
+
+	/**
 	 * Virtual field
 	 * Virtual field
 	 *
 	 *
 	 * @return string
 	 * @return string