Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added few unit tests #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Hide eclipse project data
.buildpath
.project
.settings
.settings
vendor/
composer.lock
10 changes: 8 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@
}
],
"require" : {
"doctrine/dbal": "2.*"
"doctrine/dbal": "^2.0"
},
"autoload": {
"psr-0": {
"DoctrineTimestamp": "lib"
}
},
"autoload-dev": {
"psr-4": { "DoctrineTimestamp\\Tests\\": "tests/" }
},
"require-dev": {
"phpunit/phpunit": "^5.7"
}
}
}
14 changes: 9 additions & 5 deletions lib/DoctrineTimestamp/DBAL/Types/Timestamp.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,15 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
return $value->getTimestamp();
}

throw ConversionException::conversionFailedInvalidType(
$value,
$this->getName(),
['null', 'DateTime']
);
if (is_callable('ConversionException::conversionFailedInvalidType')) {
throw ConversionException::conversionFailedInvalidType(
$value,
$this->getName(),
['null', 'DateTime']
);
}

throw ConversionException::conversionFailed($value, $this->getName());
}

/**
Expand Down
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
processIsolation="false"
stopOnFailure="false"
failOnWarning="true"
bootstrap="./vendor/autoload.php"
>
<testsuites>
<testsuite name="Test classes">
<directory suffix=".php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist addUncoveredFilesFromWhitelist="true">
<directory suffix=".php">lib/</directory>
</whitelist>
</filter>
</phpunit>
129 changes: 129 additions & 0 deletions tests/DBAL/Types/TimestampTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace DoctrineTimestamp\Test\DBAL\Types;

use DateTime;
use DateTimeInterface;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;
use DoctrineTimestamp\DBAL\Types\Timestamp;
use Doctrine\DBAL\Platforms\MySqlPlatform;

class TimestampTest extends TestCase
{
protected $type;

public function setUp()
{
if (!Type::hasType(Timestamp::TIMESTAMP)) {
Type::addType(Timestamp::TIMESTAMP, Timestamp::class);
}

$this->type = Type::getType(Timestamp::TIMESTAMP);
}

public function testGetName()
{
$this->assertEquals(
Timestamp::TIMESTAMP,
$this->type->getName()
);
}

public function testGetSqlDeclaration()
{
$platform = $this->createMock('Doctrine\DBAL\Platforms\AbstractPlatform');

$expected = 'some integer col definition';

$platform
->expects($this->any())
->method('getIntegerTypeDeclarationSQL')
->will($this->returnValue($expected))
;

$this->assertEquals(
$expected,
$this->type->getSqlDeclaration(array(), $platform)
);
}

public function testConvertToDatabaseValueWithNullReturnsNull()
{
$this->assertNull(
$this->type->convertToDatabaseValue(
null,
new MySqlPlatform()
)
);
}

/**
* @expectedException Doctrine\DBAL\Types\ConversionException
*/
public function testConvertToDatabaseValueWithInvalidTypeThrowsException()
{
$this->type->convertToDatabaseValue(
'string',
new MySqlPlatform()
);
}

/**
* @dataProvider provideSamples
*/
public function testConvertToDatabaseValue(DateTimeInterface $input, $expected)
{
$this->assertEquals(
$expected,
$this->type->convertToDatabaseValue(
$input,
new MySqlPlatform()
)
);
}

public function provideSamples()
{
return array(
array(
new DateTime('1970-01-01 00:00:00 UTC'),
0,
),
array(
new DateTime('1970-01-01 00:00:00 +01:00'),
-3600,
),
array(
new DateTime('1970-01-01 00:00:00 -01:00'),
3600,
),
);
}

public function testConvertToPHPValueWithNullReturnsNull()
{
$this->assertNull(
$this->type->convertToPHPValue(
null,
new MySqlPlatform()
)
);
}

/**
* @dataProvider provideSamples
*/
public function testConvertToPHPValue(DateTimeInterface $expected, $input)
{
$res = $this->type->convertToPHPValue(
$input,
new MySqlPlatform()
);

$this->assertEquals($expected->getTimestamp(), $res->getTimestamp());

$res = $res->setTimezone($expected->getTimezone());
$this->assertEquals($expected->format('c'), $res->format('c'));
}
}