浏览代码

Use named params for custom finders.

mscherer 2 年之前
父节点
当前提交
1b6f91fdcc

+ 8 - 12
src/Model/Behavior/BitmaskedBehavior.php

@@ -9,7 +9,6 @@ use Cake\Event\EventInterface;
 use Cake\ORM\Behavior;
 use Cake\ORM\Query\SelectQuery;
 use Cake\Utility\Inflector;
-use InvalidArgumentException;
 use RuntimeException;
 
 /**
@@ -51,21 +50,18 @@ class BitmaskedBehavior extends Behavior {
 
 	/**
 	 * @param \Cake\ORM\Query\SelectQuery $query
+	 * @param array<int> $bits
 	 * @param array<string, mixed> $options
 	 * @throws \InvalidArgumentException If the 'slug' key is missing in options
 	 * @return \Cake\ORM\Query\SelectQuery
 	 */
-	public function findBitmasked(SelectQuery $query, array $options) {
-		if (!isset($options['bits'])) {
-			throw new InvalidArgumentException("The 'bits' key is required for find('bits')");
-		}
+	public function findBitmasked(SelectQuery $query, array $bits, array $options = []) {
 		$options += [
 			'type' => $this->_config['type'] ?? 'exact',
 			'containMode' => $this->_config['containMode'],
 		];
 
 		if ($options['type'] === 'contain') {
-			$bits = (array)$options['bits'];
 			if (!$bits) {
 				$field = $this->_config['field'];
 
@@ -73,9 +69,9 @@ class BitmaskedBehavior extends Behavior {
 			}
 
 			if ($options['containMode'] === 'and') {
-				$bits = $this->encodeBitmask($options['bits']);
+				$encodedBits = $this->encodeBitmask($bits);
 
-				return $query->where($this->containsBit($bits));
+				return $query->where($this->containsBit($encodedBits));
 			}
 
 			$conditions = [];
@@ -86,13 +82,13 @@ class BitmaskedBehavior extends Behavior {
 			return $query->where(['OR' => $conditions]);
 		}
 
-		$bits = $this->encodeBitmask($options['bits']);
-		if ($bits === null) {
+		$encodedBits = $this->encodeBitmask($bits);
+		if ($encodedBits === null) {
 			$field = $this->getConfig('field');
-			$bits = $this->_getDefaultValue($field);
+			$encodedBits = $this->_getDefaultValue($field);
 		}
 
-		return $query->where([$this->_table->getAlias() . '.' . $this->_config['field'] . ' IS' => $bits]);
+		return $query->where([$this->_table->getAlias() . '.' . $this->_config['field'] . ' IS' => $encodedBits]);
 	}
 
 	/**

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

@@ -10,7 +10,6 @@ use Cake\ORM\Behavior;
 use Cake\ORM\Query\SelectQuery;
 use Cake\ORM\Table;
 use Cake\Utility\Inflector;
-use InvalidArgumentException;
 use RuntimeException;
 use Shim\Utility\Inflector as ShimInflector;
 
@@ -172,16 +171,12 @@ class SluggedBehavior extends Behavior {
 	 * ->find('slugged')
 	 *
 	 * @param \Cake\ORM\Query\SelectQuery $query
-	 * @param array<string, mixed> $options
+	 * @param string $slug
 	 * @throws \InvalidArgumentException If the 'slug' key is missing in options
 	 * @return \Cake\ORM\Query\SelectQuery
 	 */
-	public function findSlugged(SelectQuery $query, array $options): SelectQuery {
-		if (empty($options['slug'])) {
-			throw new InvalidArgumentException("The 'slug' key is required for find('slugged')");
-		}
-
-		return $query->where([$this->_config['field'] => $options['slug']]);
+	public function findSlugged(SelectQuery $query, string $slug): SelectQuery {
+		return $query->where([$this->_config['field'] => $slug]);
 	}
 
 	/**

+ 14 - 22
tests/TestCase/Model/Behavior/BitmaskedBehaviorTest.php

@@ -85,11 +85,11 @@ class BitmaskedBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testFindBitmasked() {
-		$res = $this->Comments->find('bits', ['bits' => []])->toArray();
+		$res = $this->Comments->find('bits', bits: [])->toArray();
 		$this->assertCount(1, $res);
 		$this->assertSame([], $res[0]->statuses);
 
-		$res = $this->Comments->find('bits', ['bits' => [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED]])->toArray();
+		$res = $this->Comments->find('bits', bits: [BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED])->toArray();
 		$this->assertCount(1, $res);
 		$this->assertSame([BitmaskedComment::STATUS_ACTIVE, BitmaskedComment::STATUS_APPROVED], $res[0]->statuses);
 	}
@@ -98,26 +98,26 @@ class BitmaskedBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testFindBitmaskedContain() {
+		$bits = [];
 		$options = [
-			'bits' => [],
 			'type' => 'contain',
 		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(1, $res);
 		$this->assertSame([], $res[0]->statuses);
 
+		$bits = [BitmaskedComment::STATUS_APPROVED];
 		$options = [
-			'bits' => [BitmaskedComment::STATUS_APPROVED],
 			'type' => 'contain',
 		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(3, $res);
 
+		$bits = [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED];
 		$options = [
-			'bits' => [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED],
 			'type' => 'contain',
 		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(5, $res);
 	}
 
@@ -125,29 +125,21 @@ class BitmaskedBehaviorTest extends TestCase {
 	 * @return void
 	 */
 	public function testFindBitmaskedContainAnd() {
+		$bits = [];
 		$options = [
-			'bits' => [],
 			'type' => 'contain',
 			'containMode' => 'and',
 		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(1, $res);
 		$this->assertSame([], $res[0]->statuses);
 
-		$options = [
-			'bits' => [BitmaskedComment::STATUS_APPROVED],
-			'type' => 'contain',
-			'containMode' => 'and',
-		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$bits = [BitmaskedComment::STATUS_APPROVED];
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(3, $res);
 
-		$options = [
-			'bits' => [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED],
-			'type' => 'contain',
-			'containMode' => 'and',
-		];
-		$res = $this->Comments->find('bits', $options)->toArray();
+		$bits = [BitmaskedComment::STATUS_APPROVED, BitmaskedComment::STATUS_PUBLISHED];
+		$res = $this->Comments->find('bits', bits: $bits, options: $options)->toArray();
 		$this->assertCount(1, $res);
 	}
 

+ 12 - 7
tests/TestCase/Model/Behavior/SluggedBehaviorTest.php

@@ -78,12 +78,11 @@ class SluggedBehaviorTest extends TestCase {
 
 		$entity = $this->_getEntity();
 		$result = $this->articles->save($entity);
-		$this->assertEquals('test-123', $result->get('slug'));
+		$this->assertSame('test-123', $result->get('slug'));
 
-		//$entity = $this->_getEntity();
-		//$result = $this->articles->save($entity);
-		//$this->assertEquals('test-123', $result->get('slug'));
-		//debug($result);
+		$entity = $this->_getEntity();
+		$result = $this->articles->save($entity);
+		$this->assertSame('test-123-1', $result->get('slug'));
 	}
 
 	/**
@@ -105,11 +104,17 @@ class SluggedBehaviorTest extends TestCase {
 	}
 
 	/**
-	 * SluggedBehaviorTest::testCustomFinder()
-	 *
 	 * @return void
 	 */
 	public function testCustomFinder() {
+		$article = $this->articles->find()->find('slugged', slug: 'foo')->first();
+		$this->assertEquals('Foo', $article->get('title'));
+	}
+
+	/**
+	 * @return void
+	 */
+	public function testCustomFinderLegacy() {
 		$article = $this->articles->find()->find('slugged', ['slug' => 'foo'])->first();
 		$this->assertEquals('Foo', $article->get('title'));
 	}