From 88de789d306a86d1a3656a9ced754eb36e3ac92c Mon Sep 17 00:00:00 2001 From: Fenikkusu Date: Tue, 20 Feb 2018 06:30:04 -0500 Subject: [PATCH] Tweaking tests --- .gitignore | 5 + Library/Phalcon/Test/Traits/ModelTestCase.php | 13 ++- codeception.yml => codeception.dist.yml | 0 docker-compose.yml | 37 +++++++ tests/README.md | 30 ++++++ tests/_data/dump.sql | 2 +- ...ike.suite.yml => aerospike.suite.dist.yml} | 0 ...unit.suite.5.yml => unit.suite.5.dist.yml} | 0 tests/{unit.suite.yml => unit.suite.dist.yml} | 0 tests/unit/Test/Traits/ModelTestCaseTest.php | 99 +++++++++++++++++++ tests/unit/Test/Traits/UnitTestCaseTest.php | 52 ++++++++++ ...unit5x.suite.yml => unit5x.suite.dist.yml} | 0 12 files changed, 233 insertions(+), 5 deletions(-) rename codeception.yml => codeception.dist.yml (100%) create mode 100644 docker-compose.yml rename tests/{aerospike.suite.yml => aerospike.suite.dist.yml} (100%) rename tests/{unit.suite.5.yml => unit.suite.5.dist.yml} (100%) rename tests/{unit.suite.yml => unit.suite.dist.yml} (100%) create mode 100644 tests/unit/Test/Traits/ModelTestCaseTest.php create mode 100644 tests/unit/Test/Traits/UnitTestCaseTest.php rename tests/{unit5x.suite.yml => unit5x.suite.dist.yml} (100%) diff --git a/.gitignore b/.gitignore index 7cd53b015..42ec4541e 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,8 @@ composer.lock /vendor/ +codeception.yml +tests/aerospike.suite.yml +tests/unit.suite.5.yml +tests/unit.suite.yml +tests/unit5x.suite.yml \ No newline at end of file diff --git a/Library/Phalcon/Test/Traits/ModelTestCase.php b/Library/Phalcon/Test/Traits/ModelTestCase.php index b39e80eae..836ef5174 100644 --- a/Library/Phalcon/Test/Traits/ModelTestCase.php +++ b/Library/Phalcon/Test/Traits/ModelTestCase.php @@ -18,6 +18,7 @@ namespace Phalcon\Test\Traits; +use Phalcon\Config; use Phalcon\Mvc\Model\Manager as PhModelManager; use Phalcon\Mvc\Model\Metadata\Memory as PhMetadataMemory; @@ -58,21 +59,25 @@ function () { */ protected function setDb($dbType = 'mysql') { - $config = $this->config; - if ($this->di->has('db')) { $db = $this->di->get('db'); $class = 'Phalcon\Db\Adapter\Pdo\\' . ucfirst($dbType); - if (get_class($db) == $class) { + if ($db instanceof $class) { return $db; } } + $config = $this->config ?: $this->getConfig(); + // Set the connection to whatever we chose $this->di->set( 'db', function () use ($dbType, $config) { - $params = $config['db'][$dbType]; + $params = isset($config['db'][$dbType]) ? $config['db'][$dbType] : $config['db']; + if ($params instanceof Config) { + $params = $params->toArray(); + } + $class = 'Phalcon\Db\Adapter\Pdo\\' . ucfirst($dbType); $conn = new $class($params); diff --git a/codeception.yml b/codeception.dist.yml similarity index 100% rename from codeception.yml rename to codeception.dist.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 000000000..1db56a72d --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,37 @@ +version: "3" +services: + db: + image: mysql + environment: + MYSQL_RANDOM_ROOT_PASSWORD: "yes" + MYSQL_DATABASE: "incubator" + MYSQL_USER: "incubator" + MYSQL_PASSWORD: "secret" + ports: + - "3306:3306" + + aerospike: + image: aerospike + ports: + - "3000:3000" + + redis: + image: redis + ports: + - "6379:6379" + + beanstalk: + image: schickling/beanstalkd + ports: + - "11300:11300" + memcached: + image: memcached + ports: + - "11211:11211" + + mongodb: + image: mongo + environment: + MONGO_INITDB_DATABASE: incubator + ports: + - "27017:27017" \ No newline at end of file diff --git a/tests/README.md b/tests/README.md index ec18ccc73..645ec24ae 100644 --- a/tests/README.md +++ b/tests/README.md @@ -33,6 +33,14 @@ You may need the following services to run other tests: * MongoDB * Beanstalk +## Docker Compose + +As an alternative to installing the above requirements, you may also utilize the included docker-compose.yml. This requires docker-compose and docker to be installed. You will need to update your `tests/.env` file appropriately to point to the resulting docker containers. You may start the containers by running: + +```bash +docker-compose up +``` + ## Run tests First you need to re-generate base classes for test all suites: @@ -77,6 +85,28 @@ To run single test: vendor/bin/codecept run tests/unit/some/folder/some/test/file.php ``` +## Advanced Configuration +The test suites ship with `.dist.yml` configuration files: + + ``` + codeception.dist.yml + tests/aerospike.suite.dist.yml + tests/unit.suite.5.dist.yml + tests/unit.suite.dist.yml + tests/unit5x.suite.dist.yml + ``` + + You may override the options in each of these files by creating a new configuration file of the same name, without the `.dist`. For example, to auto-populate/reset the database on every unit test, create the file `tests/unit.suite.yml` with: + + ```yaml + modules: + config: + Db: + populate: true + ``` + +For additional configuration options, see http://codeception.com. + ## Help **Note:** Cache-related tests are slower than others tests because they use wait states (sleep command) to expire generated caches. diff --git a/tests/_data/dump.sql b/tests/_data/dump.sql index ae16c2640..093d74c28 100644 --- a/tests/_data/dump.sql +++ b/tests/_data/dump.sql @@ -1283,7 +1283,7 @@ CREATE TABLE `audit_detail` ( PRIMARY KEY (id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -DROP TABLE if EXISTS `session`; +DROP TABLE if EXISTS `sessions`; CREATE TABLE `sessions` ( session_id TEXT NOT NULL, data TEXT, diff --git a/tests/aerospike.suite.yml b/tests/aerospike.suite.dist.yml similarity index 100% rename from tests/aerospike.suite.yml rename to tests/aerospike.suite.dist.yml diff --git a/tests/unit.suite.5.yml b/tests/unit.suite.5.dist.yml similarity index 100% rename from tests/unit.suite.5.yml rename to tests/unit.suite.5.dist.yml diff --git a/tests/unit.suite.yml b/tests/unit.suite.dist.yml similarity index 100% rename from tests/unit.suite.yml rename to tests/unit.suite.dist.yml diff --git a/tests/unit/Test/Traits/ModelTestCaseTest.php b/tests/unit/Test/Traits/ModelTestCaseTest.php new file mode 100644 index 000000000..38c428d29 --- /dev/null +++ b/tests/unit/Test/Traits/ModelTestCaseTest.php @@ -0,0 +1,99 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Test\Test\Traits; + +use Phalcon\Config; +use Phalcon\Db\Adapter\Pdo\Mysql; +use Phalcon\Di\FactoryDefault; +use Phalcon\Test\Codeception\ModelTestCase as ModelTest; +use Phalcon\Test\Traits\ModelTestCase; + +class ModelTestCaseTest extends ModelTest +{ + /** @var ModelTestCase */ + protected $testSubject = null; + + public function _before() + { + $this->testSubject = $this->di->get(ModelTest::class); + } + + public function testDbWithDb() + { + $mockDb = $this->getMockBuilder(Mysql::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->di = $this->getMockBuilder(FactoryDefault::class) + ->disableOriginalConstructor() + ->setMethods(['has', 'get']) + ->getMock(); + + $this->di->expects($this->once()) + ->method('has') + ->with('db') + ->willReturn(true); + + $this->di->expects($this->once()) + ->method('get') + ->willReturn($mockDb); + + $this->testSubject->setDI($this->di); + + $reflectionMethod = new \ReflectionMethod(ModelTest::class, 'setDb'); + $reflectionMethod->setAccessible(true); + + $this->assertSame($mockDb, $reflectionMethod->invoke($this->testSubject)); + } + + public function testDbWithoutConfig() + { + $this->testSubject = $this->getMockBuilder(ModelTest::class) + ->setMethods(['getConfig']) + ->getMock(); + + $this->testSubject->config = $this->getMockBuilder(Config::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->testSubject->expects($this->never()) + ->method('getConfig'); + + $this->di = $this->getMockBuilder(FactoryDefault::class) + ->disableOriginalConstructor() + ->setMethods(['has', 'set']) + ->getMock(); + + $this->di->expects($this->once()) + ->method('has') + ->with('db') + ->willReturn(false); + + $this->di->expects($this->once()) + ->method('set') + ->with('db', $this->isInstanceOf(\Closure::class)); + + $this->testSubject->setDI($this->di); + + $reflectionMethod = new \ReflectionMethod(ModelTest::class, 'setDb'); + $reflectionMethod->setAccessible(true); + + $reflectionMethod->invoke($this->testSubject); + } +} \ No newline at end of file diff --git a/tests/unit/Test/Traits/UnitTestCaseTest.php b/tests/unit/Test/Traits/UnitTestCaseTest.php new file mode 100644 index 000000000..076592498 --- /dev/null +++ b/tests/unit/Test/Traits/UnitTestCaseTest.php @@ -0,0 +1,52 @@ + | + +------------------------------------------------------------------------+ +*/ + +namespace Phalcon\Test\Test\Traits; + +use Phalcon\Test\Codeception\UnitTestCase as Unit; +use Phalcon\Config; +use Phalcon\Test\Traits\UnitTestCase; +use PHPUnit_Framework_MockObject_MockObject; + +class UnitTestCaseTest extends Unit +{ + /** @var UnitTestCase|\PHPUnit_Framework_MockObject_MockObject */ + protected $testSubject = null; + + public function _before() + { + $this->testSubject = $this->getMockBuilder( + UnitTestCase::class + )->getMockForTrait(); + } + + public function testConfig() + { + $this->tester->amGoingTo('Confirm Testing Fallback Works'); + + /** @var Config|PHPUnit_Framework_MockObject_MockObject $mockConfig */ + $mockConfig = $this->getMockBuilder(Config::class)->getMock(); + + $this->assertNull($this->testSubject->getConfig()); + + $this->di->set('config', $mockConfig); + + $this->assertSame($this->testSubject, $this->testSubject->setConfig($mockConfig)); + $this->assertSame($mockConfig, $this->testSubject->getConfig()); + } +} \ No newline at end of file diff --git a/tests/unit5x.suite.yml b/tests/unit5x.suite.dist.yml similarity index 100% rename from tests/unit5x.suite.yml rename to tests/unit5x.suite.dist.yml