Skip to content

Commit

Permalink
Merge 7b8324c into 154df95
Browse files Browse the repository at this point in the history
  • Loading branch information
sj-i committed Aug 16, 2020
2 parents 154df95 + 7b8324c commit 93794a9
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 77 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: build

on:
push:
branches:
- '*'
pull_request:
branches:
- '*'

jobs:

phpunit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Setup PHP Action
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
extensions: dom, mbstring
coverage: pcov

- name: Get Composer Cache Directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
restore-keys: ${{ runner.os }}-composer-

- name: Validate composer.json and composer.lock
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest

- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Run test suite
run: |
mkdir -p build/logs
./vendor/bin/phpunit --coverage-clover build/logs/clover.xml
- name: Send to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./vendor/bin/php-coveralls -v
4 changes: 2 additions & 2 deletions .github/workflows/phpcs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
php-version: 7.4
extensions: dom, mbstring
coverage: none
tools: cs2pr
tools: cs2pr, phpcs

- name: Get Composer Cache Directory
id: composer-cache
Expand All @@ -39,4 +39,4 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run phpcs
run: vendor/bin/phpcs --standard=./phpcs.xml --report=checkstyle -q ./src ./tests | cs2pr
run: phpcs --standard=./phpcs.xml --report=checkstyle -q ./src ./tests | cs2pr
15 changes: 6 additions & 9 deletions .github/workflows/phpunit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,19 @@ on:
jobs:

phpunit-test:
name: phpunit test
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.0', '7.1', '7.2', '7.3', '7.4']
name: PHP ${{ matrix.php-versions }} Test
steps:
- uses: actions/checkout@v2

- name: Setup PHP Action
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: ${{ matrix.php-versions }}
extensions: dom, mbstring
coverage: pcov

- name: Get Composer Cache Directory
id: composer-cache
Expand Down Expand Up @@ -49,9 +51,4 @@ jobs:
- name: Run test suite
run: |
mkdir -p build/logs
./vendor/bin/phpunit --do-not-cache-result --coverage-clover build/logs/clover.xml
- name: Send to Coveralls
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: ./vendor/bin/php-coveralls -v
./vendor/bin/phpunit
3 changes: 2 additions & 1 deletion .github/workflows/static-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
php-version: 7.4
extensions: dom, mbstring
coverage: none
tools: psalm

- name: Get Composer Cache Directory
id: composer-cache
Expand All @@ -44,4 +45,4 @@ jobs:
run: composer install --prefer-dist --no-progress --no-suggest

