Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed May 7, 2017
0 parents commit bc2caa6
Show file tree
Hide file tree
Showing 12 changed files with 375 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor
phpunit.xml
composer.lock
humbuglog.txt
coverage
66 changes: 66 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
build:
environment:
mysql: false
postgresql: false
redis: false
rabbitmq: false
php:
version: 7.1.0
tools:
php_sim: true
php_pdepend: true
php_analyzer: true
php_changetracking: true
sensiolabs_security_checker: true
php_mess_detector: true
php_code_coverage: true
external_code_coverage:
timeout: 1800
php_code_sniffer:
enabled: true
config:
standard: "PSR2"
php_cpd:
enabled: true
excluded_dirs: ["test", "vendor"]
php_loc:
enabled: true
excluded_dirs: ["test", "vendor"]
checks:
php:
code_rating: true
duplication: true
argument_type_checks: true
assignment_of_null_return: true
avoid_conflicting_incrementers: true
avoid_useless_overridden_methods: true
catch_class_exists: true
closure_use_modifiable: true
closure_use_not_conflicting: true
deprecated_code_usage: true
method_calls_on_non_object: true
missing_arguments: true
no_duplicate_arguments: true
no_non_implemented_abstract_methods: true
no_property_on_interface: true
parameter_non_unique: true
precedence_in_conditions: true
precedence_mistakes: true
require_php_tag_first: true
security_vulnerabilities: true
sql_injection_vulnerabilities: true
too_many_arguments: true
unreachable_code: true
unused_methods: true
unused_parameters: true
unused_properties: true
unused_variables: true
use_statement_alias_conflict: true
useless_calls: true
variable_existence: true
verify_access_scope_valid: true
verify_argument_usable_as_reference: true
verify_property_names: true
filter:
excluded_paths:
- test/*
22 changes: 22 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
language: php
sudo: false

php:
- 7.1
- nightly

cache:
directories:
- $HOME/.composer/cache

before_script:
- if [[ $TRAVIS_PHP_VERSION = '7.1' ]]; then PHPUNIT_FLAGS="--coverage-clover ./clover.xml"; else PHPUNIT_FLAGS=""; fi
- composer self-update
- composer install

script:
- ./vendor/bin/phpunit $PHPUNIT_FLAGS

after_script:
- wget https://scrutinizer-ci.com/ocular.phar
- if [ -f clover.xml ]; then php ocular.phar code-coverage:upload --format=php-clover ./clover.xml; fi
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 Luís Cobucci

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Clock

[![Total Downloads](https://img.shields.io/packagist/dt/lcobucci/clock.svg?style=flat-square)](https://packagist.org/packages/lcobucci/clock)
[![Latest Stable Version](https://img.shields.io/packagist/v/lcobucci/clock.svg?style=flat-square)](https://packagist.org/packages/lcobucci/clock)

![Branch master](https://img.shields.io/badge/branch-master-brightgreen.svg?style=flat-square)
[![Build Status](https://img.shields.io/travis/lcobucci/clock/master.svg?style=flat-square)](http://travis-ci.org/#!/lcobucci/clock)
[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/lcobucci/clock/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/clock/?branch=master)
[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/lcobucci/clock/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/lcobucci/clock/?branch=master)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/50e3ef67-0f42-48fe-ace5-0beb9f78d117/mini.png)](https://insight.sensiolabs.com/projects/50e3ef67-0f42-48fe-ace5-0beb9f78d117)

Yet another clock abstraction...

The purpose is to decouple projects from `DateTimeImmutable` instantiation so that
we can test things properly.

## Installation

Package is available on [Packagist](http://packagist.org/packages/lcobucci/clock),
you can install it using [Composer](http://getcomposer.org).

```shell
composer require lcobucci/clock
```

## Basic usage

Simply create an make your objects dependent on the
interface `Lcobucci\Clock\Clock` and use `SystemClock` or
`FrozenClock` to retrieve the current time:

```php
<?php

use Lcobucci\Clock\Clock;
use Lcobucci\Clock\SystemClock;
use Lcobucci\Clock\FrozenClock;

function filterData(Clock $clock, array $objects): array
{
return array_filter(
$objects,
function (stdClass $object) use ($clock): bool {
return $object->expiresAt > $clock->now();
}
);
}

// Object that will return the current time based on the given timezone
$clock = new SystemClock(new DateTimeZone('UTC'));

// Test object that always returns a fixed time object
$clock = new FrozenClock(
new DateTimeImmutable('2017-05-07 18:49:30')
);

$objects = [
(object) ['expiresAt' => new DateTimeImmutable('2017-12-31 23:59:59')],
(object) ['expiresAt' => new DateTimeImmutable('2017-06-30 23:59:59')],
(object) ['expiresAt' => new DateTimeImmutable('2017-01-30 23:59:59')],
];

var_dump(filterData($clock, $objects)); // last item will be filtered
```
37 changes: 37 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "lcobucci/clock",
"description": "Yet another clock abstraction",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "Luís Cobucci",
"email": "[email protected]"
}
],
"require": {
"php": "^7.1"
},
"require-dev": {
"phpunit/phpunit": "^6.1"
},
"autoload": {
"psr-4": {
"Lcobucci\\Clock\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Lcobucci\\Clock\\": "src"
}
},
"config": {
"preferred-install": "dist",
"sort-packages": true
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
}
}
}
29 changes: 29 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
colors="true"
verbose="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutTodoAnnotatedTests="true"
beStrictAboutChangesToGlobalState="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutResourceUsageDuringSmallTests="true"
forceCoversAnnotation="true">

<php>
<ini name="date.timezone" value="UTC" />
</php>

<testsuites>
<testsuite name="unit">
<directory>test</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
12 changes: 12 additions & 0 deletions src/Clock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Lcobucci\Clock;

use DateTimeImmutable;

interface Clock
{
public function now(): DateTimeImmutable;
}
25 changes: 25 additions & 0 deletions src/FrozenClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Lcobucci\Clock;

use DateTimeImmutable;

final class FrozenClock implements Clock
{
/**
* @var DateTimeImmutable
*/
private $now;

