Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mpyw committed Jun 23, 2021
0 parents commit 76b6838
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
coverage_clover: build/logs/clover.xml
json_path: build/logs/coveralls-upload.json
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
/vendor/
.phpunit.result.cache
28 changes: 28 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
checks:
php:
code_rating: true

filter:
excluded_paths:
- tests/*
- vendor/*

build:

environment:
php: '7.4'

dependencies:
before:
- composer require phpunit/phpunit --dev --no-update
- composer update --prefer-stable
- composer show | grep -E '^(phpunit/phpunit|sebastian/comparator)'
- mkdir -p build/logs

tests:
override:
-
command: 'phpdbg -qrr vendor/bin/phpunit --coverage-clover build/logs/clover.xml'
coverage:
file: 'build/logs/clover.xml'
format: 'clover'
67 changes: 67 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
language: php

matrix:
include:
- dist: precise
php: 5.3
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest" DISABLE_TLS="1"
- dist: precise
php: 5.3
env: COMPOSER_FLAGS="--prefer-stable" DISABLE_TLS="1"
- dist: trusty
php: 5.4
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- dist: trusty
php: 5.4
env: COMPOSER_FLAGS="--prefer-stable"
- dist: trusty
php: 5.5
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- dist: trusty
php: 5.5
env: COMPOSER_FLAGS="--prefer-stable"
- php: 5.6.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 5.6
env: COMPOSER_FLAGS="--prefer-stable"
- php: 7.0.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.0
env: COMPOSER_FLAGS="--prefer-stable"
- php: 7.1.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.1
env: COMPOSER_FLAGS="--prefer-stable"
- php: 7.2.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.2
env: COMPOSER_FLAGS="--prefer-stable"
- php: 7.3.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.3
env: COMPOSER_FLAGS="--prefer-stable"
- php: 7.4.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 7.4
env: COMPOSER_FLAGS="--prefer-stable"
- php: 8.0.0
env: COMPOSER_FLAGS="--prefer-stable --prefer-lowest"
- php: 8.0
env: COMPOSER_FLAGS="--prefer-stable"

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

before_script:
- '[ $DISABLE_TLS -eq 1 ] && composer config --global -- disable-tls true || :'
- '[ $DISABLE_TLS -eq 1 ] && composer config --global -- secure-http false || :'
- composer self-update
- composer require phpunit/phpunit --dev --no-update $COMPOSER_FLAGS
- composer update $COMPOSER_FLAGS
- composer show | grep -E '^(phpunit/phpunit|sebastian/comparator)'
- composer json:polyfill

script:
- vendor/bin/phpunit
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) 2021 mpyw

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.
58 changes: 58 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# PHPUnit Patch Serializable Comparison [![Build Status](https://travis-ci.com/mpyw/phpunit-patch-serializable-comparison.svg?branch=master)](https://travis-ci.com/mpyw/phpunit-patch-serializable-comparison)

Fixes `assertSame()`/`assertEquals()` serialization errors running in separate processes.

## Requirements

- php: `>=5.3.3`
- [phpunit/phpunit](https://github.com/sebastianbergmann/phpunit): `>=4.8.0`
- [sebastianbergmann/comparator](https://github.com/sebastianbergmann/comparator): `^1.0 || ^2.0 || ^3.0 || ^4.0`

## Installing

```bash
composer require --dev mpyw/phpunit-patch-serializable-comparison
```

## Example

```php
class AssertionTest extends TestCase
{
protected function callAssertSameInClosure(\Closure $closure)
{
static::assertSame('aaa', 'bbb');
}

/**
* @runInSeparateProcess
* @preserveGlobalState disabled
*/
public function testAssertionIncludingUnserializableTrace()
{
static::callAssertSameInClosure(function () {});
}
}
```

### Before Patching

```
PHPUnit\Framework\Exception: PHP Fatal error: Uncaught Exception: Serialization of 'Closure' is not allowed in Standard input code:XX
Stack trace:
#0 Standard input code(XX): serialize(Array)
#1 Standard input code(XX): __phpunit_run_isolated_test()
#2 {main}
thrown in Standard input code on line XX
```

### After Patching

```diff
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-'aaa'
+'bbb'
```
31 changes: 31 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "mpyw/phpunit-patch-serializable-comparison",
"description": "Fixes assertSame/assertEquals serialization errors running in separate processes.",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "mpyw",
"email": "ryosuke_i_628@yahoo.co.jp"
}
],
"keywords": ["phpunit", "isolation", "serialization", "separate", "process", "processes", "patch", "fix", "bug"],
"autoload": {
"files": [
"./files/ComparisonFailure.php"
]
},
"autoload-dev": {
"psr-4": {
"Mpyw\\PHPUnitPatchSerializableComparison\\Tests\\": "tests/"
}
},
"require": {
"php": ">=5.3.3",
"sebastian/comparator": "^1.0 || ^2.0 || ^3.0 || ^4.0"
},
"scripts": {
"json:restore": "git checkout composer.json",
"json:polyfill": "./polyfill && composer dump-autoload"
}
}
138 changes: 138 additions & 0 deletions files/ComparisonFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?php

namespace SebastianBergmann\Comparator;

use RuntimeException;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;

/**
* Thrown when an assertion for string equality failed.
*/
final class ComparisonFailure extends RuntimeException
{
/**
* Expected value of the retrieval which does not match $actual.
*
* @var mixed
*/
protected $expected;

/**
* Actually retrieved value which does not match $expected.
*
* @var mixed
*/
protected $actual;

/**
* The string representation of the expected value.
*
* @var string
*/
protected $expectedAsString;

/**
* The string representation of the actual value.
*
* @var string
*/
protected $actualAsString;

/**
* @var bool
*/
protected $identical;

/**
* Optional message which is placed in front of the first line
* returned by toString().
*
* @var string
*/
protected $message;

/**
* Initialises with the expected value and the actual value.
*
* @param mixed $expected expected value retrieved
* @param mixed $actual actual value retrieved
* @param string $expectedAsString
* @param string $actualAsString
* @param bool $identical
* @param string $message a string which is prefixed on all returned lines
* in the difference output
*
* @noinspection PhpMissingParentConstructorInspection
*/
public function __construct($expected, $actual, $expectedAsString, $actualAsString, $identical = false, $message = '')
{
$this->expected = $expected;
$this->actual = $actual;
$this->expectedAsString = $expectedAsString;
$this->actualAsString = $actualAsString;
$this->message = $message;
}

public function getActual()
{
return $this->actual;
}

public function getExpected()
{
return $this->expected;
}

/**
* @return string
*/
public function getActualAsString()
{
return $this->actualAsString;
}

/**
* @return string
*/
public function getExpectedAsString()
{
return $this->expectedAsString;
}

/**
* @return string
*/
public function getDiff()
{
if (!$this->actualAsString && !$this->expectedAsString) {
return '';
}

$header = "\n--- Expected\n+++ Actual\n";
if (\class_exists('SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder')) {
$header = new UnifiedDiffOutputBuilder($header);
}
$differ = new Differ($header);

return $differ->diff($this->expectedAsString, $this->actualAsString);
}

/**
* @return string
*/
public function toString()
{
return $this->message . $this->getDiff();
}

/**
* Make the class serializable.
*
* @return string[]
*/
public function __sleep()
{
return \array_keys(\get_object_vars($this));
}
}
22 changes: 22 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
bootstrap="vendor/autoload.php"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
>
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix="TestTest.php">./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">classmap</directory>
</whitelist>
</filter>
</phpunit>
12 changes: 12 additions & 0 deletions polyfill
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env php
<?php

require __DIR__ . '/vendor/autoload.php';

if (class_exists('PHPUnit_Framework_TestCase') && !class_exists('PHPUnit\Framework\TestCase')) {
fwrite(STDERR, "Editing composer.json");

$json = \json_decode(\file_get_contents($filename = __DIR__ . '/composer.json'), true);
$json['autoload-dev']['files'][] = './test-polyfill/TestCase.php';
\file_put_contents($filename, \json_encode($json));
}
Loading

0 comments on commit 76b6838

Please sign in to comment.