Explorar o código

ZF-8238: Move protected method only used in the Ec2 classes into the ec2 abstract class intead of being the general Amazon Abstract Class

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18913 44c647ce-9c0f-0410-b52a-842ac1e357ba
sidhighwind %!s(int64=16) %!d(string=hai) anos
pai
achega
6e5c539b70

+ 3 - 54
library/Zend/Service/Amazon/Abstract.php

@@ -48,11 +48,6 @@ abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
     protected static $_defaultSecretKey = null;
 
     /**
-     * @var string Amazon Region
-     */
-    protected static $_defaultRegion = null;
-
-    /**
      * @var string Amazon Secret Key
      */
     protected $_secretKey;
@@ -62,17 +57,6 @@ abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
      */
     protected $_accessKey;
 
-    /**
-     * @var string Amazon Region
-     */
-    protected $_region;
-
-    /**
-     * An array that contains all the valid Amazon Ec2 Regions.
-     *
-     * @var array
-     */
-    protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
 
     /**
      * Set the keys to use when accessing SQS.
@@ -88,30 +72,13 @@ abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
     }
 
     /**
-     * Set which region you are working in.  It will append the
-     * end point automaticly
-     *
-     * @param string $region
-     */
-    public static function setRegion($region)
-    {
-        if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
-            self::$_defaultRegion = $region;
-        } else {
-            require_once 'Zend/Service/Amazon/Exception.php';
-            throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
-        }
-    }
-
-    /**
-     * Create Amazon Sqs client.
+     * Create Amazon client.
      *
      * @param  string $access_key       Override the default Access Key
      * @param  string $secret_key       Override the default Secret Key
-     * @param  string $region           Sets the AWS Region
      * @return void
      */
-    public function __construct($accessKey=null, $secretKey=null, $region=null)
+    public function __construct($accessKey=null, $secretKey=null)
     {
         if(!$accessKey) {
             $accessKey = self::$_defaultAccessKey;
@@ -119,15 +86,6 @@ abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
         if(!$secretKey) {
             $secretKey = self::$_defaultSecretKey;
         }
-        if(!$region) {
-            $region = self::$_defaultRegion;
-        } else {
-            // make rue the region is valid
-            if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
-                require_once 'Zend/Service/Amazon/Exception.php';
-                throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
-            }
-        }
 
         if(!$accessKey || !$secretKey) {
             require_once 'Zend/Service/Amazon/Exception.php';
@@ -135,18 +93,9 @@ abstract class Zend_Service_Amazon_Abstract extends Zend_Service_Abstract
         }
         $this->_accessKey = $accessKey;
         $this->_secretKey = $secretKey;
-        $this->_region = $region;
     }
 
-    /**
-     * Method to fetch the AWS Region
-     *
-     * @return string
-     */
-    protected function _getRegion()
-    {
-        return (!empty($this->_region)) ? $this->_region . '.' : '';
-    }
+    
 
     /**
      * Method to fetch the Access Key

+ 68 - 0
library/Zend/Service/Amazon/Ec2/Abstract.php

@@ -70,6 +70,74 @@ abstract class Zend_Service_Amazon_Ec2_Abstract extends Zend_Service_Amazon_Abst
      * Period after which HTTP request will timeout in seconds
      */
     protected $_httpTimeout = 10;
