From 02d2a20efbaef1d98493b033a4e69d685d68acb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Morais?= Date: Wed, 18 Dec 2013 00:05:54 +0000 Subject: [PATCH] Increase code coverage on bootstrap levels --- composer.json | 3 +- composer.lock | 502 +++++++++++++++++++++++++- src/Insulin/Sugar/Sugar.php | 102 ++++-- src/Insulin/Sugar/SugarInterface.php | 6 +- src/Insulin/Sugar/Tests/SugarTest.php | 300 ++++++++++++++- 5 files changed, 850 insertions(+), 63 deletions(-) diff --git a/composer.json b/composer.json index 19691c8..7b1eecc 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,8 @@ "symfony/event-dispatcher": "2.4", "symfony/finder": "2.4.*", "symfony/locale": "2.4.*", - "symfony/process": "2.4.*" + "symfony/process": "2.4.*", + "doctrine/dbal": "2.4.*" }, "require-dev": { "satooshi/php-coveralls": "dev-master" diff --git a/composer.lock b/composer.lock index 75375b7..bb2ef88 100644 --- a/composer.lock +++ b/composer.lock @@ -3,8 +3,492 @@ "This file locks the dependencies of your project to a known state", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file" ], - "hash": "a31022e96d08e91e203e31f5323c911f", + "hash": "16b9d8c44bb07c91978406c30672c82e", "packages": [ + { + "name": "doctrine/annotations", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/annotations.git", + "reference": "a11349d39d85bef75a71bd69bd604ac4fb993f03" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/annotations/zipball/a11349d39d85bef75a71bd69bd604ac4fb993f03", + "reference": "a11349d39d85bef75a71bd69bd604ac4fb993f03", + "shasum": "" + }, + "require": { + "doctrine/lexer": "1.*", + "php": ">=5.3.2" + }, + "require-dev": { + "doctrine/cache": "1.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Annotations\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Docblock Annotations Parser", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "annotations", + "docblock", + "parser" + ], + "time": "2013-12-20 21:39:07" + }, + { + "name": "doctrine/cache", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "36c4eee5051629524389da376ba270f15765e49f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/36c4eee5051629524389da376ba270f15765e49f", + "reference": "36c4eee5051629524389da376ba270f15765e49f", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "phpunit/phpunit": ">=3.7", + "satooshi/php-coveralls": "~0.6" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Cache\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Caching library offering an object-oriented API for many cache backends", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "cache", + "caching" + ], + "time": "2013-12-18 17:21:03" + }, + { + "name": "doctrine/collections", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/collections.git", + "reference": "819871759a4d41dab244d358507bd27d0e8b1e33" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/collections/zipball/819871759a4d41dab244d358507bd27d0e8b1e33", + "reference": "819871759a4d41dab244d358507bd27d0e8b1e33", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.2.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Collections\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Collections Abstraction library", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "array", + "collections", + "iterator" + ], + "time": "2013-12-21 19:19:27" + }, + { + "name": "doctrine/common", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/common.git", + "reference": "d2d2e0a64d4686dd0908c9226c8acfe5bcb4089f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/common/zipball/d2d2e0a64d4686dd0908c9226c8acfe5bcb4089f", + "reference": "d2d2e0a64d4686dd0908c9226c8acfe5bcb4089f", + "shasum": "" + }, + "require": { + "doctrine/annotations": "1.*", + "doctrine/cache": "1.*", + "doctrine/collections": "1.*", + "doctrine/inflector": "1.*", + "doctrine/lexer": "1.*", + "php": ">=5.3.2" + }, + "require-dev": { + "phpunit/phpunit": "~3.7" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.5.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Common Library for Doctrine projects", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "annotations", + "collections", + "eventmanager", + "persistence", + "spl" + ], + "time": "2013-12-23 05:55:47" + }, + { + "name": "doctrine/dbal", + "version": "2.4.x-dev", + "source": { + "type": "git", + "url": "https://github.com/doctrine/dbal.git", + "reference": "7195d201b86daf071fc50f7c47450265afc7e310" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/dbal/zipball/7195d201b86daf071fc50f7c47450265afc7e310", + "reference": "7195d201b86daf071fc50f7c47450265afc7e310", + "shasum": "" + }, + "require": { + "doctrine/common": "~2.4", + "php": ">=5.3.2" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*", + "symfony/console": "~2.0" + }, + "suggest": { + "symfony/console": "Allows use of the command line interface" + }, + "type": "library", + "autoload": { + "psr-0": { + "Doctrine\\DBAL\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan H. Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + } + ], + "description": "Database Abstraction Layer", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "database", + "dbal", + "persistence", + "queryobject" + ], + "time": "2013-12-22 10:50:36" + }, + { + "name": "doctrine/inflector", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/inflector.git", + "reference": "a81c334f2764b09e2f13a55cfd8fe3233946f728" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/inflector/zipball/a81c334f2764b09e2f13a55cfd8fe3233946f728", + "reference": "a81c334f2764b09e2f13a55cfd8fe3233946f728", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Inflector\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com", + "homepage": "http://www.jwage.com/", + "role": "Creator" + }, + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Johannes M. Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Common String Manipulations with regard to casing and singular/plural rules.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "inflection", + "pluralize", + "singularize", + "string" + ], + "time": "2013-12-21 19:19:50" + }, + { + "name": "doctrine/lexer", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/doctrine/lexer.git", + "reference": "f12a5f74e5f4a9e3f558f3288504e121edfad891" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/lexer/zipball/f12a5f74e5f4a9e3f558f3288504e121edfad891", + "reference": "f12a5f74e5f4a9e3f558f3288504e121edfad891", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-0": { + "Doctrine\\Common\\Lexer\\": "lib/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com", + "homepage": "http://www.instaclick.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com", + "homepage": "http://jmsyst.com", + "role": "Developer of wrapped JMSSerializerBundle" + } + ], + "description": "Base library for a lexer that can be used in Top-Down, Recursive Descent Parsers.", + "homepage": "http://www.doctrine-project.org", + "keywords": [ + "lexer", + "parser" + ], + "time": "2013-12-20 21:39:00" + }, { "name": "symfony/config", "version": "2.4.x-dev", @@ -12,12 +496,12 @@ "source": { "type": "git", "url": "https://github.com/symfony/Config.git", - "reference": "16068f76c0af74968f3ad8fcec3eb90df1fde394" + "reference": "95ce2f091fc8bc6e04217b41a5bb089609706f50" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/symfony/Config/zipball/16068f76c0af74968f3ad8fcec3eb90df1fde394", - "reference": "16068f76c0af74968f3ad8fcec3eb90df1fde394", + "url": "https://api.github.com/repos/symfony/Config/zipball/95ce2f091fc8bc6e04217b41a5bb089609706f50", + "reference": "95ce2f091fc8bc6e04217b41a5bb089609706f50", "shasum": "" }, "require": { @@ -51,7 +535,7 @@ ], "description": "Symfony Config Component", "homepage": "http://symfony.com", - "time": "2013-11-26 16:40:27" + "time": "2013-12-14 12:09:49" }, { "name": "symfony/console", @@ -539,12 +1023,12 @@ "source": { "type": "git", "url": "https://github.com/guzzle/guzzle.git", - "reference": "50c5393062bc9035d0a872b9b2af8221f264ea19" + "reference": "e0df12243ee08a4d6fca3241142212428dfd632e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/guzzle/guzzle/zipball/50c5393062bc9035d0a872b9b2af8221f264ea19", - "reference": "50c5393062bc9035d0a872b9b2af8221f264ea19", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/e0df12243ee08a4d6fca3241142212428dfd632e", + "reference": "e0df12243ee08a4d6fca3241142212428dfd632e", "shasum": "" }, "require": { @@ -623,7 +1107,7 @@ "rest", "web service" ], - "time": "2013-12-09 04:01:52" + "time": "2013-12-22 22:25:34" }, { "name": "psr/log", diff --git a/src/Insulin/Sugar/Sugar.php b/src/Insulin/Sugar/Sugar.php index 12210b9..8582e6f 100644 --- a/src/Insulin/Sugar/Sugar.php +++ b/src/Insulin/Sugar/Sugar.php @@ -12,6 +12,8 @@ namespace Insulin\Sugar; +use Doctrine\DBAL\DriverManager; + /** * Abstraction to handle a SugarCRM instance. * @@ -92,9 +94,7 @@ public function bootRoot() { defined('sugarEntry') || define('sugarEntry', true); - set_include_path( - get_include_path() . PATH_SEPARATOR . $this->getPath() - ); + chdir($this->getPath()); } /** @@ -133,41 +133,37 @@ public function bootDatabase() { $config = $this->bootConfig(); - if (!isset($config['dbconfig']) || empty($config['dbconfig'])) { + if (empty($config['dbconfig'])) { throw new \RuntimeException( - 'Unable to connect to database, undefined configuration data.' + 'Unable to boot database, undefined configuration data.' ); } - $type = $config['dbconfig']['db_type']; - if (!in_array($type, \PDO::getAvailableDrivers())) { - throw new \RuntimeException( - sprintf( - "Unable to connect to database, unsupported driver '%s'.", - $type - ) - ); + // FIXME: need to translate sugar db types to doctrine db types + // we're currently only supporting mysql + if ($config['dbconfig']['db_type'] === 'mysql') { + $config['dbconfig']['db_type'] = 'pdo_mysql'; } - $hostname = $config['dbconfig']['db_host_name']; - $port = $config['dbconfig']['db_port']; - $username = $config['dbconfig']['db_user_name']; - $password = $config['dbconfig']['db_password']; - $database = $config['dbconfig']['db_name']; - - $dbh = new \PDO( - sprintf( - '%s:host=%s;port=%s;dbname=%s', - $type, - $hostname, - $port, - $database - ), - $username, - $password + $params = array( + 'dbname' => $config['dbconfig']['db_name'], + 'driver' => $config['dbconfig']['db_type'], + 'host' => $config['dbconfig']['db_host_name'], + 'password' => $config['dbconfig']['db_password'], + 'port' => $config['dbconfig']['db_port'], + 'user' => $config['dbconfig']['db_user_name'], ); - return $dbh; + $wrapper = $this->getDatabaseWrapper(); + if (!empty($wrapper)) { + $params['wrapperClass'] = $wrapper; + } + + $connection = DriverManager::getConnection($params); + $connection->connect(); + $connection->close(); + + return $connection; } /** @@ -197,20 +193,19 @@ public function bootApplication() */ public function localLogin($username = '') { - $factory = \BeanFactory::getBean('Users'); + $manager = $this->getBean('Users'); if (empty($username)) { - $user = $factory->getSystemUser(); + $user = $manager->getSystemUser(); } else { - $user = $factory->retrieve_by_string_fields( + $user = $manager->retrieve_by_string_fields( array('user_name' => $username) ); } if (!empty($user)) { $GLOBALS['current_user'] = $user; - return $user; } @@ -222,10 +217,41 @@ public function localLogin($username = '') } throw new \RuntimeException( - sprintf( - "Cannot login as '%s', user not found.", - $username - ) + sprintf("Cannot login as '%s', user not found.", $username) ); } + + /** + * Retrieves database wrapper. + * + * @return \Doctrine\DBAL\Connection|null + * Wrapper class or `null` if none. + */ + protected function getDatabaseWrapper() + { + return null; + } + + /** + * Retrieves bean based on supplied module name. + * + * @param string $module + * Module name. + * + * @return \SugarBean + * Bean instance. + * + * @throws \RuntimeException + * If no matching bean for supplied module name. + */ + protected function getBean($module) + { + $bean = \BeanFactory::getBean($module); + + if (empty($bean)) { + throw new \RuntimeException( + sprintf("Unable to retrieve bean for '%s' module.", $module) + ); + } + } } diff --git a/src/Insulin/Sugar/SugarInterface.php b/src/Insulin/Sugar/SugarInterface.php index 368dbfd..06a19e4 100644 --- a/src/Insulin/Sugar/SugarInterface.php +++ b/src/Insulin/Sugar/SugarInterface.php @@ -87,11 +87,11 @@ public function bootConfig(); /** * Boots database. * - * @return PDO - * Database handler. + * @return \Doctrine\DBAL\Connection + * Wrapper class. * * @throws \RuntimeException - * If database config is invalid or an unsupported driver is supplied. + * If database configuration data is undefined. */ public function bootDatabase(); diff --git a/src/Insulin/Sugar/Tests/SugarTest.php b/src/Insulin/Sugar/Tests/SugarTest.php index 633643f..1a0e135 100644 --- a/src/Insulin/Sugar/Tests/SugarTest.php +++ b/src/Insulin/Sugar/Tests/SugarTest.php @@ -29,6 +29,8 @@ abstract class SugarTest extends \PHPUnit_Framework_TestCase protected static $root; protected static $files; + protected $sugar; + public static function setupBeforeClass() { static::$root = sys_get_temp_dir() . '/insulin'; @@ -72,6 +74,28 @@ public static function tearDownAfterClass() @rmdir(static::$root); } + protected function setUp() + { + file_put_contents( + self::$root . '/sugar/sugar_version.php', + 'sugar = $manager->get(static::$root . '/sugar'); + } + + protected function tearDown() + { + file_put_contents(self::$root . '/sugar/sugar_version.php', ''); + + $this->sugar = null; + } + /** * @dataProvider providerGetInfo */ @@ -92,13 +116,8 @@ public function testGetInfo($property, $expectedValue, $expectedException = null $this->setExpectedException($expectedException); } - $manager = new Manager(); - - /* @var $sugar \Insulin\Sugar\SugarInterface */ - $sugar = $manager->get(static::$root . '/sugar'); - - $this->assertInstanceOf('\Insulin\Sugar\Sugar', $sugar); - $this->assertEquals($expectedValue, $sugar->getInfo($property)); + $this->assertInstanceOf('\Insulin\Sugar\Sugar', $this->sugar); + $this->assertEquals($expectedValue, $this->sugar->getInfo($property)); } public function providerGetInfo() @@ -125,14 +144,271 @@ public function testGetInfoUnsupportedProperty() file_put_contents( self::$root . '/sugar/sugar_version.php', 'get(static::$root . '/sugar'); - /* @var $sugar Finder */ - $sugar->getInfo('flavor', true); + $this->sugar->getInfo('flavor', true); + } + + public function testBootRoot() + { + $this->sugar->bootRoot(); + + $this->assertTrue(defined('sugarEntry')); + $this->assertEquals(realpath($this->sugar->getPath()), getcwd()); + } + + public function testBootConfig() + { + file_put_contents( + static::$root . '/sugar/config.php', + ' \'Home\');' + ); + + $this->sugar->bootRoot(); + $this->sugar->bootConfig(true); + + file_put_contents( + static::$root . '/sugar/config.php', + '' + ); + + $expectedValue = array('default_module' => 'Home'); + + $this->assertEquals($expectedValue, $this->sugar->bootConfig()); + } + + public function testBootConfigWithOverride() + { + file_put_contents( + static::$root . '/sugar/config.php', + ' \'Home\');' + ); + + file_put_contents( + static::$root . '/sugar/config_override.php', + 'sugar->bootRoot(); + + $expectedValue = array('default_module' => 'Accounts'); + + $this->assertEquals($expectedValue, $this->sugar->bootConfig(true)); + + file_put_contents(static::$root . '/sugar/config.php', ''); + + unlink(static::$root . '/sugar/config_override.php'); + } + + /** + * @expectedException \RuntimeException + */ + public function testBootConfigFailure() + { + @unlink(static::$root . '/sugar/config.php'); + + $this->sugar->bootRoot(); + $this->sugar->bootConfig(true); + + touch(static::$root . '/sugar/config.php'); + } + + public function testBootDatabase() + { + $config = array( + 'dbconfig' => array( + 'db_name' => 'sugardb', + 'db_type' => 'mysql', + 'db_host_name' => 'localhost', + 'db_password' => 'sugaronsteroids', + 'db_port' => '', + 'db_user_name' => 'insulin', + ), + ); + + $wrapper = $this->getMockBuilder('\Doctrine\DBAL\Connection') + ->setMethods(array('connect', 'close')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('bootConfig', 'getDatabaseWrapper')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->once())->method('bootConfig')->will( + $this->returnValue($config) + ); + + $sugar->expects($this->once())->method('getDatabaseWrapper')->will( + $this->returnValue($wrapper) + ); + + $connection = $sugar->bootDatabase(); + + $this->assertEquals($config['dbconfig']['db_name'], $connection->getDatabase()); + $this->assertEquals('pdo_mysql', $connection->getDriver()->getName()); + $this->assertEquals($config['dbconfig']['db_host_name'], $connection->getHost()); + $this->assertEquals($config['dbconfig']['db_password'], $connection->getPassword()); + $this->assertEquals($config['dbconfig']['db_user_name'], $connection->getUsername()); + $this->assertEquals($config['dbconfig']['db_port'], $connection->getPort()); + } + + /** + * @dataProvider providerBootDatabaseFailure + */ + public function testBootDatabaseFailure($config, $expectedException) + { + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('bootConfig')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->any())->method('bootConfig')->will( + $this->returnValue($config) + ); + + $this->setExpectedException($expectedException); + + $sugar->bootDatabase(); + } + + public function providerBootDatabaseFailure() + { + return array( + array( + array(), + 'RuntimeException', + ), + array( + array( + 'dbconfig' => array( + 'db_type' => '', + 'db_host_name' => '', + 'db_port' => '', + 'db_user_name' => '', + 'db_password' => '', + 'db_name' => '', + ), + ), + 'Doctrine\DBAL\DBALException', + ), + ); + } + + public function testBootApplication() + { + $this->sugar->bootRoot(); + $this->sugar->bootApplication(); + + $files = get_included_files(); + + $this->assertContains( + realpath(static::$root . '/sugar/include/entryPoint.php'), + $files + ); + $this->assertContains( + realpath(static::$root . '/sugar/include/MVC/SugarApplication.php'), + $files + ); + } + + public function testLocalLoginWithoutUsernameReturnsSystemUser() + { + $user = new \stdClass; + + $bean = $this->getMockBuilder('\stdClass') + ->setMethods(array('getSystemUser')) + ->getMock(); + + $bean->expects($this->once())->method('getSystemUser')->will( + $this->returnValue($user) + ); + + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('getBean')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->once())->method('getBean')->will( + $this->returnValue($bean) + ); + + $this->assertEquals($user, $sugar->localLogin()); + $this->assertEquals($user, $GLOBALS['current_user']); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Cannot login as administrator. Please check + * that you have at least one administrator created on your instance. + */ + public function testLocalLoginWithoutUsernameFailure() + { + $bean = $this->getMockBuilder('\stdClass') + ->setMethods(array('getSystemUser')) + ->getMock(); + + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('getBean')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->once())->method('getBean')->will( + $this->returnValue($bean) + ); + + $sugar->localLogin(); + } + + public function testLocalLoginWithUsername() + { + $user = new \stdClass; + $user->user_name = 'yoda'; + + $bean = $this->getMockBuilder('\stdClass') + ->setMethods(array('retrieve_by_string_fields')) + ->getMock(); + + $bean->expects($this->once())->method('retrieve_by_string_fields')->will( + $this->returnValue($user) + ); + + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('getBean')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->once())->method('getBean')->will( + $this->returnValue($bean) + ); + + $this->assertEquals($user, $sugar->localLogin($user->user_name)); + $this->assertEquals($user, $GLOBALS['current_user']); + } + + /** + * @expectedException \RuntimeException + * @expectedExceptionMessage Cannot login as 'yoda', user not found. + */ + public function testLocalLoginWithUsernameFailure() + { + $bean = $this->getMockBuilder('\stdClass') + ->setMethods(array('retrieve_by_string_fields')) + ->getMock(); + + $sugar = $this->getMockBuilder('\Insulin\Sugar\Sugar') + ->setMethods(array('getBean')) + ->disableOriginalConstructor() + ->getMock(); + + $sugar->expects($this->once())->method('getBean')->will( + $this->returnValue($bean) + ); + + $sugar->localLogin('yoda'); } }