Skip to content
Closed
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
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ install:

script:
- ./bin/phpspec run --format=pretty
- ./bin/phpunit -vvv tests/
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
"symfony/yaml": "~2.3"
},
"require-dev": {
"phpspec/phpspec": "2.0.*"
"phpspec/phpspec": "2.0.*",
"phpunit/phpunit": "^4.5|^5.0"
},
"config": {
"bin-dir": "bin"
},
"autoload": {
"psr-4": {"": "src"}
"psr-4": {
"": "src",
"Coduo\\PHPHumanizer\\Tests\\": "tests/"
}
},
"extra": {
"branch-alias": {
Expand Down
2 changes: 1 addition & 1 deletion src/Coduo/PHPHumanizer/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function toRoman($number)
return $romanNumeral->toRoman($number);
}

public function fromRoman($number)
public static function fromRoman($number)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

{
$romanNumeral = new RomanNumeral();

Expand Down
54 changes: 54 additions & 0 deletions tests/unit/DateTime/DifferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Coduo\PHPHumanizer\Tests\DateTime;

use Coduo\PHPHumanizer\DateTime\Difference;
use PHPUnit_Framework_TestCase;

/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test classess also should have namespaces.
Please take a look at https://github.com/isolate-org/unit-of-work/tree/master/tests folder. Just remember to specify this namespace in composer.json file just like here: https://github.com/isolate-org/unit-of-work/blob/master/composer.json#L18

* Class DifferenceTest
*
* @package Coduo\PHPHumanizer\Tests\DateTime
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If its not a problem for you please remove those doc blocks. Not because I dont like it, we are just not using docblocks to mention authors, everything is in git log and after some time is hard to keep dockblocks and git log synchronized :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure no problem.

class DifferenceTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider differenceDataProvider
*
* @param $expected
* @param $firstDate
* @param $secondDate
* @param $instance
* @param $isPast
*/
public function test_calculating_differences_between_dates($expected, $firstDate, $secondDate, $instance, $isPast )
{
$diff = new Difference($firstDate, $secondDate);

$this->assertTrue(is_a($diff->getUnit(), $instance));
$this->assertEquals($expected, $diff->getQuantity());
$this->assertEquals($isPast, $diff->isPast());
}

/**
* @return array
*/
public function differenceDataProvider()
{
return array(
array(15, new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 12:45:00"), 'Coduo\PHPHumanizer\DateTime\Unit\Minute', true),
array(15, new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 13:15:00"), 'Coduo\PHPHumanizer\DateTime\Unit\Minute', false),
array(2, new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 11:00:00"), 'Coduo\PHPHumanizer\DateTime\Unit\Hour', true),
array(3, new \DateTime("2014-04-26 13:00:00"), new \DateTime("2014-04-26 16:00:00"), 'Coduo\PHPHumanizer\DateTime\Unit\Hour', false),
array(1, new \DateTime("2014-04-10"), new \DateTime("2014-04-09"), 'Coduo\PHPHumanizer\DateTime\Unit\Day', true),
array(1, new \DateTime("2014-04-10"), new \DateTime("2014-04-11"), 'Coduo\PHPHumanizer\DateTime\Unit\Day', false),
array(2, new \DateTime("2014-04-15"), new \DateTime("2014-04-01"), 'Coduo\PHPHumanizer\DateTime\Unit\Week', true),
array(2, new \DateTime("2014-04-01"), new \DateTime("2014-04-15"), 'Coduo\PHPHumanizer\DateTime\Unit\Week', false),
array(1, new \DateTime("2014-04-01"), new \DateTime("2014-03-01"), 'Coduo\PHPHumanizer\DateTime\Unit\Month', true),
array(1, new \DateTime("2014-04-01"), new \DateTime("2014-05-01"), 'Coduo\PHPHumanizer\DateTime\Unit\Month', false),
array(2, new \DateTime("2014-01-01"), new \DateTime("2012-01-01"), 'Coduo\PHPHumanizer\DateTime\Unit\Year', true),
array(1, new \DateTime("2014-01-01"), new \DateTime("2015-01-01"), 'Coduo\PHPHumanizer\DateTime\Unit\Year', false),
);
}
}

66 changes: 66 additions & 0 deletions tests/unit/DateTime/FormatterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Coduo\PHPHumanizer\Tests\DateTime;

use Coduo\PHPHumanizer\DateTime\Formatter;
use Coduo\PHPHumanizer\DateTime\Unit\Minute;
use PHPUnit_Framework_TestCase;

/**
* Class FormatterTest
*
* @package Coduo\PHPHumanizer\Tests\DateTime
*/
class FormatterTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider formatterDataProvider
*
* @param $unit
* @param $quantity
* @param $isPast
* @param $formatted
* @param string $locale
*/
public function test_formatting_datetimes($unit, $quantity, $isPast, $formatted, $locale = 'en')
{
$translator = $this->prophesize('Symfony\Component\Translation\Translator');

$translator->transChoice(
'minute.past',
10,
array('%count%' => 10),
'difference',
'en'
)->willReturn('10 minutes ago');

$translator->transChoice(
'minute.past',
10,
array('%count%' => 10),
'difference',
'pl'
)->willReturn('10 minut temu');

$diff = $this->prophesize('\Coduo\PHPHumanizer\DateTime\Difference');

$diff->getUnit()->willReturn($unit);
$diff->getQuantity()->willReturn($quantity);
$diff->isPast()->willReturn($isPast);

$formatter = new Formatter($translator->reveal());

$this->assertEquals($formatted, $formatter->formatDifference($diff->reveal(), $locale));
}

/**
* @return array
*/
public function formatterDataProvider()
{
return array(
array(new Minute(), 10, true, '10 minutes ago'),
array(new Minute(), 10, true, '10 minut temu', 'pl'),
);
}
}
165 changes: 165 additions & 0 deletions tests/unit/DateTimeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace Coduo\PHPHumanizer\Tests;

use Coduo\PHPHumanizer\DateTime;
use PHPUnit_Framework_TestCase;

/**
* Class DateTimeTest
*
* @package Coduo\PHPHumanizer\Tests
*/
class DateTimeTest extends PHPUnit_Framework_TestCase
{
/**
* @test
* @dataProvider humanizeDataProvider
*
* @param $firstDate
* @param $secondDate
* @param $expected
* @param string $locale
*/
public function test_humanize_difference_between_dates($firstDate, $secondDate, $expected, $locale = 'en')
{
$datetime = new DateTime();

$this->assertEquals($expected, $datetime->difference(new \DateTime($firstDate), new \DateTime($secondDate), $locale));
$this->assertEquals($expected, DateTime::difference(new \DateTime($firstDate), new \DateTime($secondDate), $locale));
}

/**
* @dataProvider preciseDifferenceDataProvider
*
* @param $firstDate
* @param $secondDate
* @param $expected
* @param string $locale
*/
public function test_humanize_precise_difference_between_dates($firstDate, $secondDate, $expected, $locale = 'en')
{
$datetime = new DateTime();

$this->assertEquals($expected, $datetime->preciseDifference(new \DateTime($firstDate), new \DateTime($secondDate), $locale));
$this->assertEquals($expected, DateTime::preciseDifference(new \DateTime($firstDate), new \DateTime($secondDate), $locale));
}

/**
* @return array
*/
public function humanizeDataProvider()
{
return array(
array("2014-04-26 13:00:00", "2014-04-26 13:00:00", 'just now'),
array("2014-04-26 13:00:00", "2014-04-26 13:00:05", '5 seconds from now'),
array("2014-04-26 13:00:00", "2014-04-26 12:59:00", '1 minute ago'),
array("2014-04-26 13:00:00", "2014-04-26 12:45:00", '15 minutes ago'),
array("2014-04-26 13:00:00", "2014-04-26 13:15:00", '15 minutes from now'),
array("2014-04-26 13:00:00", "2014-04-26 14:00:00", '1 hour from now'),
array("2014-04-26 13:00:00", "2014-04-26 15:00:00", '2 hours from now'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", '1 hour ago'),
array("2014-04-26", "2014-04-25", '1 day ago'),
array("2014-04-26", "2014-04-24", '2 days ago'),
array("2014-04-26", "2014-04-28", '2 days from now'),
array("2014-04-01", "2014-04-15", '2 weeks from now'),
array("2014-04-15", "2014-04-07", '1 week ago'),
array("2014-01-01", "2014-04-01", '3 months from now'),
array("2014-05-01", "2014-04-01", '1 month ago'),
array("2015-05-01", "2014-04-01", '1 year ago'),
array("2014-05-01", "2016-04-01", '2 years from now'),

array("2014-04-26 13:00:00", "2014-04-26 13:00:00", 'w tym momencie', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 13:00:05", 'za 5 sekund', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 12:59:00", 'minutę temu', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 12:45:00", '15 minut temu', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 13:15:00", 'za 15 minut', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 14:00:00", 'za godzinę', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 15:00:00", 'za 2 godziny', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", 'godzinę temu', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 15:00:00", 'za 2 godziny', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 12:00:00", 'godzinę temu', 'pl'),
array("2014-04-26", "2014-04-25", 'wczoraj', 'pl'),
array("2014-04-26", "2014-04-24", '2 dni temu', 'pl'),
array("2014-04-26", "2014-04-28", 'za 2 dni', 'pl'),
array("2014-04-01", "2014-04-15", 'za 2 tygodnie', 'pl'),
array("2014-04-15", "2014-04-07", 'tydzień temu', 'pl'),
array("2014-01-01", "2014-04-01", 'za 3 miesiące', 'pl'),
array("2014-05-01", "2014-04-01", 'miesiąc temu', 'pl'),
array("2015-05-01", "2014-04-01", 'rok temu', 'pl'),
array("2014-05-01", "2016-04-01", 'za 2 lata', 'pl'),
array("2014-05-01", "2009-04-01", '5 lat temu', 'pl'),
);
}

/**
* @return array
*/
public function preciseDifferenceDataProvider()
{
return array(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe its a good idea to separate each test case per locale?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could, I thought it was easier and DRYer this way, simply add a new block to the dataprovider and you're good to go, the test code stays the same.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not totally sure if we should split those test cases. IMO this way is easier for new ppl to add tests for other languages.

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minute, 45 seconds ago'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 hour, 40 minutes ago'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 day, 15 minutes from now'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 days, 2 hours from now'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 year, 2 days, 4 hours from now'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 days, 10 hours from now'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 day, 1 hour, 40 minutes ago'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 years, 1 day from now'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minuta, 45 sekund temu', 'pl'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 godzina, 40 minut temu', 'pl'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 dzień, 15 minut od teraz', 'pl'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 dni, 2 godziny od teraz', 'pl'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 rok, 2 dni, 4 godziny od teraz', 'pl'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 dni, 10 godzin od teraz', 'pl'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 dzień, 1 godzina, 40 minut temu', 'pl'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 lata, 1 dzień od teraz', 'pl'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 Minute, 45 Sekunden vor', 'de'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 Stunde, 40 Minuten vor', 'de'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 Tag, 15 Minuten ab jetzt', 'de'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 Tage, 2 Stunden ab jetzt', 'de'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 Jahr, 2 Tage, 4 Stunden ab jetzt', 'de'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 Tage, 10 Stunden ab jetzt', 'de'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 Tag, 1 Stunde, 40 Minuten vor', 'de'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 Jahre, 1 Tag ab jetzt', 'de'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 dakika, 45 saniye önce', 'tr'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 saat, 40 dakika önce', 'tr'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 gün, 15 dakika sonra', 'tr'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 gün, 2 saat sonra', 'tr'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 yıl, 2 gün, 4 saat sonra', 'tr'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 gün, 10 saat sonra', 'tr'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 gün, 1 saat, 40 dakika önce', 'tr'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 yıl, 1 gün sonra', 'tr'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minute, 45 secondes il y a', 'fr'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 heure, 40 minutes il y a', 'fr'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 jour, 15 minutes maintenant', 'fr'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 jours, 2 heures maintenant', 'fr'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 année, 2 jours, 4 heures maintenant', 'fr'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 jours, 10 heures maintenant', 'fr'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 jour, 1 heure, 40 minutes il y a', 'fr'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 années, 1 jour maintenant', 'fr'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minuto, 45 segundos atrás', 'pt_BR'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 hora, 40 minutos atrás', 'pt_BR'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 dia, 15 minutos a partir de agora', 'pt_BR'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 dias, 2 horas a partir de agora', 'pt_BR'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 ano, 2 dias, 4 horas a partir de agora', 'pt_BR'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 dias, 10 horas a partir de agora', 'pt_BR'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 dia, 1 hora, 40 minutos atrás', 'pt_BR'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 anos, 1 dia a partir de agora', 'pt_BR'),

array("2014-04-26 13:00:00", "2014-04-26 12:58:15", '1 minuto, 45 secondi fa', 'it'),
array("2014-04-26 13:00:00", "2014-04-26 11:20:00", '1 ora, 40 minuti fa', 'it'),
array("2014-04-26 13:00:00", "2014-04-27 13:15:00", '1 giorno, 15 minuti da adesso', 'it'),
array("2014-04-26 13:00:00", "2014-05-03 15:00:00", '7 giorni, 2 ore da adesso', 'it'),
array("2014-04-26 13:00:00", "2015-04-28 17:00:00", '1 anno, 2 giorni, 4 ore da adesso', 'it'),
array("2014-04-26 13:00:00", "2014-04-28 23:00:00", '2 giorni, 10 ore da adesso', 'it'),
array("2014-04-26 13:00:00", "2014-04-25 11:20:00", '1 giorno, 1 ora, 40 minuti fa', 'it'),
array("2014-04-26 13:00:00", "2016-04-27 13:00:00", '2 anni, 1 giorno da adesso', 'it'),
);
}
}
Loading