+    
+    /**
+     * @var string Amazon Region
+     */
+    protected static $_defaultRegion = null;
+    
+    /**
+     * @var string Amazon Region
+     */
+    protected $_region;
+
+    /**
+     * An array that contains all the valid Amazon Ec2 Regions.
+     *
+     * @var array
+     */
+    protected static $_validEc2Regions = array('eu-west-1', 'us-east-1');
+    
+	/**
+     * Create Amazon client.
+     *
+     * @param  string $access_key       Override the default Access Key
+     * @param  string $secret_key       Override the default Secret Key
+     * @param  string $region           Sets the AWS Region
+     * @return void
+     */
+    public function __construct($accessKey=null, $secretKey=null, $region=null)
+    {
+        if(!$region) {
+            $region = self::$_defaultRegion;
+        } else {
+            // make rue the region is valid
+            if(!empty($region) && !in_array(strtolower($region), self::$_validEc2Regions, true)) {
+                require_once 'Zend/Service/Amazon/Exception.php';
+                throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
+            }
+        }
+
+        $this->_region = $region;
+        
+        parent::__construct($accessKey, $secretKey);
+    }
+    
+	/**
+     * Set which region you are working in.  It will append the
+     * end point automaticly
+     *
+     * @param string $region
+     */
+    public static function setRegion($region)
+    {
+        if(in_array(strtolower($region), self::$_validEc2Regions, true)) {
+            self::$_defaultRegion = $region;
+        } else {
+            require_once 'Zend/Service/Amazon/Exception.php';
+            throw new Zend_Service_Amazon_Exception('Invalid Amazon Ec2 Region');
+        }
+    }
+    
+	/**
+     * Method to fetch the AWS Region
+     *
+     * @return string
+     */
+    protected function _getRegion()
+    {
+        return (!empty($this->_region)) ? $this->_region . '.' : '';
+    }
 
     /**
      * Sends a HTTP request to the queue service using Zend_Http_Client

+ 0 - 22
tests/Zend/Service/Amazon/AbstractTest.php

@@ -94,24 +94,6 @@ class AmamzonAbstract extends PHPUnit_Framework_TestCase
         $this->assertEquals('TestAccessKey', $class->returnAccessKey());
         $this->assertEquals('TestSecretKey', $class->returnSecretKey());
     }
-
-    public function testSetRegion()
-    {
-        TestAmamzonAbstract::setRegion('eu-west-1');
-
-        $class = new TestAmamzonAbstract('TestAccessKey', 'TestSecretKey');
-        $this->assertEquals('eu-west-1', $class->returnRegion());
-    }
-    
-    public function testSetInvalidRegionThrowsException()
-    {
-        try {
-            TestAmamzonAbstract::setRegion('eu-west-1a');
-            $this->fail('Invalid Region Set with no Exception Thrown');
-        } catch (Zend_Service_Amazon_Exception $zsae) {
-            // do nothing
-        }
-    }
 }
 
 class TestAmamzonAbstract extends Zend_Service_Amazon_Abstract
@@ -126,9 +108,5 @@ class TestAmamzonAbstract extends Zend_Service_Amazon_Abstract
         return $this->_secretKey;
     }
 
-    public function returnRegion()
-    {
-        return $this->_region;
-    }
 }
 

+ 95 - 0
tests/Zend/Service/Amazon/Ec2/AbstractTest.php

@@ -0,0 +1,95 @@
+<?php
+/**
+ * Zend Framework
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * It is also available through the world-wide-web at this URL:
+ * http://framework.zend.com/license/new-bsd
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to license@zend.com so we can send you a copy immediately.
+ *
+ * @category   Zend
+ * @package    Zend_Service_Amazon
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @version    $Id: AbstractTest.php 17667 2009-08-18 21:40:09Z mikaelkael $
+ */
+
+/**
+ * Test helper
+ */
+require_once dirname(__FILE__) . '/../../../../TestHelper.php';
+
+require_once 'PHPUnit/Framework/TestCase.php';
+require_once 'Zend/Service/Amazon/Ec2/Abstract.php';
+
+/**
+ * @todo: Rename class to Zend_Service_Amazon_AbstractTest
+ *
+ * @category   Zend
+ * @package    Zend_Service_Amazon
+ * @subpackage UnitTests
+ * @copyright  Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license    http://framework.zend.com/license/new-bsd     New BSD License
+ * @group      Zend_Service
+ * @group      Zend_Service_Amazon
+ */
+class Zend_Service_Amazon_Ec2_AbstractTest extends PHPUnit_Framework_TestCase
+{
+    /**
+     * Prepares the environment before running a test.
+     */
+    protected function setUp()
+    {
+        parent::setUp();
+    }
+
+    /**
+     * Cleans up the environment after running a test.
+     */
+    protected function tearDown()
+    {
+        parent::tearDown();
+    }
+
+    public function testNoKeysThrowException()
+    {
+        try {
+            $class = new TestAmamzonAbstract();
+            $this->fail('Exception should be thrown when no keys are passed in.');
+        } catch(Zend_Service_Amazon_Exception $zsae) {}
+    }
+
+    public function testSetRegion()
+    {
+        TestAmamzonAbstract::setRegion('eu-west-1');
+
+        $class = new TestAmamzonAbstract('TestAccessKey', 'TestSecretKey');
+        $this->assertEquals('eu-west-1', $class->returnRegion());
+    }
+    
+    public function testSetInvalidRegionThrowsException()
+    {
+        try {
+            TestAmamzonAbstract::setRegion('eu-west-1a');
+            $this->fail('Invalid Region Set with no Exception Thrown');
+        } catch (Zend_Service_Amazon_Exception $zsae) {
+            // do nothing
+        }
+    }
+}
+
+class TestAmamzonAbstract extends Zend_Service_Amazon_Ec2_Abstract
+{
+
+    public function returnRegion()
+    {
+        return $this->_region;
+    }
+}
+