Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop support for PHP 7.2 and PHPUnit 8.x/9.0 #16

Merged
merged 2 commits into from
Apr 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ inherit: true
build:
environment:
php:
version: 7.2
version: 7.4
tests:
override:
- make test
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
language: php
matrix:
include:
- php: 7.2
env: EXECUTE_DEPLOYMENT=true
- php: 7.3
env: deps=low
env: EXECUTE_DEPLOYMENT=true
- php: 7.4
- php: 7.4
env: deps=low
fast_finish: true
before_install:
- phpenv config-rm xdebug.ini || echo "XDebug is not enabled"
Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ package: tools/box

cd build/phar && \
composer remove phpunit/phpunit --no-update && \
composer config platform.php 7.2 && \
composer config platform.php 7.3 && \
composer update --no-dev -o -a

tools/box compile
Expand All @@ -97,16 +97,16 @@ tools/deptrac:
curl -Ls http://get.sensiolabs.de/deptrac.phar -o tools/deptrac && chmod +x tools/deptrac

tools/infection: tools/infection.pubkey
curl -Ls https://github.com/infection/infection/releases/download/0.13.2/infection.phar -o tools/infection && chmod +x tools/infection
curl -Ls https://github.com/infection/infection/releases/download/0.16.1/infection.phar -o tools/infection && chmod +x tools/infection

tools/infection.pubkey:
curl -Ls https://github.com/infection/infection/releases/download/0.13.2/infection.phar.pubkey -o tools/infection.pubkey
curl -Ls https://github.com/infection/infection/releases/download/0.16.1/infection.phar.pubkey -o tools/infection.pubkey

tools/box:
curl -Ls https://github.com/humbug/box/releases/download/3.8.4/box.phar -o tools/box && chmod +x tools/box

tests/phar/tools/phpunit:
curl -Ls https://phar.phpunit.de/phpunit-8.phar -o tests/phar/tools/phpunit && chmod +x tests/phar/tools/phpunit
curl -Ls https://phar.phpunit.de/phpunit-9.phar -o tests/phar/tools/phpunit && chmod +x tests/phar/tools/phpunit

tests/phar/tools/phpunit.d/zalas-phpunit-doubles-extension.phar: build/zalas-phpunit-doubles-extension.phar
cp build/zalas-phpunit-doubles-extension.phar tests/phar/tools/phpunit.d/zalas-phpunit-doubles-extension.phar
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Remember to instruct PHPUnit to load extensions in your `phpunit.xml`:

## Usage

Include the `Zalas\PHPUnit\Doubles\TestCase\TestDoubles` trait to have your test doubles initialised
in one of the supported test doubling frameworks.
Include the `Zalas\PHPUnit\Doubles\TestCase\ProphecyTestDoubles` or `Zalas\PHPUnit\Doubles\TestCase\PHPUnitTestDoubles`
trait to have your test doubles initialised in one of the supported test doubling frameworks.

Both the type of test double and the kind of test doubling framework are taken from the property type:

Expand All @@ -54,11 +54,11 @@ Currently, two test doubling frameworks are supported:

use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Zalas\PHPUnit\Doubles\TestCase\TestDoubles;
use Zalas\PHPUnit\Doubles\TestCase\ProphecyTestDoubles;

