Skip to content

Commit

Permalink
Merge pull request #115 from AyoobMH/refactor-tests-to-pest
Browse files Browse the repository at this point in the history
Refactor tests to pest
  • Loading branch information
freekmurze authored Nov 21, 2022
2 parents 47f5c5e + 4c400d2 commit 67c0dc5
Show file tree
Hide file tree
Showing 21 changed files with 1,610 additions and 2,013 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction --no-suggest

- name: Execute tests
run: vendor/bin/phpunit
run: vendor/bin/pest
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ vendor
coverage
.php-cs-fixer.cache
.phpunit.result.cache
.idea
12 changes: 8 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"require-dev": {
"larapack/dd": "^1.1",
"nesbot/carbon": "^2.54",
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^9.5",
"spatie/ray": "^1.31"
},
Expand All @@ -35,10 +36,13 @@
}
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}
}
258 changes: 40 additions & 218 deletions tests/BoundaryTest.php
Original file line number Diff line number Diff line change
@@ -1,236 +1,58 @@
<?php

namespace Spatie\Period\Tests;

use Generator;
use PHPUnit\Framework\TestCase;
use Spatie\Period\Boundaries;
use Spatie\Period\Period;
use Spatie\Period\Precision;

class BoundaryTest extends TestCase
{
/** @test */
public function exclude_none()
{
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_NONE());

$this->assertFalse($period->isStartExcluded());
$this->assertFalse($period->isEndExcluded());
}

/** @test */
public function exclude_start()
{
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_START());

$this->assertTrue($period->isStartExcluded());
$this->assertFalse($period->isEndExcluded());
}

/** @test */
public function exclude_end()
{
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_END());

$this->assertFalse($period->isStartExcluded());
$this->assertTrue($period->isEndExcluded());
}

/** @test */
public function exclude_all()
{
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_ALL());

$this->assertTrue($period->isStartExcluded());
$this->assertTrue($period->isEndExcluded());
}

/**
* @test
* @dataProvider periodsWithAmountsOfIncludedDates
*/
public function length_with_boundaries($expectedAmount, Period $period)
{
$this->assertEquals($expectedAmount, $period->length());
}

public function periodsWithAmountsOfIncludedDates(): Generator
{
yield [4, Period::make('2016-01-01', '2019-02-05', Precision::YEAR(), Boundaries::EXCLUDE_NONE())];
yield [3, Period::make('2016-01-01', '2019-02-05', Precision::YEAR(), Boundaries::EXCLUDE_START())];
yield [3, Period::make('2016-01-01', '2019-02-05', Precision::YEAR(), Boundaries::EXCLUDE_END())];
yield [2, Period::make('2016-01-01', '2019-02-05', Precision::YEAR(), Boundaries::EXCLUDE_ALL())];

yield [43, Period::make('2016-01-01', '2019-07-05', Precision::MONTH(), Boundaries::EXCLUDE_NONE())];
yield [42, Period::make('2016-01-01', '2019-07-05', Precision::MONTH(), Boundaries::EXCLUDE_START())];
yield [42, Period::make('2016-01-01', '2019-07-05', Precision::MONTH(), Boundaries::EXCLUDE_END())];
yield [41, Period::make('2016-01-01', '2019-07-05', Precision::MONTH(), Boundaries::EXCLUDE_ALL())];

yield [31, Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_NONE())];
yield [30, Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_START())];
yield [30, Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_END())];
yield [29, Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_ALL())];

yield [24, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:00', Precision::HOUR(), Boundaries::EXCLUDE_NONE())];
yield [23, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:00', Precision::HOUR(), Boundaries::EXCLUDE_START())];
yield [23, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:00', Precision::HOUR(), Boundaries::EXCLUDE_END())];
yield [22, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:00', Precision::HOUR(), Boundaries::EXCLUDE_ALL())];

yield [1440, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::MINUTE(), Boundaries::EXCLUDE_NONE())];
yield [1439, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::MINUTE(), Boundaries::EXCLUDE_START())];
yield [1439, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::MINUTE(), Boundaries::EXCLUDE_END())];
yield [1438, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::MINUTE(), Boundaries::EXCLUDE_ALL())];

yield [86363, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::SECOND(), Boundaries::EXCLUDE_NONE())];
yield [86362, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::SECOND(), Boundaries::EXCLUDE_START())];
yield [86362, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::SECOND(), Boundaries::EXCLUDE_END())];
yield [86361, Period::make('2018-01-01 00:00:00', '2018-01-01 23:59:22', Precision::SECOND(), Boundaries::EXCLUDE_ALL())];
}

/** @test */
public function overlap_with_excluded_boundaries()
{
$a = Period::make('2018-01-01', '2018-01-05', boundaries: Boundaries::EXCLUDE_END());
$b = Period::make('2018-01-05', '2018-01-10');
$this->assertFalse($a->overlapsWith($b));

$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10', boundaries: Boundaries::EXCLUDE_START());
$this->assertFalse($a->overlapsWith($b));

$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10');
$this->assertTrue($a->overlapsWith($b));
}

/**
* @dataProvider boundariesForSubtract
* @test
*/
public function subtract_with_boundaries(string $period, string $subtract, string $result)
{
$this->assertEquals(
$result,
Period::fromString($period)
->subtract(Period::fromString($subtract))[0]
->asString()
);
}

