Alfonso Alba García avatar Alfonso Alba García committed e60e4d7

Test and first implementation of PleskDBFactory

Comments (0)

Files changed (3)

src/AAlba/PleskBackup/DomainList/Factory/PleskDBFactory.php

 <?php
 /**
- * This file is part of Cursosf2Application.
+ * This file is part of pleskbackup.
  *
- * Cursosf2Application is free software: you can redistribute it and/or modify
+ * pleskbackup is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * Cursosf2Application is distributed in the hope that it will be useful,
+ * pleskbackup is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Cursosf2Application.  If not, see <http://www.gnu.org/licenses/>.
+ * along with pleskbackup.  If not, see <http://www.gnu.org/licenses/>.
  */
+
 namespace AAlba\PleskBackup\DomainList\Factory
 {
+use Symfony\Component\Yaml\Parser;
 class PleskDBFactory implements \AAlba\PleskBackup\DomainList\Factory\DomainListFactoryInterface
 {
+    /**
+     * @var \AAlba\PleskBackup\DomainList
+     */
+    private $_domains;
+
+    /**
+     * @var \PDO
+     */
+    private $_pdo;
+
+    private
+        $_host,
+        $_port,
+        $_database,
+        $_username,
+        $_password;
+
+    public function __construct($data = null)
+    {
+        if(is_string($data))
+        {
+            $parser = new Parser();
+            try{
+                $databaseData = $parser->parse(file_get_contents($data));
+                $this->_host = $databaseData['host'];
+                $this->_port = $databaseData['port'];
+                $this->_database = $databaseData['database'];
+                $this->_username = $databaseData['username'];
+                $this->_password = $databaseData['password'];
+            } catch (\Exception $e) {
+                throw $e;
+            }
+
+        } else if(is_array($data))
+        {
+            $this->_host = $data['host'];
+            $this->_port = $data['port'];
+            $this->_database = $data['database'];
+            $this->_username = $data['username'];
+            $this->_password = $data['password'];
+        }
+    }
+
+    /**
+     * Returns the list of domains
+     *
+     * @return \AAlba\PleskBackup\DomainList
+     */
     public function getDomainList()
     {
+        $this->connect();
+        $this->doGetDomains();
+        $this->disconnect();
+    }
+
+    private function doGetDomains()
+    {
+        $this->_domains = new \AAlba\PleskBackup\DomainList();
+        $query = "select name from domains";
+        $res = $this->_pdo->query($query);
+        while($r = $res->fetch())
+        {
+            $this->_domains[] = $r['name'];
+        }
+    }
+    private function connect()
+    {
+        try
+        {
+            $this->_pdo = new PDO('mysql:host='.$this->_host.';port='.$this->_port.';dbname='.$this->_database,$this->_username,$this->_password);
+        } catch (\Exception $e)
+        {
+            throw $e;
+        }
+    }
+
+    private function disconnect()
+    {
+        $this->_pdo = null;
+    }
+
+    public function getHost()
+    {
+        return $this->_host;
+    }
+    public function setHost($host)
+    {
+        $this->_host = $host;
+    }
+
+    public function getPort()
+    {
+        return $this->_port;
+    }
+
+    public function setPort($port)
+    {
+        $this->_port = $port;
+    }
+
+    public function getDatabase()
+    {
+        return $this->_database;
+    }
+
+    public function setDatabase($database)
+    {
+        $this->_database = $database;
+    }
+
+    public function getUsername()
+    {
+        return $this->_username;
+    }
 
+    public function setUsername($username)
+    {
+        $this->_username = $username;
+    }
+
+    public function getPassword()
+    {
+        return $this->_password;
+    }
+
+    public function setPassword($password)
+    {
+        $this->_password = $password;
     }
 }
 }

tests/DomainListPleskDBTest.php

 <?php
 /**
- * This file is part of Cursosf2Application.
+ * This file is part of pleskbackup.
  *
- * Cursosf2Application is free software: you can redistribute it and/or modify
+ * pleskbackup is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
  *
- * Cursosf2Application is distributed in the hope that it will be useful,
+ * pleskbackup is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with Cursosf2Application.  If not, see <http://www.gnu.org/licenses/>.
+ * along with pleskbackup.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 require_once dirname(__FILE__).'/../vendor/autoload.php';
 
 use \AAlba\PleskBackup\DomainList\Factory\PleskDBFactory;
 use \AAlba\PleskBackup\DomainList\Factory\DomainListFactoryInterface;
+use Symfony\Component\Yaml\Parser;
 
 class DomainListPleskDBTest extends PHPUnit_Framework_TestCase
 {
+    private $_factory;
+    public function setUp()
+    {
+        $this->_factory = new PleskDBFactory();
+    }
+
+    public function tearDown()
+    {
+        $this->_factory = null;
+    }
     public function testThatClassExits()
     {
         $factory = new PleskDBFactory();
         $this->assertTrue($factory instanceof DomainListFactoryInterface,'Implements DomainListFactoryInterface');
         $this->assertTrue($factory instanceof PleskDBFactory,'Instance of PleskDBFactory');
     }
+
+    public function testThatICanGetTheDatabaseConnectionParameters()
+    {
+        $this->_factory->setHost("FAKEHOST");
+        $this->_factory->setPort("FAKEPORT");
+        $this->_factory->setDatabase("FAKEDATABASE");
+        $this->_factory->setUsername("FAKEUSERNAME");
+        $this->_factory->setPassword("FAKEPASSWORD");
+
+        $this->assertEquals("FAKEHOST", $this->_factory->getHost(),'I get the host I set');
+        $this->assertEquals("FAKEPORT", $this->_factory->getPort(),'I get the port I set');
+        $this->assertEquals("FAKEDATABASE", $this->_factory->getDatabase(),'I get the database I set');
+        $this->assertEquals("FAKEUSERNAME", $this->_factory->getUsername(),'I get the username I set');
+        $this->assertEquals("FAKEPASSWORD", $this->_factory->getPassword(),'I get the password I set');
+    }
+
+    public function testSetDatabaseDataFromFile()
+    {
+        $file = dirname(__FILE__).'/config/pleskdb.yml';
+        $parser = new Parser();
+        $factory = new PleskDBFactory($file);
+
+        $testData = $parser->parse(file_get_contents($file));
+        $this->assertEquals($testData['host'], $factory->getHost());
+        $this->assertEquals($testData['port'], $factory->getPort());
+        $this->assertEquals($testData['database'], $factory->getDatabase());
+        $this->assertEquals($testData['username'], $factory->getUsername());
+        $this->assertEquals($testData['password'], $factory->getPassword());
+    }
+
+    public function testSetDatabaseDataFromArray()
+    {
+        $testData = array(
+            'host' => 'FAKEHOST',
+            'port' => 'FAKEPORT',
+            'database' => 'FAKEDATABASE',
+            'username' => 'FAKEUSERNAME',
+            'password' => 'FAKEPASSWORD'
+
+        );
+        $factory = new PleskDBFactory($testData);
+
+        $this->assertEquals($testData['host'], $factory->getHost());
+        $this->assertEquals($testData['port'], $factory->getPort());
+        $this->assertEquals($testData['database'], $factory->getDatabase());
+        $this->assertEquals($testData['username'], $factory->getUsername());
+        $this->assertEquals($testData['password'], $factory->getPassword());
+    }
 }

tests/config/pleskdb.yml

+host: FAKEHOST
+port: FAKEPORT
+database: FAKEDATABASE
+username: FAKEUSERNAME
+password: FAKEPASSWORD
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.