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 TimeInterval and ParserSettings tests #25

Merged
merged 1 commit into from
Nov 2, 2016
Merged
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
135 changes: 135 additions & 0 deletions tests/IntervalParserTests/ParserSettingsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php declare(strict_types = 1);

namespace IntervalParserTests;

use IntervalParser\ParserSettings;
use PHPUnit\Framework\TestCase;

class ParserSettingsTest extends TestCase
{
public function testGetLeadingSeparatorWhenManuallySet()
{
$parserSettings = new ParserSettings('foo');

$this->assertSame('foo', $parserSettings->getLeadingSeparator());
}

public function testGetLeadingSeparatorWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame('in', $parserSettings->getLeadingSeparator());
}

public function testGetSymbolSeparatorWhenManuallySet()
{
$parserSettings = new ParserSettings('foo', false, ParserSettings::SYMBOL, ';');

$this->assertSame(';', $parserSettings->getSymbolSeparator());
}

public function testGetSymbolSeparatorWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame(',', $parserSettings->getSymbolSeparator());
}

public function testGetWordSeparatorWhenManuallySet()
{
$parserSettings = new ParserSettings('foo', false, ParserSettings::SYMBOL, ';', 'bar');

$this->assertSame('bar', $parserSettings->getWordSeparator());
}

public function testGetWordSeparatorWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame(',', $parserSettings->getWordSeparator());
}

public function testKeepLeadingSeparatorWhenManuallySetToTrue()
{
$parserSettings = new ParserSettings('foo', true);

$this->assertTrue($parserSettings->keepLeadingSeparator());
}

public function testKeepLeadingSeparatorWhenManuallySetToFalse()
{
$parserSettings = new ParserSettings('foo', false);

$this->assertFalse($parserSettings->keepLeadingSeparator());
}

public function testKeepLeadingSeparatorWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertFalse($parserSettings->keepLeadingSeparator());
}

public function testGetSeparationTypeWhenManuallySetToSymbol()
{
$parserSettings = new ParserSettings('foo', true, ParserSettings::SYMBOL);

$this->assertSame('symbol', $parserSettings->getSeparationType());
}

public function testGetSeparationTypeWhenManuallySetToString()
{
$parserSettings = new ParserSettings('foo', false, ParserSettings::STRING);

$this->assertSame('string', $parserSettings->getSeparationType());
}

public function testGetSeparationTypeWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame('symbol', $parserSettings->getSeparationType());
}

public function testGetLeadingSeparatorExpressionWhenManuallySet()
{
$parserSettings = new ParserSettings('foo');

$this->assertSame('/(.*)\s+(?:foo)\s+(.*)/ui', $parserSettings->getLeadingSeparatorExpression());
}

public function testGetLeadingSeparatorExpressionWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame('/(.*)\s+(?:in)\s+(.*)/ui', $parserSettings->getLeadingSeparatorExpression());
}

public function testGetSymbolSeparatorExpressionWhenManuallySet()
{
$parserSettings = new ParserSettings('foo', false, ParserSettings::SYMBOL, ';');

$this->assertSame('/(?<first>[^;]*)\s?;\s?(?<next>.*)$/ui', $parserSettings->getSymbolSeparatorExpression());
}

public function testGetSymbolSeparatorExpressionWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame('/(?<first>[^,]*)\s?,\s?(?<next>.*)$/ui', $parserSettings->getSymbolSeparatorExpression());
}

public function testGetWordSeparatorExpressionWhenManuallySet()
{
$parserSettings = new ParserSettings('in', false, ParserSettings::SYMBOL, ',', 'bar');

$this->assertSame('/^(?<first>.*?)\s?bar\s?(?<next>.*)$/ui', $parserSettings->getWordSeparatorExpression());
}

public function testGetWordSeparatorExpressionWhenUsingTheDefault()
{
$parserSettings = new ParserSettings();

$this->assertSame('/^(?<first>.*?)\s?word\s?(?<next>.*)$/ui', $parserSettings->getWordSeparatorExpression());
}
}
61 changes: 61 additions & 0 deletions tests/IntervalParserTests/TimeIntervalTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php declare(strict_types = 1);

namespace IntervalParserTests;

use IntervalParser\TimeInterval;
use PHPUnit\Framework\TestCase;

# Value object representing a time interval.
class TimeIntervalTest extends TestCase
{
public function testGetInterval()
{
$dateInterval = new \DateInterval('P1D');
$timeInterval = new TimeInterval($dateInterval, 1, 2, 'foo', 'bar');

$this->assertInstanceOf(\DateInterval::class, $timeInterval->getInterval());
$this->assertSame($dateInterval, $timeInterval->getInterval());
}

public function testGetIntervalOffset()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2, 'foo', 'bar');

$this->assertSame(1, $timeInterval->getIntervalOffset());
}

public function testGetIntervalLength()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2, 'foo', 'bar');

$this->assertSame(2, $timeInterval->getIntervalLength());
}

public function testGetLeadingData()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2, 'foo', 'bar');

$this->assertSame('foo', $timeInterval->getLeadingData());
}

public function testGetLeadingDataReturnsEmptyStringWhenNotSet()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2);

$this->assertSame('', $timeInterval->getLeadingData());
}

public function testGetTrailingData()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2, 'foo', 'bar');

$this->assertSame('bar', $timeInterval->getTrailingData());
}

public function testGetTrailingDataReturnsEmptyStringWhenNotSet()
{
$timeInterval = new TimeInterval(new \DateInterval('P1D'), 1, 2);

$this->assertSame('', $timeInterval->getTrailingData());
}
}