public function boundariesForSubtract(): Generator
{
yield [
'period' => '[2021-01-01,2021-02-01)', // [=========)
'subtract' => '[2021-01-15,2021-02-01]', // [=====]
'result' => '[2021-01-01,2021-01-15)', // [=======)
];

yield [
'period' => '[2021-01-01,2021-02-01)', // [=========)
'subtract' => '(2021-01-15,2021-02-01]', // (=====]
'result' => '[2021-01-01,2021-01-16)', // [========)
];

yield [
'period' => '[2021-01-01,2021-02-01]', // [=========]
'subtract' => '(2021-01-15,2021-02-01]', // (=====]
'result' => '[2021-01-01,2021-01-15]', // [=======]
];

yield [
'period' => '[2021-01-01,2021-02-01]', // [=========]
'subtract' => '[2021-01-15,2021-02-01]', // [=====]
'result' => '[2021-01-01,2021-01-14]', // [======]
];
it('exclude none', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_NONE());

yield [
'period' => '[2021-01-01,2021-02-01]', // [=======]
'subtract' => '[2021-01-01,2021-01-10]', // [=========]
'result' => '[2021-01-11,2021-02-01]', // [====]
];
expect($period->isStartExcluded())->toBeFalse();
expect($period->isEndExcluded())->toBeFalse();
});

yield [
'period' => '(2021-01-01,2021-02-01]', // (=======]
'subtract' => '[2021-01-01,2021-01-10]', // [=========]
'result' => '(2021-01-10,2021-02-01]', // (=====]
];
it('exclude start', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_START());

yield [
'period' => '(2021-01-01,2021-02-01]', // (=======]
'subtract' => '[2021-01-01,2021-01-10)', // [=========)
'result' => '(2021-01-09,2021-02-01]', // (======]
];
expect($period->isStartExcluded())->toBeTrue();
expect($period->isEndExcluded())->toBeFalse();
});

yield [
'period' => '[2021-01-01,2021-02-01]', // [=======]
'subtract' => '[2021-01-01,2021-01-10)', // [=========)
'result' => '[2021-01-10,2021-02-01]', // [=====]
];
}
it('exclude end', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_END());

/**
* @dataProvider boundariesForOverlap
* @test
*/
public function overlap_with_boundaries(string $period, string $overlap, string $result)
{
$this->assertEquals(
$result,
Period::fromString($period)
->overlap(Period::fromString($overlap))
->asString()
);
}
expect($period->isStartExcluded())->toBeFalse();
expect($period->isEndExcluded())->toBeTrue();
});

public function boundariesForOverlap(): Generator
{
yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-10,2021-01-15]', // [=========]
'result' => '[2021-01-10,2021-01-16)', // [==========)
];
it('exclude all', function () {
$period = Period::make('2018-01-01', '2018-01-31', boundaries: Boundaries::EXCLUDE_ALL());

yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-10,2021-01-15)', // [=========)
'result' => '[2021-01-10,2021-01-15)', // [=========)
];
expect($period->isStartExcluded())->toBeTrue();
expect($period->isEndExcluded())->toBeTrue();
});

yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-10,2021-02-15)', // [=========)
'result' => '[2021-01-10,2021-02-01)', // [======)
];
it('length with boundaries', function ($expectedAmount, Period $period) {
expect($period->length())->toEqual($expectedAmount);
})->with('periods_with_amounts_of_included_dates');

yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-10,2021-02-15]', // [=========]
'result' => '[2021-01-10,2021-02-01)', // [======)
];
it('overlap with excluded boundaries', function () {
$a = Period::make('2018-01-01', '2018-01-05', boundaries: Boundaries::EXCLUDE_END());
$b = Period::make('2018-01-05', '2018-01-10');
expect($a->overlapsWith($b))->toBeFalse();

yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-01,2021-01-15]', // [===========]
'result' => '[2021-01-01,2021-01-16)', // [=======)
];
$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10', boundaries: Boundaries::EXCLUDE_START());
expect($a->overlapsWith($b))->toBeFalse();

yield [
'period' => '[2021-01-01,2021-02-01)', // [==================)
'overlap' => '[2021-01-01,2021-01-15)', // [===========)
'result' => '[2021-01-01,2021-01-15)', // [======)
];
$a = Period::make('2018-01-01', '2018-01-05');
$b = Period::make('2018-01-05', '2018-01-10');
expect($a->overlapsWith($b))->toBeTrue();
});

yield [
'period' => '(2021-01-01,2021-02-01)', // (==================)
'overlap' => '[2021-01-10,2021-01-15]', // [======]
'result' => '(2021-01-09,2021-01-16)', // (========)
];
it('subtract with boundaries', function (string $period, string $subtract, string $result) {
expect(Period::fromString($period)->subtract(Period::fromString($subtract))[0]->asString())->toEqual($result);
})->with('boundaries_for_subtract');

yield [
'period' => '(2021-01-01,2021-02-01)', // (==================)
'overlap' => '(2021-01-10,2021-01-15)', // (======)
'result' => '(2021-01-10,2021-01-15)', // (======)
];
}
}
it('overlap with boundaries', function (string $period, string $overlap, string $result) {
expect(Period::fromString($period)->overlap(Period::fromString($overlap))->asString())->toEqual($result);
})->with('boundaries_for_overlap');
Loading

0 comments on commit 67c0dc5

Please sign in to comment.