Skip to content

Commit

Permalink
Merge pull request #15003 from phalcon/fix/correct-database-tests
Browse files Browse the repository at this point in the history
Correct database tests
  • Loading branch information
sergeyklay authored May 4, 2020
2 parents abb5d64 + d2f4548 commit 930fc08
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 130 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- Changed the default ACL access level from boolean `FALSE` to `Enum::DENY` [#14974](https://github.com/phalcon/cphalcon/pull/14974)
- Changed the way `Phalcon\Http\Response::__construct` checks `content` data type. Now a `TypeError` will be thrown if incompatible data type was passed [#14983](https://github.com/phalcon/cphalcon/issues/14983)
- Changed return type hints of the following `Phalcon\Flash\FlashInterface`'s methods: `error`, `message`, `notice`, `success` and `warning` [#14994](https://github.com/phalcon/cphalcon/issues/14994)
- Changed return type hints for `Phalcon\Mvc\ModelInterface::sum` [#15000](https://github.com/phalcon/cphalcon/issues/15000)
- Changed return type hint for `Phalcon\Mvc\ModelInterface::sum` [#15000](https://github.com/phalcon/cphalcon/issues/15000)

## Fixed
- Fixed `Phalcon\Mvc\Model\Query\Builder::getPhql` to add single quote between string value on a simple condition [#14874](https://github.com/phalcon/cphalcon/issues/14874)
Expand Down
104 changes: 66 additions & 38 deletions tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Nanobox can also be used for developing Phalcon, and its installation
instructions can be found [in the Nanobox documentation][nanobox-doc].
Codeception can be installed using [Composer][composer].

```sh
```shell script
# run this command from project root
composer install --prefer-source
$ composer install --prefer-source
```

You can read more about installing and configuring Codeception from the
Expand Down Expand Up @@ -80,39 +80,64 @@ The PHP extensions enabled are:

## Docker Compose Way

### Setup databases

To generate the necessary database schemas, you need to run the relevant script:

```shell script
$ php tests/_ci/generate-db-schemas.php
```

### Run tests

First you need to re-generate base classes for all suites:

```shell script
$ ./vendor/bin/codecept build
```

You will also need to provide Codeception configuration and run Docker containers:

```shell script
# Create Codeception configuration
cp tests/_ci/.env.default .env
$ cp tests/_ci/.env.default .env

# Run Docker containers
cd tests
docker-compose up -d
cd ..

# Generate database schemas
php tests/_ci/generate-db-schemas.php
$ cd tests
$ docker-compose up -d
$ cd ..
```

# Generate Codeception classes for all suites
./vendor/bin/codecept build
Then, run the tests on a terminal:

# Run unit tests
./vendor/bin/codecept run unit
```shell script
./vendor/bin/codecept run
# OR
./vendor/bin/codecept run --debug # Detailed output
```

# Run CLI tests
./vendor/bin/codecept run cli
Execute `unit` test with `run unit` command:

# Run integration tests
./vendor/bin/codecept run integration
```shell script
$ ./vendor/bin/codecept run unit
```

To run database related tests you need to run the `database` suite specifying
the RDBMS and group:

```shell script
./vendor/bin/codecept run database -g common
./vendor/bin/codecept run database -g mysql --env mysql
./vendor/bin/codecept run database -g sqlite --env sqlite
./vendor/bin/codecept run database -g pgsql --env pgsql
$ ./vendor/bin/codecept run database -g common
$ ./vendor/bin/codecept run database -g mysql --env mysql
$ ./vendor/bin/codecept run database -g sqlite --env sqlite
$ ./vendor/bin/codecept run database -g pgsql --env pgsql
```

Available options:

```shell script
--env mysql
--env sqlite
--env pgsql
```

Note that certain tests are grouped as `common`. Those do not require a
Expand All @@ -134,15 +159,15 @@ is not there to allow for more flexibility. We have two setup files, one for
PHP 7.2 and one for PHP 7.3. If you wish to set up an environment for Phalcon
using PHP 7.2, you can copy the relevant file at the root of your folder:

```bash
cp -v ./tests/_ci/nanobox/boxfile.7.2.yml ./boxfile.yml
```shell script
$ cp -v ./tests/_ci/nanobox/boxfile.7.2.yml ./boxfile.yml
```

You can also create a 7.3 environment by copying the relevant file.

Run
```bash
nanobox run
```shell script
$ nanobox run
```

The process will take a while (for the first time) and once it is done you will
Expand Down Expand Up @@ -179,29 +204,30 @@ Preparing environment :
```

Now that zephir is in your environment, you can check it by typing:
```bash
```shell script
/app $ zephir
```

This should show you the help screen. You can now compile the extension:
```bash
```shell script
/app $ zephir fullclean
/app $ zephir build

```

After the compilation is completed, you can check if the extension is loaded:
```bash
```shell script
/app $ php -m | grep phalcon
```

### Setup databases

To generate the necessary database schemas, you need to run the relevant script:

```sh
```shell script
/app $ php ./tests/_ci/generage-db-schemas.php
```

The script looks for classes located under `tests/_data/fixtures/Migrations`.
These classes contain the necessary code to create the relevant SQL statements
for each RDBMS. You can easily inspect one of those files to understand its
Expand All @@ -221,50 +247,52 @@ tests.

First you need to re-generate base classes for all suites:

```sh
```shell script
/app $ codecept build
```

Then, run the tests on a terminal:

```sh
```shell script
/app $ codecept run
# OR
/app $ codecept run --debug # Detailed output
```

Execute `unit` test with `run unit` command:

```sh
```shell script
/app $ codecept run unit
```

Execute all tests from a folder:

```sh
```shell script
/app $ codecept run tests/unit/some/folder/
```

Execute single test:

```sh
```shell script
/app $ codecept run tests/unit/some/folder/some/test/file.php
```

To run database related tests you need to run the `database` suite specifying
the RDBMS and group:

```sh
```shell script
/app $ codecept run tests/database -g common
/app $ codecept run tests/database -g mysql --env mysql
/app $ codecept run tests/database -g sqlite --env sqlite
/app $ codecept run tests/database -g pgsql --env pgsql
```

Available options:

```sh
-- env mysql
-- env sqlite
```shell script
--env mysql
--env sqlite
--env pgsql
```

## Help
Expand Down
69 changes: 50 additions & 19 deletions tests/database/Db/Adapter/Pdo/DescribeColumnsCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
* For the full copyright and license information, please view the
* LICENSE.txt file that was distributed with this source code.
*/

declare(strict_types=1);
Expand All @@ -23,19 +23,47 @@ final class DescribeColumnsCest
use DiTrait;

/**
* @param DatabaseTester $I
* @var ComplexDefaultMigration
*/
private $migration;

/**
* Executed before each test
*
* @throws Exception
* @param DatabaseTester $I
* @return void
*/
public function _before(DatabaseTester $I)
public function _before(DatabaseTester $I): void
{
$this->setNewFactoryDefault();
try {
$this->setNewFactoryDefault();
} catch (Exception $e) {
$I->fail($e->getMessage());
}

$this->setDatabase($I);

$this->migration = new ComplexDefaultMigration($I->getConnection());
}

/**
* Executed after each test
*
* @param DatabaseTester $I
* @return void
*/
public function _after(DatabaseTester $I): void
{
if ($this->migration) {
$this->migration->clear();
}
}

/**
* Tests Phalcon\Db\Adapter\Pdo :: describeColumns()
*
* @param DatabaseTester $I
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-03-02
*
Expand All @@ -45,14 +73,12 @@ public function dbAdapterPdoDescribeColumnsOnUpdate(DatabaseTester $I)
{
$I->wantToTest('Db\Adapter\Pdo - describeColumns()');

$connection = $I->getConnection();
$db = $this->container->get('db');
$db = $this->container->get('db');
$now = date('Y-m-d H:i:s');

$now = date('Y-m-d H:i:s');
$migration = new ComplexDefaultMigration($connection);
$migration->insert(1, $now, $now);
$this->migration->insert(1, $now, $now);

$columns = $db->describeColumns($migration->getTable());
$columns = $db->describeColumns($this->migration->getTable());

$I->assertSame('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP', $columns[2]->getDefault());
$I->assertSame('NULL on update CURRENT_TIMESTAMP', $columns[3]->getDefault());
Expand All @@ -61,6 +87,8 @@ public function dbAdapterPdoDescribeColumnsOnUpdate(DatabaseTester $I)
/**
* Tests Phalcon\Db\Adapter\Pdo :: describeColumns()
*
* @param DatabaseTester $I
*
* @author Jeremy PASTOURET <https://github.com/jenovateurs>
* @since 2020-03-09
*
Expand All @@ -70,16 +98,19 @@ public function dbAdapterPdoDescribeColumnsDefaultPostgres(DatabaseTester $I)
{
$I->wantToTest('Db\Adapter\Pdo - describeColumns() - CheckPostgres Default value');

$connection = $I->getConnection();
$db = $this->container->get('db');
$db = $this->container->get('db');
$now = date('Y-m-d H:i:s');

$now = date('Y-m-d H:i:s');
$migration = new ComplexDefaultMigration($connection);
$migration->insert(1, $now, $now);
$this->migration->insert(1, $now, $now);

$columns = $db->describeColumns($migration->getTable());
$columns = $db->describeColumns($this->migration->getTable());

$I->assertSame('CURRENT_TIMESTAMP', $columns[1]->getDefault());
$I->assertSame('CURRENT_TIMESTAMP', $columns[2]->getDefault());

if ($I->getDriver() === 'mysql') {
$I->assertSame('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP', $columns[2]->getDefault());
} else {
$I->assertSame('CURRENT_TIMESTAMP', $columns[2]->getDefault());
}
}
}
Loading

0 comments on commit 930fc08

Please sign in to comment.