Browse Source

a unit test added

Val Bancer 9 years ago
parent
commit
42255de44e

+ 8 - 8
Model/Behavior/BitmaskedBehavior.php

@@ -174,7 +174,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	/**
 	 * @param Model $Model
 	 * @param int $value Bitmask.
-	 * @param string $fieldName field name.
+	 * @param string|null $fieldName field name.
 	 * @return array Bitmask array (from DB to APP).
 	 */
 	public function decodeBitmask(Model $Model, $value, $fieldName = null) {
@@ -195,7 +195,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	/**
 	 * @param Model $Model
 	 * @param array $value Bitmask array.
-	 * @param array $defaultValue Default bitmask array.
+	 * @param array|null $defaultValue Default bitmask array.
 	 * @return int Bitmask (from APP to DB).
 	 */
 	public function encodeBitmask(Model $Model, $value, $defaultValue = null) {
@@ -279,7 +279,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	/**
 	 * @param Model $Model
 	 * @param mixed $bits (int, array)
-	 * @param string $fieldName field name.
+	 * @param string|null $fieldName field name.
 	 * @return array SQL snippet.
 	 */
 	public function isBit(Model $Model, $bits, $fieldName = null) {
@@ -288,13 +288,13 @@ class BitmaskedBehavior extends ModelBehavior {
 		}
 		$bits = (array)$bits;
 		$bitmask = $this->encodeBitmask($Model, $bits);
-		return array($Model->alias . '.' . $fieldName => $bitmask);
+		return [$Model->alias . '.' . $fieldName => $bitmask];
 	}
 
 	/**
 	 * @param Model $Model
 	 * @param mixed $bits (int, array)
-	 * @param string $fieldName field name.
+	 * @param string|null $fieldName field name.
 	 * @return array SQL snippet.
 	 */
 	public function isNotBit(Model $Model, $bits, $fieldName = null) {
@@ -304,7 +304,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	/**
 	 * @param Model $Model
 	 * @param mixed $bits (int, array)
-	 * @param string $fieldName field name.
+	 * @param string|null $fieldName field name.
 	 * @return array SQL snippet.
 	 */
 	public function containsBit(Model $Model, $bits, $fieldName = null) {
@@ -314,7 +314,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	/**
 	 * @param Model $Model
 	 * @param mixed $bits (int, array)
-	 * @param string $fieldName field name.
+	 * @param string|null $fieldName field name.
 	 * @return array SQL snippet.
 	 */
 	public function containsNotBit(Model $Model, $bits, $fieldName = null) {
@@ -328,7 +328,7 @@ class BitmaskedBehavior extends ModelBehavior {
 	 * @param bool $contain
 	 * @return array SQL snippet.
 	 */
-	protected function _containsBit(Model $Model, $bits, $fieldName = null, $contain = true) {
+	protected function _containsBit(Model $Model, $bits, $fieldName, $contain = true) {
 		if (empty($fieldName)) {
 			$fieldName = $this->_getFieldName($Model);
 		}

+ 24 - 0
Test/Case/Model/Behavior/BitmaskedBehaviorTest.php

@@ -178,4 +178,28 @@ class BitmaskedBehaviorTest extends MyCakeTestCase {
 		$this->assertTrue(!empty($res) && count($res) === 5);
 	}
 
+	public function testSaveMultiFields() {
+		$this->Comment->Behaviors->unload('Bitmasked');
+		$this->Comment->Behaviors->load('Tools.Bitmasked', [
+			['mappedField' => 'types', 'field' => 'type'],
+			['mappedField' => 'statuses', 'field' => 'status'],
+		]);
+		$data = [
+			'comment' => 'test save',
+			'types' => [
+				BitmaskedComment::TYPE_COMPLAINT,
+				BitmaskedComment::TYPE_RFC,
+			],
+			'statuses' => [
+				BitmaskedComment::STATUS_ACTIVE,
+				BitmaskedComment::STATUS_APPROVED,
+			],
+		];
+		$this->Comment->create();
+		$result = $this->Comment->save($data);
+		$expectedStatus = BitmaskedComment::STATUS_ACTIVE | BitmaskedComment::STATUS_APPROVED;
+		$this->assertEquals($expectedStatus, $result['BitmaskedComment']['status']);
+		$expectedType = BitmaskedComment::TYPE_COMPLAINT | BitmaskedComment::TYPE_RFC;
+		$this->assertEquals($expectedType, $result['BitmaskedComment']['type']);
+	}
 }

+ 15 - 0
Test/test_app/Model/BitmaskedComment.php

@@ -12,6 +12,16 @@ class BitmaskedComment extends MyModel {
 		]
 	];
 
+	public static function types($value = null) {
+		$options = [
+			static::TYPE_BUG => 'Bug',
+			static::TYPE_COMPLAINT => 'Complaint',
+			static::TYPE_DISCUSSION => 'Discussion',
+			static::TYPE_RFC => 'Request for change',
+		];
+		return static::enum($value, $options);
+	}
+
 	public static function statuses($value = null) {
 		$options = [
 			static::STATUS_ACTIVE => __d('tools', 'Active'),
@@ -23,6 +33,11 @@ class BitmaskedComment extends MyModel {
 		return static::enum($value, $options);
 	}
 
+	const TYPE_BUG = 0;
+	const TYPE_COMPLAINT = 1;
+	const TYPE_DISCUSSION = 2;
+	const TYPE_RFC = 4;
+
 	const STATUS_NONE = 0;
 	const STATUS_ACTIVE = 1;
 	const STATUS_PUBLISHED = 2;