Skip to content

Commit

Permalink
init unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Supply.Parts committed May 3, 2017
1 parent d9356c9 commit bbe4699
Show file tree
Hide file tree
Showing 23 changed files with 995 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Ignore all test and documentation for archive
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/docs export-ignore
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
.idea

/vendor
/composer.lock

# phpunit
phpunit.phar
/phpunit.xml
/tests/runtime
37 changes: 37 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

# faster builds on new travis setup not using sudo
sudo: false

# cache vendor dirs
cache:
directories:
- $HOME/.composer/cache

install:
- travis_retry composer self-update && composer --version
- travis_retry composer global require "fxp/composer-asset-plugin:^1.3.1"
- export PATH="$HOME/.composer/vendor/bin:$PATH"
- travis_retry composer install --prefer-dist --no-interaction

before_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
PHPUNIT_FLAGS="--coverage-clover=coverage.clover"
fi
script:
- phpunit --verbose $PHPUNIT_FLAGS

after_script:
- |
if [ $TRAVIS_PHP_VERSION = '5.6' ]; then
travis_retry wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
fi
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
31 changes: 31 additions & 0 deletions tests/AbstactColumnSchemaArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace tigrov\tests\unit\pgsql;

abstract class AbstractColumnSchemaArrayTest extends AbstractColumnSchemaTest
{
/**
* @dataProvider arrayValuesProvider
*/
public function testArrayDbTypecast($expected, $value)
{
$this->assertEquals($expected, $this->fixture->dbTypecast($value));
}

/**
* @dataProvider arrayValuesProvider
*/
public function testArrayPhpTypecast($value, $expected)
{
$this->assertEquals($expected, $this->fixture->phpTypecast($value));
}

public function arrayValuesProvider()
{
return [
['{}', []],
['{NULL}', [null]],
['{NULL,NULL,NULL}', [null, null, null]],
];
}
}
48 changes: 48 additions & 0 deletions tests/AbstractColumnSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace tigrov\tests\unit\pgsql;

use tigrov\pgsql\ColumnSchema;

abstract class AbstractColumnSchemaTest extends TestCase
{
/**
* @var ColumnSchema
*/
protected $fixture;

protected function tearDown()
{
parent::tearDown();

$this->fixture = null;
}

public function testNullDbTypecast()
{
$this->assertNull($this->fixture->dbTypecast(null));
}

/**
* @dataProvider valuesProvider
*/
public function testDbTypecast($expected, $value)
{
$this->assertEquals($expected, $this->fixture->dbTypecast($value));
}

public function testNullPhpTypecast()
{
$this->assertNull($this->fixture->phpTypecast(null));
}

/**
* @dataProvider valuesProvider
*/
public function testPhpTypecast($value, $expected)
{
$this->assertEquals($expected, $this->fixture->phpTypecast($value));
}

abstract public function valuesProvider();
}
145 changes: 145 additions & 0 deletions tests/ActiveRecordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<?php

namespace tigrov\tests\unit\pgsql;

use tigrov\tests\unit\pgsql\data\Datatypes;
use yii\helpers\ArrayHelper;

