Skip to content

Commit

Permalink
Tweaking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenikkusu authored and sergeyklay committed May 30, 2018
1 parent 3f2dbed commit 88de789
Show file tree
Hide file tree
Showing 12 changed files with 233 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 9 additions & 4 deletions Library/Phalcon/Test/Traits/ModelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down
File renamed without changes.
37 changes: 37 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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"
30 changes: 30 additions & 0 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tests/_data/dump.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
99 changes: 99 additions & 0 deletions tests/unit/Test/Traits/ModelTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| 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 [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Phoenix Osiris <[email protected]> |
+------------------------------------------------------------------------+
*/

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);
}
}
52 changes: 52 additions & 0 deletions tests/unit/Test/Traits/UnitTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2016 Phalcon Team (https://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file LICENSE.txt. |
| |
| 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 [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Phoenix Osiris <[email protected]> |
+------------------------------------------------------------------------+
*/

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());
}
}
File renamed without changes.

0 comments on commit 88de789

Please sign in to comment.