- name: Run psalm
run: ./vendor/bin/psalm --shepherd --show-info=true --diff --diff-methods --output-format=github
run: psalm --shepherd --show-info=true --diff --diff-methods --output-format=github
4 changes: 1 addition & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,10 @@
}
],
"require": {
"php": "^7.4 || ^8.0"
"php": "^7.0 || ^8.0"
},
"require-dev": {
"phpunit/phpunit": "^6.5 || ^7.0 || ^8.0 || ^9.0",
"vimeo/psalm": "^3.12",
"squizlabs/php_codesniffer": "^3.5",
"twinh/php-coveralls": "^2.3"
},
"autoload": {
Expand Down
121 changes: 88 additions & 33 deletions tests/PhpCast/CastTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class CastTest extends TestCase
{
public function testToInt(): void
public function testToInt()
{
$this->assertSame(0, Cast::toInt(0));
$this->assertSame(1, Cast::toInt(1));
Expand All @@ -35,30 +35,57 @@ public function testToInt(): void
$this->assertSame(0, Cast::toInt(false));
$this->assertSame(1, Cast::toInt(true));

$this->assertTypeError(fn () => Cast::toInt(''));
$this->assertTypeError(fn () => Cast::toInt('a'));
$this->assertTypeError(fn () => Cast::toInt('ok'));
$this->assertTypeError(fn () => Cast::toInt('true'));
$this->assertTypeError(fn () => Cast::toInt('null'));
$this->assertTypeError(fn () => Cast::toInt(null));
$this->assertTypeError(fn () => Cast::toInt([]));
$this->assertTypeError(fn () => Cast::toInt((object)[]));
$this->assertTypeError(fn () => Cast::toInt(PHP_INT_MAX + 1));
$this->assertTypeError(fn () => Cast::toInt(-9223372036854776833));
$this->assertTypeError(function () {
return Cast::toInt('');
});
$this->assertTypeError(function () {
return Cast::toInt('a');
});
$this->assertTypeError(function () {
return Cast::toInt('ok');
});
$this->assertTypeError(function () {
return Cast::toInt('true');
});
$this->assertTypeError(function () {
return Cast::toInt('null');
});
$this->assertTypeError(function () {
return Cast::toInt(null);
});
$this->assertTypeError(function () {
return Cast::toInt([]);
});
$this->assertTypeError(function () {
return Cast::toInt((object)[]);
});
$this->assertTypeError(function () {
return Cast::toInt(PHP_INT_MAX + 1);
});
$this->assertTypeError(function () {
return Cast::toInt(-9223372036854776833);
});

try {
Cast::toInt('123abc');
} catch (\Throwable $e) {
}
$this->assertTrue(isset($e));
$this->assertInstanceOf(TypeError::class, $e);
$this->assertInstanceOf(Exception::class, $e->getPrevious());
$this->assertSame('A non well formed numeric value encountered', $e->getPrevious()->getMessage());
if (version_compare(PHP_VERSION, '7.1.0') >= 0) {
$this->assertTrue(isset($e));
if (version_compare(PHP_VERSION, '7.4.0') >= 0) {
$this->assertInstanceOf(TypeError::class, $e);
$this->assertInstanceOf(Exception::class, $e->getPrevious());
$this->assertSame('A non well formed numeric value encountered', $e->getPrevious()->getMessage());
} else {
$this->assertInstanceOf(Exception::class, $e);
$this->assertSame('A non well formed numeric value encountered', $e->getMessage());
}
}
$result = @Cast::toInt('123abc');
$this->assertSame(123, $result);
}

public function testToFloat(): void
public function testToFloat()
{
$this->assertSame(0.0, Cast::toFloat(0));
$this->assertSame(1.0, Cast::toFloat(1));
Expand All @@ -73,17 +100,33 @@ public function testToFloat(): void
$this->assertSame(PHP_INT_MAX + 1, Cast::toFloat(PHP_INT_MAX + 1));
$this->assertSame(-9223372036854776833, Cast::toFloat(-9223372036854776833));

$this->assertTypeError(fn () => Cast::toFloat(''));
$this->assertTypeError(fn () => Cast::toFloat('a'));
$this->assertTypeError(fn () => Cast::toFloat('ok'));
$this->assertTypeError(fn () => Cast::toFloat('true'));
$this->assertTypeError(fn () => Cast::toFloat('null'));
$this->assertTypeError(fn () => Cast::toFloat(null));
$this->assertTypeError(fn () => Cast::toFloat([]));
$this->assertTypeError(fn () => Cast::toFloat((object)[]));
$this->assertTypeError(function () {
return Cast::toFloat('');
});
$this->assertTypeError(function () {
return Cast::toFloat('a');
});
$this->assertTypeError(function () {
return Cast::toFloat('ok');
});
$this->assertTypeError(function () {
return Cast::toFloat('true');
});
$this->assertTypeError(function () {
return Cast::toFloat('null');
});
$this->assertTypeError(function () {
return Cast::toFloat(null);
});
$this->assertTypeError(function () {
return Cast::toFloat([]);
});
$this->assertTypeError(function () {
return Cast::toFloat((object)[]);
});
}

public function testToString(): void
public function testToString()
{
$this->assertSame('0', Cast::toString(0));
$this->assertSame('1', Cast::toString(1));
Expand All @@ -106,12 +149,18 @@ public function __toString()
)
);

$this->assertTypeError(fn () => Cast::toString(null));
$this->assertTypeError(fn () => Cast::toString([]));
$this->assertTypeError(fn () => Cast::toString((object)[]));
$this->assertTypeError(function () {
return Cast::toString(null);
});
$this->assertTypeError(function () {
return Cast::toString([]);
});
$this->assertTypeError(function () {
return Cast::toString((object)[]);
});
}

public function testToBool(): void
public function testToBool()
{
$this->assertFalse(Cast::toBool(0));
$this->assertTrue(Cast::toBool(1));
Expand All @@ -130,12 +179,18 @@ public function testToBool(): void
$this->assertTrue(Cast::toBool('true'));
$this->assertTrue(Cast::toBool('null'));

$this->assertTypeError(fn () => Cast::toBool(null));
$this->assertTypeError(fn () => Cast::toBool([]));
$this->assertTypeError(fn () => Cast::toBool((object)[]));
$this->assertTypeError(function () {
return Cast::toBool(null);
});
$this->assertTypeError(function () {
return Cast::toBool([]);
});
$this->assertTypeError(function () {
return Cast::toBool((object)[]);
});
}

private function assertTypeError(callable $func): void
private function assertTypeError(callable $func)
{
try {
$func();
Expand Down
Loading

0 comments on commit 93794a9

Please sign in to comment.