From 68a6bd55795726cdfcb285e95f41844a36422bee Mon Sep 17 00:00:00 2001 From: Olivier Laviale Date: Thu, 23 Jun 2022 20:41:23 +0200 Subject: [PATCH] Requires PHP 7.3+ --- .gitattributes | 5 +- .github/workflows/test.yml | 9 +- .gitignore | 1 + MIGRATION.md | 23 ++ Makefile | 18 +- README.md | 45 +-- composer.json | 18 +- docker-compose.yaml | 8 +- lib/DateTime.php | 149 ++++---- lib/TimeZone.php | 16 +- lib/TimeZoneLocation.php | 22 +- phpunit.xml | 15 + phpunit.xml.dist | 21 -- tests/DateTimeTest.php | 605 +++++++-------------------------- tests/TimeZoneLocationTest.php | 149 ++++---- tests/TimeZoneTest.php | 27 +- 16 files changed, 359 insertions(+), 772 deletions(-) create mode 100644 MIGRATION.md create mode 100644 phpunit.xml delete mode 100644 phpunit.xml.dist diff --git a/.gitattributes b/.gitattributes index 1a31373..7a6aaab 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,6 +1,9 @@ .github .gitattributes export-ignore +.github export-ignore .gitignore export-ignore +docker-compose.yml export-ignore +Dockerfile export-ignore Makefile export-ignore -phpunit.xml.dist export-ignore +phpunit.xml export-ignore tests/ export-ignore diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ce665de..96968b1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,13 +7,12 @@ on: jobs: phpunit: name: phpunit - runs-on: ubuntu-18.04 + runs-on: ubuntu-20.04 strategy: matrix: php-version: - - "5.6" - - "7.0" - - "7.4" + - "7.3" + - "8.1" steps: - name: Checkout uses: actions/checkout@v2 @@ -40,7 +39,7 @@ jobs: run: make test-coveralls - name: Upload code coverage - if: ${{ matrix.php-version == '5.6' }} + if: ${{ matrix.php-version == '7.3' }} env: COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | diff --git a/.gitignore b/.gitignore index 073e37a..4edb3d9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.phpunit.result.cache build composer.lock vendor diff --git a/MIGRATION.md b/MIGRATION.md new file mode 100644 index 0000000..c4b5adf --- /dev/null +++ b/MIGRATION.md @@ -0,0 +1,23 @@ +# Migration + +## v1.x to v2.x + +### New Requirements + +Requires PHP 7.3+ + +### New features + +None + +### Backward Incompatible Changes + +None + +### Deprecated Features + +None + +### Other Changes + +None diff --git a/Makefile b/Makefile index f500375..8d148f4 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,8 @@ PHPUNIT = vendor/bin/phpunit vendor: @composer install +# testing + .PHONY: test-dependencies test-dependencies: vendor @@ -18,19 +20,19 @@ test: test-dependencies .PHONY: test-coverage test-coverage: test-dependencies @mkdir -p build/coverage - @$(PHPUNIT) --coverage-html build/coverage + @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-html build/coverage .PHONY: test-coveralls test-coveralls: test-dependencies @mkdir -p build/logs - @$(PHPUNIT) --coverage-clover build/logs/clover.xml + @XDEBUG_MODE=coverage $(PHPUNIT) --coverage-clover build/logs/clover.xml -.PHONY: test-container-56 -test-container-56: - @docker-compose run --rm app56 sh +.PHONY: test-container-73 +test-container-73: + @docker-compose run --rm app73 sh @docker-compose down -.PHONY: test-container-74 -test-container-74: - @docker-compose run --rm app74 sh +.PHONY: test-container-81 +test-container-81: + @docker-compose run --rm app81 sh @docker-compose down diff --git a/README.md b/README.md index 2227eb3..152f085 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,12 @@ year, quarter and more has been made especially easy. Also, all instances can be +#### Installation + +```bash +composer require icanboogie/datetime +``` + ### Usage @@ -343,36 +349,6 @@ echo $date->localize('fr')->as_medium; // 5 mai 2015 23:13:05 -## Installation - -The recommended way to install this package is through [Composer](https://getcomposer.org/): - -``` -$ composer require icanboogie/datetime -``` - -The package [icanboogie/common](https://github.com/ICanBoogie/Common) is suggested to -provide finer exceptions, such as [PropertyNotDefined][] and [PropertyNotWritable][]. -If the package is not included, `RunTimeException` instances are thrown instead. - - - -## Documentation - -The package is documented as part of the [ICanBoogie](http://icanboogie.org/) framework -[documentation][]. The documentation for the package and its -dependencies can be generated with the `make doc` command. The documentation is generated in -the `build/docs` directory using [ApiGen](http://apigen.org/). The package directory can later by -cleaned with the `make clean` command. - -The following classes are documented: - -- [DateTime][] -- [TimeZone][] -- [TimeZoneLocation][] - - - ## Continuous Integration The project is continuously tested by [GitHub actions](https://github.com/ICanBoogie/DateTime/actions). @@ -395,9 +371,6 @@ Please see [CONTRIBUTING](CONTRIBUTING.md) for details. [ICanBoogie/CLDR]: https://github.com/ICanBoogie/CLDR [JsonSerializable interface]: http://php.net/manual/en/class.jsonserializable.php -[documentation]: http://api.icanboogie.org/datetime/latest/ -[DateTime]: http://api.icanboogie.org/datetime/latest/class-ICanBoogie.DateTime.html -[TimeZone]: http://api.icanboogie.org/datetime/latest/class-ICanBoogie.TimeZone.html -[TimeZoneLocation]: http://api.icanboogie.org/datetime/latest/class-ICanBoogie.TimeZoneLocation.html -[PropertyNotDefined]: http://api.icanboogie.org/common/1.2/class-ICanBoogie.PropertyNotDefined.html -[PropertyNotWritable]: http://api.icanboogie.org/common/1.2/class-ICanBoogie.PropertyNotWritable.html +[DateTime]: lib/DateTime.php +[TimeZone]: lib/TimeZone.php +[TimeZoneLocation]: lib/TimeZoneLocation.php diff --git a/composer.json b/composer.json index 136b752..5c19f46 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ { "name": "Olivier Laviale", "email": "olivier.laviale@gmail.com", - "homepage": "https://olvlvl.com", + "homepage": "https://olvlvl.com/", "role" : "Developer" } ], @@ -17,19 +17,27 @@ "issues": "https://github.com/ICanBoogie/DateTime/issues", "source": "https://github.com/ICanBoogie/DateTime" }, + "config": { + "sort-packages": true + }, "require": { - "php": ">=5.6.0" + "php": ">=7.3" }, "require-dev": { - "icanboogie/common": "^1.2", - "phpunit/phpunit": "^5.6" + "icanboogie/common": "^2.0", + "phpunit/phpunit": "^9.5" }, "suggest": { "icanboogie/common": "Allows finer exceptions to be thrown" }, "autoload": { "psr-4": { - "ICanBoogie\\": "lib/" + "ICanBoogie\\": "lib" + } + }, + "autoload-dev": { + "psr-4": { + "Test\\ICanBoogie\\": "tests" } } } diff --git a/docker-compose.yaml b/docker-compose.yaml index 190188e..953abf0 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,22 +1,22 @@ --- version: "3.0" services: - app56: + app73: build: context: . args: - PHP_VERSION: 5.6 + PHP_VERSION: 7.3 environment: PHP_IDE_CONFIG: "serverName=icanboogie-datetime" volumes: - .:/app:delegated - ~/.composer:/root/.composer:delegated working_dir: /app - app74: + app81: build: context: . args: - PHP_VERSION: 7.4 + PHP_VERSION: 8.1 environment: PHP_IDE_CONFIG: "serverName=icanboogie-datetime" volumes: diff --git a/lib/DateTime.php b/lib/DateTime.php index bf3d096..f194158 100644 --- a/lib/DateTime.php +++ b/lib/DateTime.php @@ -11,6 +11,9 @@ namespace ICanBoogie; +use DateTimeZone; +use RuntimeException; + /** * Representation of a date and time. * @@ -169,38 +172,28 @@ class DateTime extends \DateTime implements \JsonSerializable * We redefine the constant to make sure that the cookie uses a valid pattern. * * @see http://grokbase.com/t/php/php-bugs/111xynxd6m/php-bug-bug-53879-new-datetime-createfromformat-fails-to-parse-cookie-expiration-date - * - * @var string */ - const COOKIE = 'l, d-M-Y H:i:s T'; + public const COOKIE = 'l, d-M-Y H:i:s T'; /** * DB (example: 2013-02-03 20:59:03) - * - * @var string */ - const DB = 'Y-m-d H:i:s'; + public const DB = 'Y-m-d H:i:s'; /** * Number (example: 20130203205903) - * - * @var string */ - const NUMBER = 'YmdHis'; + public const NUMBER = 'YmdHis'; /** * Date (example: 2013-02-03) - * - * @var string */ - const DATE = 'Y-m-d'; + public const DATE = 'Y-m-d'; /** * Time (example: 20:59:03) - * - * @var string */ - const TIME = 'H:i:s'; + public const TIME = 'H:i:s'; /** * Callable used to create localized instances. @@ -225,10 +218,8 @@ class DateTime extends \DateTime implements \JsonSerializable * @param mixed $source * @param mixed $timezone The time zone to use to create the time. The value is ignored if the * source is an instance of {@link \DateTime}. - * - * @return DateTime */ - static public function from($source, $timezone = null) + static public function from($source, $timezone = null): self { if ($source instanceof static) { @@ -249,10 +240,8 @@ static public function from($source, $timezone = null) * **Note:** Subsequent calls return equal times, event if they are minutes apart. _now_ * actually refers to the `REQUEST_TIME` or, if it is now available, to the first time * the method was invoked. - * - * @return static */ - static public function now() + static public function now(): self { static $now; @@ -268,10 +257,8 @@ static public function now() * Returns an instance with the current local time and the local time zone. * * **Note:** Subsequent calls may return different times. - * - * @return static */ - static public function right_now() + static public function right_now(): self { return new static; } @@ -293,12 +280,12 @@ static public function right_now() * $d->zone->name; // "Asia/Tokio" * * - * @param \DateTimeZone|string $timezone The time zone in which the empty date is created. + * @param DateTimeZone|string $timezone The time zone in which the empty date is created. * Defaults to "UTC". * * @return DateTime */ - static public function none($timezone = 'utc') + static public function none($timezone = 'utc'): self { return new static('0000-00-00', $timezone); } @@ -317,22 +304,16 @@ static public function none($timezone = 'utc') * new DateTime; * * - * @param string $time Defaults to "now". - * @param \DateTimeZone|string|null $timezone + * @param DateTimeZone|string|null $timezone */ - public function __construct($time = 'now', $timezone = null) + public function __construct(string $time = 'now', $timezone = null) { if (is_string($timezone)) { - $timezone = new \DateTimeZone($timezone); + $timezone = new DateTimeZone($timezone); } - # - # PHP 5.3.3 considers null $timezone as an error and will complain that it is not - # a \DateTimeZone instance. - # - - $timezone === null ? parent::__construct($time) : parent::__construct($time, $timezone); + parent::__construct($time, $timezone); } /** @@ -404,8 +385,16 @@ public function __get($property) $time->setTime(0, 0, 0); return $time; - /* + /** * days + * + * @uses get_monday + * @uses get_tuesday + * @uses get_wednesday + * @uses get_thursday + * @uses get_friday + * @uses get_saturday + * @uses get_sunday */ case 'monday': case 'tuesday': @@ -434,22 +423,20 @@ public function __get($property) return $transitions[0]['isdst']; } - if (class_exists('ICanBoogie\PropertyNotDefined')) + if (class_exists(PropertyNotDefined::class)) { throw new PropertyNotDefined([ $property, $this ]); } else { - throw new \RuntimeException("Property is not defined: $property."); + throw new RuntimeException("Property is not defined: $property."); } } /** * Returns Monday of the week. - * - * @return DateTime */ - protected function get_monday() + private function get_monday(): self { $time = clone $this; $day = $time->weekday; @@ -466,60 +453,48 @@ protected function get_monday() /** * Returns Tuesday of the week. - * - * @return DateTime */ - protected function get_tuesday() + private function get_tuesday(): self { return $this->monday->modify('+1 day'); } /** * Returns Wednesday of the week. - * - * @return DateTime */ - protected function get_wednesday() + private function get_wednesday(): self { return $this->monday->modify('+2 day'); } /** * Returns Thursday of the week. - * - * @return DateTime */ - protected function get_thursday() + private function get_thursday(): self { return $this->monday->modify('+3 day'); } /** * Returns Friday of the week. - * - * @return DateTime */ - protected function get_friday() + private function get_friday(): self { return $this->monday->modify('+4 day'); } /** * Returns Saturday of the week. - * - * @return DateTime */ - protected function get_saturday() + private function get_saturday(): self { return $this->monday->modify('+5 day'); } /** * Returns Sunday of the week. - * - * @return DateTime */ - protected function get_sunday() + private function get_sunday(): self { $time = clone $this; $day = $time->weekday; @@ -543,10 +518,12 @@ protected function get_sunday() * * @inheritdoc */ - public function __set($property, $value) + public function __set($property, $value): void { - static $readonly = [ 'quarter', 'week', 'year_day', 'weekday', - 'tomorrow', 'yesterday', 'utc', 'local' ]; + static $readonly = [ + 'quarter', 'week', 'year_day', 'weekday', + 'tomorrow', 'yesterday', 'utc', 'local' + ]; switch ($property) { @@ -570,23 +547,23 @@ public function __set($property, $value) if (strpos($property, 'is_') === 0 || strpos($property, 'as_') === 0 || in_array($property, $readonly) || method_exists($this, 'get_' . $property)) { - if (class_exists('ICanBoogie\PropertyNotWritable')) + if (class_exists(PropertyNotWritable::class)) { throw new PropertyNotWritable([ $property, $this ]); } else { - throw new \RuntimeException("Property is not writeable: $property."); + throw new RuntimeException("Property is not writeable: $property."); } } - if (class_exists('ICanBoogie\PropertyNotDefined')) + if (class_exists(PropertyNotDefined::class)) { throw new PropertyNotDefined([ $property, $this ]); } else { - throw new \RuntimeException("Property is not defined: $property."); + throw new RuntimeException("Property is not defined: $property."); } } @@ -634,17 +611,15 @@ public function __call($method, $arguments) * @return string The instance rendered as an {@link ISO8601} string, or an empty string if the * datetime is empty. */ - public function __toString() + public function __toString(): string { return $this->is_empty ? "" : $this->as_iso8601; } /** * Returns a {@link ISO8601} representation of the instance. - * - * @return string */ - public function jsonSerialize() + public function jsonSerialize(): string { return (string) $this; } @@ -657,16 +632,16 @@ public function jsonSerialize() * * @inheritdoc */ - public function setTimezone($timezone) + public function setTimezone($timezone): self { if ($timezone === 'local') { $timezone = date_default_timezone_get(); } - if (!$timezone instanceof \DateTimeZone) + if (!$timezone instanceof DateTimeZone) { - $timezone = new \DateTimeZone($timezone); + $timezone = new DateTimeZone($timezone); } return parent::setTimezone($timezone); @@ -689,14 +664,12 @@ public function setTimezone($timezone) * $time->change([ 'year' => 2000, 'second' => 0 ]); * * - * @param array $options + * @param array{ year: int, month: int, day: int, hour: int, minute: int, second: int, timezone: string } $options * @param bool $cascade If `true`, time options (`hour`, `minute`, `second`) reset * cascading, so if only the hour is passed, then minute and second is set to 0. If the hour * and minute is passed, then second is set to 0. - * - * @return DateTime */ - public function change(array $options, $cascade=false) + public function change(array $options, bool $cascade = false): self { static $default_options = [ @@ -766,12 +739,12 @@ public function change(array $options, $cascade=false) /** * Instantiate a new instance with changes properties. * - * @param array $options - * @param bool $cascade - * - * @return DateTime + * @param array{ year: int, month: int, day: int, hour: int, minute: int, second: int, timezone: string } $options + * @param bool $cascade If `true`, time options (`hour`, `minute`, `second`) reset + * cascading, so if only the hour is passed, then minute and second is set to 0. If the hour + * and minute is passed, then second is set to 0. */ - public function with(array $options, $cascade = false) + public function with(array $options, bool $cascade = false): self { $dt = clone $this; @@ -787,7 +760,7 @@ public function with(array $options, $cascade = false) * * @inheritdoc */ - public function format($format) + public function format($format): string { if (($format == self::DATE || $format == self::DB) && $this->is_empty) { @@ -800,19 +773,17 @@ public function format($format) /** * Returns a localized instance. * - * @param string $locale - * * @return mixed * - * @throws \RuntimeException if {@link $localizer} is not defined. + * @throws RuntimeException if {@link $localizer} is not defined. */ - public function localize($locale = 'en') + public function localize(string $locale = 'en') { $localizer = self::$localizer; if (!$localizer) { - throw new \RuntimeException("Localizer is not defined yet."); + throw new RuntimeException("Localizer is not defined yet."); } return $localizer($this, $locale); diff --git a/lib/TimeZone.php b/lib/TimeZone.php index b9daf12..62a3877 100644 --- a/lib/TimeZone.php +++ b/lib/TimeZone.php @@ -45,10 +45,8 @@ class TimeZone extends \DateTimeZone * the same instance. * * @param mixed $source Source of the timezone. - * - * @return TimeZone */ - static public function from($source) + static public function from($source): self { if ($source instanceof self) { @@ -87,10 +85,8 @@ static public function from($source) /** * Initializes the {@link $name} property. - * - * @param string $timezone */ - public function __construct($timezone) + public function __construct(string $timezone) { parent::__construct($timezone); @@ -111,7 +107,7 @@ public function __construct($timezone) * * @inheritdoc */ - public function __get($property) + public function __get(string $property) { switch ($property) { @@ -140,7 +136,7 @@ public function __get($property) return $this->getOffset($utc_time); } - if (class_exists('ICanBoogie\PropertyNotDefined')) + if (class_exists(PropertyNotDefined::class)) { throw new PropertyNotDefined([ $property, $this ]); } @@ -152,10 +148,8 @@ public function __get($property) /** * Returns the name of the timezone. - * - * @return string */ - public function __toString() + public function __toString(): string { return $this->name; } diff --git a/lib/TimeZoneLocation.php b/lib/TimeZoneLocation.php index 3f188b6..a50c9c2 100644 --- a/lib/TimeZoneLocation.php +++ b/lib/TimeZoneLocation.php @@ -11,6 +11,8 @@ namespace ICanBoogie; +use RuntimeException; + /** * Representation of a time zone location. * @@ -41,12 +43,8 @@ class TimeZoneLocation /** * Creates an instance from a {@link \DateTimeZone} instance. - * - * @param \DateTimeZone $zone - * - * @return \ICanBoogie\TimeZoneLocation */ - static public function from(\DateTimeZone $zone) + static public function from(\DateTimeZone $zone): self { $hash = spl_object_hash($zone); @@ -75,8 +73,6 @@ public function __construct(array $location) * {@link $comments} properties. * * @throws PropertyNotDefined in attempt to get an unsupported property. - * - * @inheritdoc */ public function __get($property) { @@ -85,13 +81,13 @@ public function __get($property) return $this->location[$property]; } - if (class_exists('ICanBoogie\PropertyNotDefined')) + if (class_exists(PropertyNotDefined::class)) { throw new PropertyNotDefined([ $property, $this ]); } else { - throw new \RuntimeException("Property is not defined: $property."); + throw new RuntimeException("Property is not defined: $property."); } } @@ -102,13 +98,13 @@ public function __get($property) */ public function __set($property, $value) { - if (class_exists('ICanBoogie\PropertyNotWritable')) + if (class_exists(PropertyNotWritable::class)) { throw new PropertyNotWritable([ $property, $this ]); } else { - throw new \RuntimeException("Property is not writable: $property."); + throw new RuntimeException("Property is not writable: $property."); } } @@ -117,8 +113,8 @@ public function __set($property, $value) * * @return string */ - public function __toString() + public function __toString(): string { - return "{$this->country_code},{$this->latitude},{$this->longitude}"; + return "$this->country_code,$this->latitude,$this->longitude"; } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..7b5140a --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,15 @@ + + + + + ./lib + + + + + ./tests + + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist deleted file mode 100644 index 18113bc..0000000 --- a/phpunit.xml.dist +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - ./tests - - - - - - ./lib - - - diff --git a/tests/DateTimeTest.php b/tests/DateTimeTest.php index 9fb9204..7b13ae9 100644 --- a/tests/DateTimeTest.php +++ b/tests/DateTimeTest.php @@ -9,19 +9,56 @@ * file that was distributed with this source code. */ -namespace Tests\ICanBoogie; +namespace Test\ICanBoogie; use ICanBoogie\DateTime; -use ICanBoogie\DateTimeTest\MyDateTime; +use ICanBoogie\PropertyNotDefined; +use ICanBoogie\PropertyNotWritable; use PHPUnit\Framework\TestCase; +use Test\ICanBoogie\DateTimeTest\MyDateTime; +use function array_map; +use function preg_split; +use function trim; -class DateTimeTest extends TestCase +final class DateTimeTest extends TestCase { - protected function setUp() + protected function setUp(): void { date_default_timezone_set('Europe/Paris'); } + /** + * @dataProvider provide_readonly + */ + public function test_readonly(string $property): void + { + $d = DateTime::now(); + + $this->expectExceptionMessage("The property `$property` for object of class `ICanBoogie\DateTime` is not writable."); + $this->expectException(PropertyNotWritable::class); + $d->{ $property } = null; + } + + public function provide_readonly(): array + { + $properties = <<assertEquals('2001-01-01 01:01:01', $d->as_db); } - public function provider_test_change_cascade() + public function provider_test_change_cascade(): array { return [ ['2001-01-01 01:02:00', [ 'minute' => 2 ]], @@ -146,7 +183,7 @@ public function provider_test_change_cascade() ]; } - public function test_change_on_timezone_option() + public function test_change_on_timezone_option(): void { $expectedTimezone = 'Asia/Taipei'; $now = new DateTime(); @@ -164,7 +201,7 @@ public function test_change_on_timezone_option() /** * @dataProvider provider_test_change_cascade */ - public function test_change_cascade($expected_datetime, $change_format) + public function test_change_cascade($expected_datetime, $change_format): void { $datetime = '2001-01-01 01:01:01'; @@ -172,7 +209,7 @@ public function test_change_cascade($expected_datetime, $change_format) $this->assertEquals($expected_datetime, $d->change($change_format, true)->as_db); } - public function test_with() + public function test_with(): void { $d = new DateTime('2001-01-01 01:01:01'); $e = $d->with([ 'year' => 2015, 'month' => 5, 'day' => 5 ]); @@ -184,7 +221,7 @@ public function test_with() $this->assertEquals('2015-05-05 01:01:01', $e->as_db); } - public function provider_test_get_year() + public function provider_test_get_year(): array { return [ ['2012-12-16 15:00:00', 2012], @@ -196,20 +233,20 @@ public function provider_test_get_year() /** * @dataProvider provider_test_get_year */ - public function test_get_year($datetime, $expected) + public function test_get_year($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->year); } - public function test_set_year() + public function test_set_year(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->year = 2009; $this->assertEquals('2009-01-01 01:01:01', $d->as_db); } - public function provider_test_get_quarter() + public function provider_test_get_quarter(): array { return [ ['2012-01-16 15:00:00', 1], @@ -230,23 +267,13 @@ public function provider_test_get_quarter() /** * @dataProvider provider_test_get_quarter */ - public function test_get_quarter($datetime, $expected) + public function test_get_quarter($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->quarter); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `quarter` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_quarter() - { - $d = DateTime::now(); - $d->quarter = true; - } - - public function provider_test_get_month() + public function provider_test_get_month(): array { return [ ['2012-01-16 15:00:00', 1], @@ -258,20 +285,20 @@ public function provider_test_get_month() /** * @dataProvider provider_test_get_month */ - public function test_get_month($datetime, $expected) + public function test_get_month($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->month); } - public function test_set_month() + public function test_set_month(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->month = 9; $this->assertEquals('2001-09-01 01:01:01', $d->as_db); } - public function provider_test_get_week() + public function provider_test_get_week(): array { return [ ['2012-01-01 15:00:00', 52], @@ -282,23 +309,13 @@ public function provider_test_get_week() /** * @dataProvider provider_test_get_week */ - public function test_get_week($datetime, $expected) + public function test_get_week($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->week); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `week` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_week() - { - $d = DateTime::now(); - $d->week = true; - } - - public function provider_test_get_year_day() + public function provider_test_get_year_day(): array { return [ ['2012-01-01 15:00:00', 1], @@ -309,23 +326,13 @@ public function provider_test_get_year_day() /** * @dataProvider provider_test_get_year_day */ - public function test_get_year_day($datetime, $expected) + public function test_get_year_day($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->year_day); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `year_day` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_year_day() - { - $d = DateTime::now(); - $d->year_day = true; - } - - public function provider_test_get_weekday() + public function provider_test_get_weekday(): array { return [ ['2012-12-17 15:00:00', 1], @@ -342,23 +349,13 @@ public function provider_test_get_weekday() * Sunday must be 7, Monday must be 1. * @dataProvider provider_test_get_weekday */ - public function test_get_weekday($datetime, $expected) + public function test_get_weekday($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->weekday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `weekday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_weekday() - { - $d = DateTime::now(); - $d->weekday = true; - } - - public function provider_test_get_day() + public function provider_test_get_day(): array { return [ ['2012-12-16 15:00:00', 16], @@ -370,59 +367,59 @@ public function provider_test_get_day() /** * @dataProvider provider_test_get_day */ - public function test_get_day($datetime, $expected) + public function test_get_day($datetime, $expected): void { $d = new DateTime($datetime); $this->assertEquals($expected, $d->day); } - public function test_set_day() + public function test_set_day(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->day = 9; $this->assertEquals('2001-01-09 01:01:01', $d->as_db); } - public function test_get_hour() + public function test_get_hour(): void { $d = new DateTime('2013-01-01 01:23:45'); $this->assertEquals(1, $d->hour); } - public function test_set_hour() + public function test_set_hour(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->hour = 9; $this->assertEquals('2001-01-01 09:01:01', $d->as_db); } - public function test_get_minute() + public function test_get_minute(): void { $d = new DateTime('2013-01-01 01:23:45'); $this->assertEquals(23, $d->minute); } - public function test_set_minute() + public function test_set_minute(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->minute = 9; $this->assertEquals('2001-01-01 01:09:01', $d->as_db); } - public function test_get_second() + public function test_get_second(): void { $d = new DateTime('2013-01-01 01:23:45'); $this->assertEquals(45, $d->second); } - public function test_set_second() + public function test_set_second(): void { $d = new DateTime('2001-01-01 01:01:01'); $d->second = 9; $this->assertEquals('2001-01-01 01:01:09', $d->as_db); } - public function test_get_is_monday() + public function test_get_is_monday(): void { $d = new DateTime('2013-02-04 21:00:00', 'utc'); $this->assertTrue($d->is_monday); @@ -434,17 +431,7 @@ public function test_get_is_monday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_monday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_monday() - { - $d = new DateTime('2013-02-04 21:00:00', 'utc'); - $d->is_monday = true; - } - - public function test_get_is_tuesday() + public function test_get_is_tuesday(): void { $d = new DateTime('2013-02-05 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -456,17 +443,7 @@ public function test_get_is_tuesday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_tuesday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_tuesday() - { - $d = new DateTime('2013-02-05 21:00:00', 'utc'); - $d->is_tuesday = true; - } - - public function test_get_is_wednesday() + public function test_get_is_wednesday(): void { $d = new DateTime('2013-02-06 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -478,17 +455,7 @@ public function test_get_is_wednesday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_wednesday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_wednesday() - { - $d = new DateTime('2013-02-06 21:00:00', 'utc'); - $d->is_wednesday = true; - } - - public function test_get_is_thursday() + public function test_get_is_thursday(): void { $d = new DateTime('2013-02-07 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -500,17 +467,7 @@ public function test_get_is_thursday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_thursday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_thursday() - { - $d = new DateTime('2013-02-07 21:00:00', 'utc'); - $d->is_thursday = true; - } - - public function test_get_is_friday() + public function test_get_is_friday(): void { $d = new DateTime('2013-02-08 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -522,17 +479,7 @@ public function test_get_is_friday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_friday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_friday() - { - $d = new DateTime('2013-02-08 21:00:00', 'utc'); - $d->is_friday = true; - } - - public function test_get_is_saturday() + public function test_get_is_saturday(): void { $d = new DateTime('2013-02-09 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -544,17 +491,7 @@ public function test_get_is_saturday() $this->assertFalse($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_saturday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_saturday() - { - $d = new DateTime('2013-02-09 21:00:00', 'utc'); - $d->is_saturday = true; - } - - public function test_get_is_sunday() + public function test_get_is_sunday(): void { $d = new DateTime('2013-02-10 21:00:00', 'utc'); $this->assertFalse($d->is_monday); @@ -566,17 +503,7 @@ public function test_get_is_sunday() $this->assertTrue($d->is_sunday); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_sunday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_sunday() - { - $d = new DateTime('2013-02-10 21:00:00', 'utc'); - $d->is_sunday = true; - } - - public function test_get_is_today() + public function test_get_is_today(): void { $d = DateTime::now(); $d->zone = 'Asia/Tokyo'; @@ -585,17 +512,7 @@ public function test_get_is_today() $this->assertFalse($d->yesterday->is_today); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_today` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_today() - { - $d = DateTime::now(); - $d->is_today = true; - } - - public function test_get_is_past() + public function test_get_is_past(): void { $d = DateTime::now(); $d->zone = 'Asia/Tokyo'; @@ -605,17 +522,7 @@ public function test_get_is_past() $this->assertFalse($d->is_past); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_past` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_past() - { - $d = DateTime::now(); - $d->is_past = true; - } - - public function test_get_is_future() + public function test_get_is_future(): void { $d = DateTime::now(); $d->zone = 'Asia/Tokyo'; @@ -625,16 +532,6 @@ public function test_get_is_future() $this->assertTrue($d->is_future); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_future` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_future() - { - $d = DateTime::now(); - $d->is_future = true; - } - public function test_get_is_empty() { $d = DateTime::none(); @@ -649,74 +546,28 @@ public function test_get_is_empty() $this->assertFalse($d->is_empty); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_empty` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_empty() - { - DateTime::now()->is_empty = true; - } - public function test_get_tomorrow() { $d = new DateTime('2013-02-10 21:21:21', 'utc'); $this->assertEquals('2013-02-11 00:00:00', $d->tomorrow->as_db); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `tomorrow` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_tomorrow() - { - $d = DateTime::now(); - $d->tomorrow = true; - } - public function test_get_yesterday() { $d = new DateTime('2013-02-10 21:21:21', 'utc'); $this->assertEquals('2013-02-09 00:00:00', $d->yesterday->as_db); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `yesterday` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_yesterday() - { - $d = DateTime::now(); - $d->yesterday = true; - } - - /** - * @dataProvider provide_writeonly_property - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessageReg The property `\w+` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_writeonly_property($property) - { - DateTime::now()->$property = null; - } - - public function provide_writeonly_property() - { - $properties = 'monday|tuesday|wednesday|thursday|friday|saturday|sunday'; - - return array_map(function($v) { return (array) $v; }, explode('|', $properties)); - } - /** * @dataProvider provide_test_day_instance */ - public function test_day_instance($date, $expected, $day) + public function test_day_instance($date, $expected, $day): void { $d = new DateTime($date); $this->assertEquals($expected, $d->$day->as_db); } - public function provide_test_day_instance() + public function provide_test_day_instance(): array { return [ @@ -835,21 +686,21 @@ public function provide_test_day_instance() ]; } - public function test_far_past() + public function test_far_past(): void { $d = new DateTime('-4712-12-07 12:06:46'); $this->assertEquals(-4712, $d->year); $this->assertEquals('-4712-12-07 12:06:46', $d->as_db); } - public function test_far_future() + public function test_far_future(): void { $d = new DateTime('4712-12-07 12:06:46'); $this->assertEquals(4712, $d->year); $this->assertEquals('4712-12-07 12:06:46', $d->as_db); } - public function test_get_utc() + public function test_get_utc(): void { $d = new DateTime('2013-03-06 18:00:00', 'Europe/Paris'); $utc = $d->utc; @@ -858,17 +709,7 @@ public function test_get_utc() $this->assertEquals('2013-03-06 17:00:00', $utc->as_db); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `utc` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_utc() - { - $d = DateTime::now(); - $d->utc = null; - } - - public function test_get_is_utc() + public function test_get_is_utc(): void { $d = DateTime::now(); $d->zone = 'Asia/Tokyo'; @@ -877,16 +718,6 @@ public function test_get_is_utc() $this->assertTrue($d->is_utc); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_utc` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_utc() - { - $d = DateTime::now(); - $d->is_utc = true; - } - public function test_get_local() { $d = new DateTime('2013-03-06 17:00:00', 'UTC'); @@ -896,16 +727,6 @@ public function test_get_local() $this->assertEquals('2013-03-06 18:00:00', $local->as_db); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `local` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_local() - { - $d = DateTime::now(); - $d->local = true; - } - public function test_get_is_local() { $d = DateTime::now(); @@ -915,16 +736,6 @@ public function test_get_is_local() $this->assertTrue($d->is_local); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_local` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_local() - { - $d = DateTime::now(); - $d->is_local = true; - } - public function test_get_is_dst() { $d = new DateTime('2013-02-03 21:00:00'); @@ -933,16 +744,6 @@ public function test_get_is_dst() $this->assertTrue($d->is_dst); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `is_dst` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_is_dst() - { - $d = DateTime::now(); - $d->is_dst = true; - } - public function test_format() { $empty = DateTime::none(); @@ -955,28 +756,18 @@ public function test_format() * Predefined formats */ - public function test_format_as_atom() + public function test_format_as_atom(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::ATOM), $now->format_as_atom()); } - public function test_get_as_atom() + public function test_get_as_atom(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::ATOM), $now->as_atom); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_atom` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_atom() - { - $d = DateTime::now(); - $d->as_atom = true; - } - public function test_format_as_cookie() { $now = DateTime::now(); @@ -992,251 +783,151 @@ public function test_get_as_cookie() $this->assertEquals("Monday, 04-Nov-2013 20:21:22 UTC", $date->as_cookie); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_cookie` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_cookie() - { - $d = DateTime::now(); - $d->as_cookie = true; - } - - public function test_format_as_iso8601() + public function test_format_as_iso8601(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::ISO8601), $now->format_as_iso8601()); } - public function test_format_as_iso8601_utc() + public function test_format_as_iso8601_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'Z', $now->format(DateTime::ISO8601)), $now->format_as_iso8601()); } - public function test_get_as_iso8601() + public function test_get_as_iso8601(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::ISO8601), $now->as_iso8601); } - public function test_as_iso8601_utc() + public function test_as_iso8601_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'Z', $now->format(DateTime::ISO8601)), $now->as_iso8601); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_iso8601` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_iso8601() - { - $d = DateTime::now(); - $d->as_iso8601 = true; - } - - public function test_format_as_rfc822() + public function test_format_as_rfc822(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC822), $now->format_as_rfc822()); } - public function test_format_as_rfc822_utc() + public function test_format_as_rfc822_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'GMT', $now->format(DateTime::RFC822)), $now->format_as_rfc822()); } - public function test_get_as_rfc822() + public function test_get_as_rfc822(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC822), $now->as_rfc822); } - public function test_get_as_rfc822_utc() + public function test_get_as_rfc822_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'GMT', $now->format(DateTime::RFC822)), $now->as_rfc822); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc822` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc822() - { - $d = DateTime::now(); - $d->as_rfc822 = true; - } - - public function test_format_as_rfc850() + public function test_format_as_rfc850(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC850), $now->format_as_rfc850()); } - public function test_get_as_rfc850() + public function test_get_as_rfc850(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC850), $now->as_rfc850); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc850` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc850() - { - $d = DateTime::now(); - $d->as_rfc850 = true; - } - - public function test_format_as_rfc1036() + public function test_format_as_rfc1036(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC1036), $now->format_as_rfc1036()); } - public function test_get_as_rfc1036() + public function test_get_as_rfc1036(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC1036), $now->as_rfc1036); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc1036` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc1036() - { - $d = DateTime::now(); - $d->as_rfc1036 = true; - } - - public function test_format_as_rfc1123() + public function test_format_as_rfc1123(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC1123), $now->format_as_rfc1123()); } - public function test_format_as_rfc1123_utc() + public function test_format_as_rfc1123_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'GMT', $now->format(DateTime::RFC1123)), $now->format_as_rfc1123()); } - public function test_get_as_rfc1123() + public function test_get_as_rfc1123(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC1123), $now->as_rfc1123); } - public function test_get_as_rfc1123_utc() + public function test_get_as_rfc1123_utc(): void { $now = DateTime::now()->utc; $this->assertEquals(str_replace('+0000', 'GMT', $now->format(DateTime::RFC1123)), $now->as_rfc1123); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc1123` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc1123() - { - $d = DateTime::now(); - $d->as_rfc1123 = true; - } - - public function test_format_as_rfc2822() + public function test_format_as_rfc2822(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC2822), $now->format_as_rfc2822()); } - public function test_get_as_rfc2822() + public function test_get_as_rfc2822(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC2822), $now->as_rfc2822); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc2822` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc2822() - { - $d = DateTime::now(); - $d->as_rfc2822 = true; - } - - public function test_format_as_rfc3339() + public function test_format_as_rfc3339(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC3339), $now->format_as_rfc3339()); } - public function test_get_as_rfc3339() + public function test_get_as_rfc3339(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RFC3339), $now->as_rfc3339); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rfc3339` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rfc3339() - { - $d = DateTime::now(); - $d->as_rfc3339 = true; - } - - public function test_format_as_rss() + public function test_format_as_rss(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RSS), $now->format_as_rss()); } - public function test_get_as_rss() + public function test_get_as_rss(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::RSS), $now->as_rss); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_rss` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_rss() - { - $d = DateTime::now(); - $d->as_rss = true; - } - - public function test_format_as_w3c() + public function test_format_as_w3c(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::W3C), $now->format_as_w3c()); } - public function test_get_as_w3c() + public function test_get_as_w3c(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::W3C), $now->as_w3c); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_w3c` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_w3c() - { - $d = DateTime::now(); - $d->as_w3c = true; - } - - public function test_format_as_db() + public function test_format_as_db(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::DB), $now->format_as_db()); @@ -1244,7 +935,7 @@ public function test_format_as_db() $this->assertEquals('0000-00-00 00:00:00', $empty->format_as_db()); } - public function test_get_as_db() + public function test_get_as_db(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::DB), $now->as_db); @@ -1252,39 +943,19 @@ public function test_get_as_db() $this->assertEquals('0000-00-00 00:00:00', $empty->as_db); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_db` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_db() - { - $d = DateTime::now(); - $d->as_db = true; - } - - public function test_format_as_number() + public function test_format_as_number(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::NUMBER), $now->format_as_number()); } - public function test_get_as_number() + public function test_get_as_number(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::NUMBER), $now->as_number); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_number` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_number() - { - $d = DateTime::now(); - $d->as_number = true; - } - - public function test_format_as_date() + public function test_format_as_date(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::DATE), $now->format_as_date()); @@ -1292,7 +963,7 @@ public function test_format_as_date() $this->assertEquals('0000-00-00', $empty->format_as_date()); } - public function test_get_as_date() + public function test_get_as_date(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::DATE), $now->as_date); @@ -1300,39 +971,19 @@ public function test_get_as_date() $this->assertEquals('0000-00-00', $empty->as_date); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_date` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_date() - { - $d = DateTime::now(); - $d->as_date = true; - } - - public function test_format_as_time() + public function test_format_as_time(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::TIME), $now->format_as_time()); } - public function test_get_as_time() + public function test_get_as_time(): void { $now = DateTime::now(); $this->assertEquals($now->format(DateTime::TIME), $now->as_time); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `as_time` for object of class `ICanBoogie\DateTime` is not writable. - */ - public function test_set_as_time() - { - $d = DateTime::now(); - $d->as_time = true; - } - - public function test_compare() + public function test_compare(): void { $d1 = DateTime::now(); $d2 = DateTime::now(); @@ -1354,42 +1005,42 @@ public function test_compare() $this->assertSame($tomorrow, max($now, $yesterday, $tomorrow)); } - public function test_json_serialize() + public function test_json_serialize(): void { $date = new DateTime("2014-10-23 13:50:10", "Europe/Paris"); $this->assertEquals('{"date":"2014-10-23T13:50:10+0200"}', json_encode([ 'date' => $date ])); } - public function test_localize() + public function test_localize(): void { $invoked = false; $reference = DateTime::now(); DateTime::$localizer = function(DateTime $datetime, $locale) use (&$invoked, &$reference) { + $invoked = true; $this->assertSame($datetime, $reference); $this->assertEquals('fr', $locale); }; $reference->localize('fr'); + + $this->assertTrue($invoked); } - /** - * @expectedException \RuntimeException - * @expectedExceptionMessageReg Undefined property `\w+` for object of class `ICanBoogie\DateTime`. - */ public function test_getting_undefined_property_should_throw_exception() { $date = DateTime::now(); $property = uniqid(); + $this->expectException(PropertyNotDefined::class); + $this->expectExceptionMessage("Undefined property `$property` for object of class `ICanBoogie\DateTime`."); $date->$property; } } -namespace ICanBoogie\DateTimeTest; +namespace Test\ICanBoogie\DateTimeTest; -class MyDateTime extends \ICanBoogie\DateTime +final class MyDateTime extends \ICanBoogie\DateTime { - } diff --git a/tests/TimeZoneLocationTest.php b/tests/TimeZoneLocationTest.php index 81072f2..d5ad980 100644 --- a/tests/TimeZoneLocationTest.php +++ b/tests/TimeZoneLocationTest.php @@ -9,154 +9,127 @@ * file that was distributed with this source code. */ -namespace Tests\ICanBoogie; +namespace Test\ICanBoogie; +use DateTimeZone; +use ICanBoogie\DateTime; +use ICanBoogie\PropertyNotDefined; +use ICanBoogie\PropertyNotWritable; use ICanBoogie\TimeZoneLocation; use PHPUnit\Framework\TestCase; +use function array_map; +use function preg_split; +use function trim; class TimeZoneLocationTest extends TestCase { - public function test_from() - { - $zone = new \DateTimeZone('Europe/Paris'); - $location = TimeZoneLocation::from($zone); - - $this->assertInstanceOf(TimeZoneLocation::class, $location); - } - - public function test_from_cache() - { - $zone = new \DateTimeZone('Europe/Paris'); - $location = TimeZoneLocation::from($zone); - $cached = TimeZoneLocation::from($zone); - $this->assertEquals(spl_object_hash($location), spl_object_hash($cached)); - } - - public function test_location() - { - $zone = new \DateTimeZone('Europe/Paris'); - $location = $zone->getLocation(); - new TimeZoneLocation($location); - } - - public function test_get_coutry_code() + /** + * @dataProvider provide_readonly + */ + public function test_readonly(string $property): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $this->assertEquals($location['country_code'], $instance->country_code); + $this->expectExceptionMessage("The property `$property` for object of class `ICanBoogie\TimeZoneLocation` is not writable."); + $this->expectException(PropertyNotWritable::class); + $instance->$property = null; } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `country_code` for object of class `ICanBoogie\TimeZoneLocation` is not writable. - */ - public function test_set_country_code() + public function provide_readonly(): array { - $zone = new \DateTimeZone('Europe/Paris'); - $location = $zone->getLocation(); - $instance = new TimeZoneLocation($location); - - $instance->country_code = true; + $properties = <<getLocation(); - $instance = new TimeZoneLocation($location); + $zone = new DateTimeZone('Europe/Paris'); + $location = TimeZoneLocation::from($zone); - $this->assertEquals($location['latitude'], $instance->latitude); + $this->assertInstanceOf(TimeZoneLocation::class, $location); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `latitude` for object of class `ICanBoogie\TimeZoneLocation` is not writable. - */ - public function test_set_latitude() + public function test_from_cache(): void { - $zone = new \DateTimeZone('Europe/Paris'); - $location = $zone->getLocation(); - $instance = new TimeZoneLocation($location); + $zone = new DateTimeZone('Europe/Paris'); + $location = TimeZoneLocation::from($zone); + $cached = TimeZoneLocation::from($zone); - $instance->latitude = true; + $this->assertEquals(spl_object_hash($location), spl_object_hash($cached)); } - public function test_get_longitude() + public function test_location(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); - $instance = new TimeZoneLocation($location); + $l = new TimeZoneLocation($location); - $this->assertEquals($location['longitude'], $instance->longitude); + $this->assertEquals("FR", $l->country_code); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `longitude` for object of class `ICanBoogie\TimeZoneLocation` is not writable. - */ - public function test_set_longitude() + public function test_get_country_code(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $instance->longitude = true; + $this->assertEquals($location['country_code'], $instance->country_code); } - public function test_get_comments() + public function test_get_latitude(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $this->assertEquals($location['comments'], $instance->comments); + $this->assertEquals($location['latitude'], $instance->latitude); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `comments` for object of class `ICanBoogie\TimeZoneLocation` is not writable. - */ - public function test_set_comments() + public function test_get_longitude(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $instance->comments = true; + $this->assertEquals($location['longitude'], $instance->longitude); } - /** - * @expectedException \ICanBoogie\PropertyNotDefined - * @expectedExceptionMessage Undefined property `you_know_that_i_m_no_good` for object of class `ICanBoogie\TimeZoneLocation`. - */ - public function test_get_unsupported_property() + public function test_get_comments(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $instance->you_know_that_i_m_no_good; + $this->assertEquals($location['comments'], $instance->comments); } - /** - * @expectedException \ICanBoogie\PropertyNotWritable - * @expectedExceptionMessage The property `unsupported_property` for object of class `ICanBoogie\TimeZoneLocation` is not writable. - */ - public function test_set_unsupported_property() + public function test_get_unsupported_property(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); - $instance->unsupported_property = true; + $this->expectExceptionMessage("Undefined property `you_know_that_i_m_no_good` for object of class `ICanBoogie\TimeZoneLocation`."); + $this->expectException(PropertyNotDefined::class); + $instance->you_know_that_i_m_no_good; } - public function test_to_string() + public function test_to_string(): void { - $zone = new \DateTimeZone('Europe/Paris'); + $zone = new DateTimeZone('Europe/Paris'); $location = $zone->getLocation(); $instance = new TimeZoneLocation($location); diff --git a/tests/TimeZoneTest.php b/tests/TimeZoneTest.php index 6102ffa..9ee69b4 100644 --- a/tests/TimeZoneTest.php +++ b/tests/TimeZoneTest.php @@ -9,27 +9,28 @@ * file that was distributed with this source code. */ -namespace Tests\ICanBoogie; +namespace Test\ICanBoogie; +use ICanBoogie\PropertyNotDefined; use ICanBoogie\TimeZone; use ICanBoogie\TimeZoneLocation; use PHPUnit\Framework\TestCase; -class TimeZoneTest extends TestCase +final class TimeZoneTest extends TestCase { - public function test_get_location() + public function test_get_location(): void { $z = new TimeZone('Europe/Paris'); $this->assertInstanceOf(TimeZoneLocation::class, $z->location); } - public function test_get_name() + public function test_get_name(): void { $z = new TimeZone('Europe/Paris'); $this->assertEquals($z->getName(), $z->name); } - public function test_get_offset() + public function test_get_offset(): void { $z = new TimeZone('Europe/Paris'); $utc = new \DateTime('now', new \DateTimeZone('utc')); @@ -37,7 +38,7 @@ public function test_get_offset() $this->assertEquals($z->getOffset($utc), $z->offset); } - public function test_utc_case() + public function test_utc_case(): void { $this->assertEquals('UTC', TimeZone::from('utc')->name); $this->assertEquals('UTC', TimeZone::from('UTC')->name); @@ -45,7 +46,7 @@ public function test_utc_case() $this->assertEquals('UTC', (new TimeZone('UTC'))->name); } - public function test_should_create_from_php_datetimezone() + public function test_should_create_from_php_datetimezone(): void { $name = 'Europe/Paris'; $z1 = new \DateTimeZone($name); @@ -54,7 +55,7 @@ public function test_should_create_from_php_datetimezone() $this->assertSame($name, $z2->name); } - public function test_should_reuse_instance() + public function test_should_reuse_instance(): void { $z1 = TimeZone::from('utc'); $z2 = TimeZone::from('utc'); @@ -62,7 +63,7 @@ public function test_should_reuse_instance() $this->assertSame($z1, $z2); } - public function test_should_reuse_timezone() + public function test_should_reuse_timezone(): void { $z1 = TimeZone::from('Europe/Paris'); $z2 = TimeZone::from($z1); @@ -70,17 +71,15 @@ public function test_should_reuse_timezone() $this->assertSame($z1, $z2); } - /** - * @expectedException \RuntimeException - */ - public function test_getting_undefined_property_should_throw_exception() + public function test_getting_undefined_property_should_throw_exception(): void { $property = uniqid(); $z1 = TimeZone::from('utc'); + $this->expectException(PropertyNotDefined::class); $z1->$property; } - public function test_to_string() + public function test_to_string(): void { $name = 'Europe/Paris'; $z1 = TimeZone::from($name);