浏览代码

Merge pull request #134 from dereuromark/develop

Prepare release v0.9
Mark S. 10 年之前
父节点
当前提交
8a7684cfce

+ 1 - 1
Controller/Component/AuthExtComponent.php

@@ -151,7 +151,7 @@ class AuthExtComponent extends AuthComponent {
 		$Model = $this->getModel();
 		$userArray = $user;
 		if (!is_array($userArray)) {
-			$user = $Model->get($user);
+			$user = $Model->get($user, ['noException' => true]);
 			if (!$user) {
 				return [];
 			}

+ 9 - 60
Model/MyModel.php

@@ -1226,71 +1226,20 @@ class MyModel extends ShimModel {
 
 	/**
 	 * Shortcut method to find a specific entry via primary key.
-	 *
-	 * Either provide the id directly:
+	 * Wraps ShimModel::get() for an exception free response.
 	 *
 	 *   $record = $this->Model->get($id);
 	 *
-	 * Or use
-	 *
-	 *   $this->Model->id = $id;
-	 *   $record = $this->Model->get();
-	 *
 	 * @param mixed $id
-	 * @param array $options Options for find(). Used to be fields array/string.
-	 * @param array $contain Deprecated - use
-	 * @return mixed
+	 * @param array $options Options for find().
+	 * @return array
 	 */
-	public function get($id = null, $options = [], $contain = []) {
-		if (is_array($id)) {
-			$column = $id[0];
-			$value = $id[1];
-		} else {
-			$column = $this->primaryKey;
-			$value = $id;
-			if ($value === null) {
-				$value = $this->id;
-			}
-		}
-		if (!$value) {
-			return [];
-		}
-
-		// BC
-		$fields = null;
-		if (is_string($options)) {
-			$fields = $options;
-			$options = [];
+	public function record($id, array $options = []) {
+		try {
+			return $this->get($id, $options);
+		} catch (RecordNotFoundException $e) {
 		}
-		if (!empty($options) && !array_key_exists('fields', $options) && !array_key_exists('contain', $options)) {
-			$fields = $options;
-			$options = [];
-		}
-
-		if ($fields === '*') {
-			$fields = $this->alias . '.*';
-		} elseif (!empty($fields)) {
-			foreach ((array)$fields as $row => $field) {
-				if (strpos($field, '.') !== false) {
-					continue;
-				}
-				$fields[$row] = $this->alias . '.' . $field;
-			}
-		}
-
-		$options = [
-			'conditions' => [$this->alias . '.' . $column => $value],
-		] + $options;
-
-		// BC
-		if (!empty($fields)) {
-			$options['fields'] = $fields;
-		}
-		if (!empty($contain)) {
-			$options['contain'] = $contain;
-		}
-
-		return $this->find('first', $options);
+		return array();
 	}
 
 	/**
@@ -1362,7 +1311,7 @@ class MyModel extends ShimModel {
 	 * @return ARRAY record: [Model][values],...
 	 */
 	public function toggleField($fieldName, $id) {
-		$record = $this->get($id, [$this->primaryKey, $fieldName]);
+		$record = $this->get($id, ['conditions' => [$this->primaryKey, $fieldName]]);
 
 		if (!empty($record) && !empty($fieldName) && $this->hasField($fieldName)) {
 			$record[$this->alias][$fieldName] = ($record[$this->alias][$fieldName] == 1 ? 0 : 1);

+ 5 - 5
Test/Case/Model/Behavior/LogableBehaviorTest.php

@@ -244,7 +244,7 @@ class LogableBehaviorTest extends CakeTestCase {
 
 	public function testUserLogging() {
 		$this->LogableUser->save(['LogableUser' => ['name' => 'Jonny']]);
-		$result = $this->Log->get(6, ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']);
+		$result = $this->Log->get(6, ['fields' => ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']]);
 		$expected = [
 			'LogableLog' => [
 				'id' => 6,
@@ -260,7 +260,7 @@ class LogableBehaviorTest extends CakeTestCase {
 		// check with LogableUser
 		$this->assertEquals($expected, $result);
 		$this->LogableUser->delete(302);
-		$result = $this->Log->get(7, ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']);
+		$result = $this->Log->get(7, ['fields' => ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']]);
 		$expected = [
 			'LogableLog' => [
 				'id' => 7,
@@ -279,7 +279,7 @@ class LogableBehaviorTest extends CakeTestCase {
 
 	public function testLoggingWithoutDisplayField() {
 		$this->LogableComment->save(['LogableComment' => ['content' => 'You too?']]);
-		$result = $this->Log->get(6, ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']);
+		$result = $this->Log->get(6, ['fields' => ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']]);
 		$expected = [
 			'LogableLog' => [
 				'id' => 6,
@@ -522,7 +522,7 @@ class LogableBehaviorTest extends CakeTestCase {
 		$this->LogableComment->Behaviors->load('Logable', ['descriptionIds' => false, 'userModel' => 'LogableUser']);
 		$this->LogableComment->setUserData(['LogableUser' => ['id' => 66, 'name' => 'Alexander']]);
 		$this->LogableComment->save(['LogableComment' => ['id' => 1, 'content' => 'You too?']]);
-		$result = $this->Log->get(6, ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']);
+		$result = $this->Log->get(6, ['fields' => ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']]);
 		$expected = [
 			'LogableLog' => [
 				'id' => (string)6,
@@ -541,7 +541,7 @@ class LogableBehaviorTest extends CakeTestCase {
 	public function testIgnoreExtraFields() {
 		$this->LogableComment->setUserData(['LogableUser' => ['id' => 66, 'name' => 'Alexander']]);
 		$this->LogableComment->save(['LogableComment' => ['id' => 1, 'content' => 'You too?', 'extra_field' => 'some data']]);
-		$result = $this->Log->get(6, ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']);
+		$result = $this->Log->get(6, ['fields' => ['id', 'title', 'description', 'model', 'foreign_id', 'action', 'user_id', 'change']]);
 		$expected = [
 			'LogableLog' => [
 				'id' => (string)6,

+ 31 - 8
Test/Case/Model/MyModelTest.php

@@ -40,11 +40,6 @@ class MyModelTest extends MyCakeTestCase {
 		$record = $this->Post->get(2, ['fields' => ['id', 'title', 'body'], 'contain' => ['Author']]);
 		$this->assertEquals(3, count($record['Post']));
 		$this->assertEquals(3, $record['Author']['id']);
-
-		// BC
-		$record = $this->Post->get(2, ['id', 'title', 'body'], ['Author']);
-		$this->assertEquals(3, count($record['Post']));
-		$this->assertEquals(3, $record['Author']['id']);
 	}
 
 	/**
@@ -98,13 +93,41 @@ class MyModelTest extends MyCakeTestCase {
 	}
 
 	/**
-	 * More tests in MyModel Test directly
+	 * Test 3.x shim get()
+	 *
+	 * @expectedException RecordNotFoundException
+	 * @return void
+	 */
+	public function testGetInvalid() {
+		$this->User->order = [];
+		$this->User->get('xyz');
+	}
+
+	/**
+	 * MyModelTest::testRecord()
+	 *
+	 * @return void
+	 */
+	public function testRecord() {
+		$record = $this->Post->record(2);
+		$this->assertEquals(2, $record['Post']['id']);
+
+		$record = $this->Post->record(2, ['fields' => ['id', 'created']]);
+		$this->assertEquals(2, count($record['Post']));
+
+		$record = $this->Post->record(2, ['fields' => ['id', 'title', 'body'], 'contain' => ['Author']]);
+		$this->assertEquals(3, count($record['Post']));
+		$this->assertEquals(3, $record['Author']['id']);
+	}
+
+	/**
+	 * Test record()
 	 *
 	 * @return void
 	 */
-	public function testGetFalse() {
+	public function testRecordInvalid() {
 		$this->User->order = [];
-		$is = $this->User->get('xyz');
+		$is = $this->User->record('xyz');
 		$this->assertSame([], $is);
 	}
 

+ 2 - 2
View/Helper/FormExtHelper.php

@@ -1,5 +1,5 @@
 <?php
-App::uses('FormHelper', 'View/Helper');
+App::uses('FormShimHelper', 'Shim.View/Helper');
 
 /**
  * Enhance Forms with JS widget stuff
@@ -20,7 +20,7 @@ App::uses('FormHelper', 'View/Helper');
  *   $this->Js->writeBuffer() with onDomReady=>false then, though.
  *
  */
-class FormExtHelper extends FormHelper {
+class FormExtHelper extends FormShimHelper {
 
 	public $helpers = ['Html', 'Js', 'Tools.Common'];
 

+ 2 - 2
View/Helper/HtmlExtHelper.php

@@ -1,5 +1,5 @@
 <?php
-App::uses('HtmlHelper', 'View/Helper');
+App::uses('HtmlShimHelper', 'Shim.View/Helper');
 
 /**
  * HtmlExt Helper
@@ -10,7 +10,7 @@ App::uses('HtmlHelper', 'View/Helper');
  * @author Mark Scherer
  * @license http://opensource.org/licenses/mit-license.php MIT
  */
-class HtmlExtHelper extends HtmlHelper {
+class HtmlExtHelper extends HtmlShimHelper {
 
 	/**
 	 * For convenience functions Html::defaultLink() and defaultUrl().

+ 1 - 1
composer.json

@@ -15,7 +15,7 @@
 	"require": {
 		"php": ">=5.3",
 		"composer/installers": "*",
-		"dereuromark/cakephp-shim": "0.1.*"
+		"dereuromark/cakephp-shim": "0.2.*"
 	},
 	"support": {
 		"source": "https://github.com/dereuromark/cakephp-tools"

+ 3 - 19
docs/Shims.md

@@ -22,7 +22,9 @@ $config['Paginator'] = array(
 ```
 Just make sure, that your AppController extends the `Tools.MyController` class.
 
-You can also set `Configure::write('App.warnAboutNamedParams', true)` to get a warning if
+Tip: Using the Shim plugin this is already done for you out of the box.
+
+You can also set `Configure::write('Shim.warnAboutNamedParams', true)` to get a warning if
 any of your code still uses the deprecated named params.
 
 
@@ -30,12 +32,6 @@ any of your code still uses the deprecated named params.
 In 3.x there will be a FlashComponent instead. Mine also provides stackable (multi) messages.
 See https://github.com/dereuromark/cakephp-tools/wiki/Flash-messages for details.
 
-### IntegrationTestCase
-The ControllerTestCase will be gone in 3.x. Instead there will be a IntegrationTestCase that involves
-less, but smarter, mocking.
-I backported a basic version of it in 2.x already.
-When upgrading you almost have nothing to change in those test case files.
-
 ### Login/Passwords
 Already use the PHP5.5+ password functionality with the ModernPasswordHasher class and the Passwordable behavior.
 Easily upgradable to 3.x in minutes.
@@ -43,18 +39,6 @@ Easily upgradable to 3.x in minutes.
 ### RSS
 Use RssView instead of the akward and limited helper approach.
 
-### MyCakeTestCase
-- testAction() defaults to GET
-- TestConsoleOutput() for stdout and stderr instead of mocks
-
-### Model shims
-In 3.x the updateAll() and deleteAll() won't autojoin anymore. In order to already write future proof versions of that you can use
-- updateAllJoinless()
-- deleteAllJoinless()
-In 3.x all you need to do is rename them back again instead of tryig to fix all broken code.
-
-For findById() and exists() there will be get(). You can directly use it via MyModel::get().
-
 
 ### More