class ActiveRecordTest extends TestCase
{
protected function setUp()
{
parent::setUp();

$config = ArrayHelper::merge(
require(__DIR__ . '/data/config.php'),
require(__DIR__ . '/data/config.local.php'));

$this->mockApplication($config);
$this->createDatatypesTable();
}

protected function tearDown()
{
$this->dropDatatypesTable();

parent::tearDown();
}

public function testNull()
{
$model = new Datatypes;
foreach ($model->attributes() as $attribute) {
if ($attribute != 'id') {
$model->$attribute = null;
}
}

$model->save();

$newModel = Datatypes::findOne($model->id);
foreach ($newModel->attributes() as $attribute) {
if ($attribute != 'id') {
$this->assertNull($newModel->$attribute);
}
}
}

/**
* @dataProvider arrayValuesProvider
*/
public function testArrayTypes($value)
{
$attributes = [
'strings',
'integers',
'numerics',
'doubles',
'booleans',
'bits',
'datetimes',
];

$model = new Datatypes;
foreach ($attributes as $attribute) {
$model->$attribute = $value;
}

$model->save();

$newModel = Datatypes::findOne($model->id);
foreach ($attributes as $attribute) {
$this->assertEquals($value, $newModel->$attribute);
}
}

/**
* @dataProvider valuesProvider
*/
public function testTypes($attribute, $value)
{
$model = new Datatypes;
$model->$attribute = $value;
$model->save();

$this->assertEquals($value, Datatypes::findOne($model->id)->$attribute);
}

public function arrayValuesProvider()
{
return [
[[]],
[[null]],
[[null, null, null]],
];
}

public function valuesProvider()
{
return [
['strings', ['']],
['strings', ['string1','str\\in"g2','str,ing3']],
['strings', ['null','NULL',null]],
['integers', [0]],
['integers', [-1]],
['integers', [1,2,3]],
['numerics', ['0']],
['numerics', ['0.00']],
['numerics', ['-1.5']],
['numerics', ['-1.50']],
['numerics', ['1.50', '-1.50', null]],
['doubles', [0]],
['doubles', [-1.5]],
['doubles', [1.5, -1.5, null]],
['booleans', [true]],
['booleans', [false]],
['booleans', [true, false, null]],
['bit', 0],
['bit', 1],
['bit', 8],
['bit', 15],
['bits', [0]],
['bits', [1]],
['bits', [8, 15, null]],
['datetime', new \DateTime('1901-01-01')],
['datetime', new \DateTime('2017-05-02 17:50:32')],
['datetimes', [new \DateTime('1901-01-01')]],
['datetimes', [new \DateTime('2017-05-02 17:50:32')]],
['datetimes', [new \DateTime('1901-01-01'), new \DateTime('2017-05-02 17:50:32')]],
['json', []],
['json', ''],
['json', true],
['json', false],
['json', 0],
['json', 1.5],
['json', -1.5],
['json', 'string'],
['json', ['']],
['json', ['string']],
['json', ['string',0, false, null]],
['json', ['key' => 'value']],
['json', ['key1' => 'value1', 'key2' => true, 'key3' => false, 'key4' => '', 'key5' => null]],
['json', ['key' => ['key' => ['key' => 'value']]]],
];
}
}
87 changes: 87 additions & 0 deletions tests/ArrayConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace tigrov\tests\unit\pgsql;

use tigrov\pgsql\ArrayConverter;

class ArrayConverterTest extends TestCase
{
/**
* @var ArrayConverter
*/
protected $fixture;

protected function setUp()
{
parent::setUp();

$this->fixture = new ArrayConverter(['delimiter' => ',']);
}

protected function tearDown()
{
parent::tearDown();

$this->fixture = null;
}

public function testNullToDb()
{
$this->assertNull($this->fixture->toDb(null));
}

public function testBooleanToDb()
{
$this->assertEquals('{false}', $this->fixture->toDb([false]));
$this->assertEquals('{true}', $this->fixture->toDb([true]));
}

/**
* @dataProvider valuesProvider
*/
public function testToDb($expected, $value)
{
$this->assertEquals($expected, $this->fixture->toDb($value));
}

public function testNullToPhp()
{
$this->assertNull($this->fixture->toPhp(null));
}

public function testBooleanToPhp()
{
// Typecasting for boolean type realized in ColumnSchema
$this->assertEquals(['f'], $this->fixture->toPhp('{f}'));
$this->assertEquals(['t'], $this->fixture->toPhp('{t}'));
}

/**
* @dataProvider valuesProvider
*/
public function testToPhp($value, $expected)
{
$this->assertEquals($expected, $this->fixture->toPhp($value));
}

public function testAdditionalToPhp()
{
$this->assertEquals(['string'], $this->fixture->toPhp('{string}'));
$this->assertEquals(['string1', ',', 'string3'], $this->fixture->toPhp('{string1,",",string3}'));
}

public function valuesProvider()
{
return [
['{}', []],
['{NULL}', [null]],
['{0}', [0]],
['{-1}', [-1]],
['{1.5}', [1.5]],
['{""}', ['']],
['{1,2,3}', [1,2,3]],
['{"string1","str\\\\in\\"g2","str,ing3"}', ['string1','str\\in"g2','str,ing3']],
['{"null","NULL",NULL}', ['null','NULL',null]],
];
}
}
Loading

0 comments on commit bbe4699

Please sign in to comment.