public function __construct(DateTimeImmutable $now)
{
$this->now = $now;
}

public function now(): DateTimeImmutable
{
return $this->now;
}
}
26 changes: 26 additions & 0 deletions src/SystemClock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Lcobucci\Clock;

use DateTimeImmutable;
use DateTimeZone;

final class SystemClock implements Clock
{
/**
* @var DateTimeZone
*/
private $timezone;

public function __construct(DateTimeZone $timezone = null)
{
$this->timezone = $timezone ?: new DateTimeZone(date_default_timezone_get());
}

public function now(): DateTimeImmutable
{
return new DateTimeImmutable('now', $this->timezone);
}
}
26 changes: 26 additions & 0 deletions test/FrozenClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace Lcobucci\Clock;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;

final class FrozenClockTest extends TestCase
{
/**
* @test
*
* @covers \Lcobucci\Clock\FrozenClock::__construct
* @covers \Lcobucci\Clock\FrozenClock::now
*/
public function nowShouldReturnAlwaysTheSameObject()
{
$now = new DateTimeImmutable();
$clock = new FrozenClock($now);

self::assertSame($now, $clock->now());
self::assertSame($now, $clock->now());
}
}
42 changes: 42 additions & 0 deletions test/SystemClockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Lcobucci\Clock;

use DateTimeImmutable;
use DateTimeZone;
use PHPUnit\Framework\TestCase;

final class SystemClockTest extends TestCase
{
/**
* @test
*
* @covers \Lcobucci\Clock\SystemClock::__construct
*/
public function constructShouldUseSystemDefaultTimezoneIfNoneWasProvided(): void
{
self::assertAttributeEquals(new DateTimeZone('UTC'), 'timezone', new SystemClock());
}

/**
* @test
*
* @covers \Lcobucci\Clock\SystemClock::__construct
* @covers \Lcobucci\Clock\SystemClock::now
*/
public function nowShouldRespectTheProvidedTimezone(): void
{
$timezone = new DateTimeZone('America/Sao_Paulo');
$clock = new SystemClock($timezone);

$lower = new DateTimeImmutable('now', $timezone);
$now = $clock->now();
$upper = new DateTimeImmutable('now', $timezone);

self::assertEquals($timezone, $now->getTimezone());
self::assertGreaterThanOrEqual($lower, $now);
self::assertLessThanOrEqual($upper, $now);
}
}

0 comments on commit bc2caa6

Please sign in to comment.