class DiscworldTest extends TestCase
{
use TestDoubles;
use ProphecyTestDoubles;

/**
* @var Vimes|ObjectProphecy
Expand Down Expand Up @@ -89,11 +89,11 @@ class DiscworldTest extends TestCase

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Zalas\PHPUnit\Doubles\TestCase\TestDoubles;
use Zalas\PHPUnit\Doubles\TestCase\PHPUnitTestDoubles;

class DiscworldTest extends TestCase
{
use TestDoubles;
use PHPUnitTestDoubles;

/**
* @var Vimes|MockObject
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
"description": "Initialises test doubles in PHPUnit test cases for you",
"type": "library",
"require": {
"php": "^7.2,<8.0",
"phpunit/phpunit": "^8.5 || ^9.0",
"php": "^7.3,<8.0",
"phpunit/phpunit": "^9.1",
"phpdocumentor/reflection-docblock": "^4.0.1"
},
"require-dev": {
"phpspec/prophecy-phpunit": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 2 additions & 1 deletion infection.json.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"@default": true,
"ArrayItemRemoval": {
"ignore": [
"Zalas\\PHPUnit\\Doubles\\TestCase\\TestDoubles::initialiseTestDoubles"
"Zalas\\PHPUnit\\Doubles\\TestCase\\PHPUnitTestDoubles::initialisePHPUnitTestDoubles",
"Zalas\\PHPUnit\\Doubles\\TestCase\\ProphecyTestDoubles::initialiseProphecyTestDoubles"
]
},
"IdenticalEqual": false,
Expand Down
4 changes: 2 additions & 2 deletions manifest.xml.in
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" ?>
<phar xmlns="https://phar.io/xml/manifest/1.0">
<contains name="zalas/phpunit-doubles" version="@@version@@" type="extension">
<extension for="phpunit/phpunit" compatible="^8.0"/>
<extension for="phpunit/phpunit" compatible="^9.1"/>
</contains>

<copyright>
Expand All @@ -10,6 +10,6 @@
</copyright>

<requires>
<php version="^7.2"/>
<php version="^7.3"/>
</requires>
</phar>
43 changes: 15 additions & 28 deletions src/TestCase/Doubler.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,21 @@ class Doubler
private $injector;

/**
* @var array
* @var callable
*/
private $doubleFactories;
private $doubleFactory;

public function __construct(Extractor $extractor, Injector $injector, array $doubleFactories)
/**
* @var string
*/
private $doubleType;

public function __construct(Extractor $extractor, Injector $injector, callable $doubleFactory, string $doubleType)
{
$this->extractor = $extractor;
$this->injector = $injector;
$this->doubleFactories = $doubleFactories;
$this->doubleFactory = $doubleFactory;
$this->doubleType = $doubleType;
}

public function createDoubles(/*object */$testCase)
Expand All @@ -43,11 +49,8 @@ public function createDoubles(/*object */$testCase)

private function createTestDouble(Property $property)
{
$doubleType = $this->getDoubleType($property);
$allDoubleTypes = \array_keys($this->doubleFactories);

return $this->doubleFactories[$doubleType]($property->getTypesFiltered(function (string $type) use ($allDoubleTypes): bool {
return !\in_array($type, $allDoubleTypes);
return ($this->doubleFactory)($property->getTypesFiltered(function (string $type): bool {
return $type !== $this->doubleType;
}));
}

Expand All @@ -58,28 +61,12 @@ private function createTestDouble(Property $property)
*/
private function getTestDoubleProperties(/*object */$testCase): array
{
$supportedDoubles = \array_keys($this->doubleFactories);

return $this->extractor->extract($testCase, function (Property $property) use ($supportedDoubles): bool {
$doubleTypes = $property->getTypesFiltered(function (string $type) use ($supportedDoubles): bool {
return \in_array($type, $supportedDoubles);
return $this->extractor->extract($testCase, function (Property $property): bool {
$doubleTypes = $property->getTypesFiltered(function (string $type): bool {
return $type === $this->doubleType;
});

return \count($doubleTypes) > 0;
});
}

private function getDoubleType(Property $property): string
{
$supportedDoubles = \array_keys($this->doubleFactories);
$doubleTypes = $property->getTypesFiltered(function (string $type) use ($supportedDoubles): bool {
return \in_array($type, $supportedDoubles);
});

if (\count($doubleTypes) > 1) {
throw new \LogicException(\sprintf('Ambiguous test double definition for "%s": "%s".', $property->getName(), \implode('|', $property->getTypes())));
}

return \array_shift($doubleTypes);
}
}
45 changes: 45 additions & 0 deletions src/TestCase/PHPUnitTestDoubles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);

namespace Zalas\PHPUnit\Doubles\TestCase;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Zalas\PHPUnit\Doubles\Injector\PropertyAccessInjector;
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor;

trait PHPUnitTestDoubles
{
abstract public function getMockBuilder(string $className): MockBuilder;

/**
* @before
*/
protected function initialisePHPUnitTestDoubles(): void
{
$doubler = new Doubler(
new ReflectionExtractor([TestCase::class, Assert::class]),
new PropertyAccessInjector(),
function (array $types) {
return $this->createTestDoubleWithPhpunit($types);
},
MockObject::class
);
$doubler->createDoubles($this);
}

private function createTestDoubleWithPhpunit(array $types): MockObject
{
$normalisedTypes = \array_shift($types) ?? \stdClass::class;

return $this->getMockBuilder($normalisedTypes)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disableProxyingToOriginalMethods()
->disallowMockingUnknownTypes()
->getMock();
}
}
45 changes: 45 additions & 0 deletions src/TestCase/ProphecyTestDoubles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php declare(strict_types=1);

namespace Zalas\PHPUnit\Doubles\TestCase;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Zalas\PHPUnit\Doubles\Injector\PropertyAccessInjector;
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor;

trait ProphecyTestDoubles
{
abstract protected function prophesize(?string $classOrInterface = null): ObjectProphecy;

/**
* @before
*/
protected function initialiseProphecyTestDoubles(): void
{
$doubler = new Doubler(
new ReflectionExtractor([TestCase::class, Assert::class]),
new PropertyAccessInjector(),
function (array $types) {
return $this->createTestDoubleWithProphecy($types);
},
ObjectProphecy::class
);
$doubler->createDoubles($this);
}

private function createTestDoubleWithProphecy(array $types): ObjectProphecy
{
$prophecy = $this->prophesize(\array_shift($types));

foreach ($types as $type) {
if (\interface_exists($type)) {
$prophecy->willImplement($type);
} else {
$prophecy->willExtend($type);
}
}

return $prophecy;
}
}
64 changes: 5 additions & 59 deletions src/TestCase/TestDoubles.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,11 @@

namespace Zalas\PHPUnit\Doubles\TestCase;

use PHPUnit\Framework\Assert;
use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Prophecy\Prophecy\ObjectProphecy;
use Zalas\PHPUnit\Doubles\Injector\PropertyAccessInjector;
use Zalas\PHPUnit\Doubles\PhpDocumentor\ReflectionExtractor;

/**
* @deprecated use one of the replacement traits: ProphecyTestDoubles, PHPUnitTestDoubles.
*/
trait TestDoubles
{
abstract public function getMockBuilder(string $className): MockBuilder;

abstract protected function prophesize(?string $classOrInterface = null): ObjectProphecy;

/**
* @before
*/
protected function initialiseTestDoubles(): void
{
$doubler = new Doubler(
new ReflectionExtractor([TestCase::class, Assert::class]),
new PropertyAccessInjector(),
[
ObjectProphecy::class => function (array $types) {
return $this->createTestDoubleWithProphecy($types);
},
MockObject::class => function (array $types) {
return $this->createTestDoubleWithPhpunit($types);
},
]
);
$doubler->createDoubles($this);
}

private function createTestDoubleWithProphecy(array $types): ObjectProphecy
{
$prophecy = $this->prophesize(\array_shift($types));

foreach ($types as $type) {
if (\interface_exists($type)) {
$prophecy->willImplement($type);
} else {
$prophecy->willExtend($type);
}
}

return $prophecy;
}

private function createTestDoubleWithPhpunit(array $types): MockObject
{
$normalisedTypes = \array_shift($types) ?? \stdClass::class;

return $this->getMockBuilder($normalisedTypes)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disableProxyingToOriginalMethods()
->disallowMockingUnknownTypes()
->getMock();
}
use ProphecyTestDoubles;
use PHPUnitTestDoubles;
}

This file was deleted.

Loading