Skip to content

Commit

Permalink
Merge pull request #6 from adamnicholson/feature/php8
Browse files Browse the repository at this point in the history
Add PHP8 Support
  • Loading branch information
adamnicholson authored Jan 23, 2023
2 parents 1af396b + 9d2cebc commit a225588
Show file tree
Hide file tree
Showing 16 changed files with 128 additions and 59 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: PHPUnit Tests
on: [push, pull_request]
jobs:
build:
strategy:
matrix:
operating-system: [ubuntu-latest, macos-latest]
php-versions: ['8.0', '8.1']
runs-on: ${{ matrix.operating-system }}
steps:
- name: Checkout
uses: actions/checkout@v3

# Docs: https://github.com/shivammathur/setup-php
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
extensions: mbstring, simplexml, dom
coverage: xdebug

- name: Get composer cache directory
id: composer-cache
run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT

- name: Cache composer dependencies
uses: actions/cache@v3
with:
path: ${{ steps.composer-cache.outputs.dir }}
# Use composer.json for key, if composer.lock is not committed.
# key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

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

- name: Test with phpunit
run: vendor/bin/phpunit --coverage-text
5 changes: 2 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: php
php:
- 7.0
- 5.6
- hhvm
- 8.1
- 8.0
install: composer install --no-interaction --prefer-source

script:
Expand Down
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@
}
],
"require": {
"php": ">=5.4.0",
"psr/cache": "^1.0"
"php": ">=8.0",
"psr/cache": "^1.0 || ^2.0 || 3.0"
},
"require-dev": {
"psr/log": "*@dev",
"illuminate/container": "*@dev",
"illuminate/events": "*@dev",
"league/container": "~1.3",
"phpunit/phpunit": "^5.2"
"phpunit/phpunit": "^9.5",
"phpspec/prophecy": "^1.16",
"phpspec/prophecy-phpunit": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion tests/Bridge/Laravel/IlluminateContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IlluminateContainerTest extends ChiefTestCase
{
public function testMakeHitsInnerContainer()
{
$container = new IlluminateContainer($inner = $this->getMock('Illuminate\Container\Container'));
$container = new IlluminateContainer($inner = $this->createMock('Illuminate\Container\Container'));
$container->expects($this->once())->method('make')->with('stdClass')->willReturn(new \stdClass());
$made = $container->make('stdClass');
$this->assertTrue($made instanceof \stdClass);
Expand Down
2 changes: 1 addition & 1 deletion tests/Bridge/Laravel/IlluminateEventDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class IlluminateEventDispatcherTest extends ChiefTestCase
{
public function testDispatchHitsDispatcher()
{
$instance = new IlluminateEventDispatcher($dispatcher = $this->getMock('Illuminate\Events\Dispatcher'));
$instance = new IlluminateEventDispatcher($dispatcher = $this->createMock('Illuminate\Events\Dispatcher'));
$eventName = 'Foo.Event';
$eventdata = new TestCommand();
$dispatcher->expects($this->once())->method('fire')->with($eventName, $eventdata);
Expand Down
2 changes: 1 addition & 1 deletion tests/Bridge/League/LeagueContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class LeagueContainerTest extends ChiefTestCase
{
public function testMakeHitsInnerContainer()
{
$container = new LeagueContainer($inner = $this->getMock('League\Container\Container'));
$container = new LeagueContainer($inner = $this->createMock('League\Container\Container'));
$container->expects($this->once())->method('get')->with('stdClass')->willReturn(new \stdClass());
$made = $container->make('stdClass');
$this->assertTrue($made instanceof \stdClass);
Expand Down
8 changes: 4 additions & 4 deletions tests/Busses/SynchronousCommandBusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function testInstance()

public function testExecuteFiresHandlerProvidedByResolver()
{
$resolver = $this->getMock('Chief\CommandHandlerResolver');
$handler = $this->getMock('Chief\CommandHandler');
$resolver = $this->createMock('Chief\CommandHandlerResolver');
$handler = $this->createMock('Chief\CommandHandler');
$bus = new SynchronousCommandBus($resolver);
$command = new TestCommand;
$handler->expects($this->once())->method('handle')->with($command);
Expand All @@ -26,8 +26,8 @@ public function testExecuteFiresHandlerProvidedByResolver()

public function testExecuteReturnsHandlerResponse()
{
$resolver = $this->getMock('Chief\CommandHandlerResolver');
$handler = $this->getMock('Chief\CommandHandler');
$resolver = $this->createMock('Chief\CommandHandlerResolver');
$handler = $this->createMock('Chief\CommandHandler');
$bus = new SynchronousCommandBus($resolver);
$command = new TestCommand;
$handler->expects($this->once())->method('handle')->with($command)->willReturn('Foo-Bar.');
Expand Down
20 changes: 10 additions & 10 deletions tests/ChiefTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function testExecuteFiresByAutoResolution()
public function testExecuteFiresHandlerAttachedByInstance()
{
$resolver = new NativeCommandHandlerResolver;
$resolver->bindHandler('Chief\Stubs\TestCommand', $handler = $this->getMock('Chief\CommandHandler'));
$resolver->bindHandler('Chief\Stubs\TestCommand', $handler = $this->createMock('Chief\CommandHandler'));
$syncBus = new SynchronousCommandBus($resolver);
$bus = new Chief($syncBus);
$command = new TestCommand;
Expand Down Expand Up @@ -63,23 +63,23 @@ public function testExecuteThrowsExceptionWhenNoHandler()
{
$bus = new Chief();
$command = new TestCommandWithoutHandler;
$this->setExpectedException('Exception');
$this->expectException('Exception');
$bus->execute($command);
}

public function testCommandCanHandleItselfIfImplementsCommandHandler()
{
$bus = new Chief();
$command = $this->getMock('Chief\Stubs\SelfHandlingCommand');
$command = $this->createMock('Chief\Stubs\SelfHandlingCommand');
$command->expects($this->once())->method('handle')->with($command);
$bus->execute($command);
}

public function testDecoratorCommandBus()
{
$bus = new LogDecoratorCommandBus(
$logger = $this->getMock('Psr\Log\LoggerInterface'),
$innerBus = $this->getMock('Chief\Busses\SynchronousCommandBus')
$logger = $this->createMock('Psr\Log\LoggerInterface'),
$innerBus = $this->createMock('Chief\Busses\SynchronousCommandBus')
);
$chief = new Chief($bus);
$command = new TestCommand;
Expand All @@ -91,7 +91,7 @@ public function testDecoratorCommandBus()
public function testInstanceWithDecorators()
{
$chief = new Chief(new SynchronousCommandBus, [
$decorator = $this->getMock('Chief\Decorator')
$decorator = $this->createMock('Chief\Decorator')
]);
$command = new TestCommand;
$decorator->expects($this->once())->method('execute')->with($command);
Expand All @@ -100,11 +100,11 @@ public function testInstanceWithDecorators()

public function testInstanceWithMultipleDecoratorsHitsNestedDecorators()
{
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger = $this->createMock('Psr\Log\LoggerInterface');

$chief = new Chief(new SynchronousCommandBus, [
$decoratorOne = new LoggingDecorator($logger),
$decoratorTwo = $this->getMock('Chief\Decorator'),
$decoratorTwo = $this->createMock('Chief\Decorator'),
]);
$command = new TestCommand;
$decoratorTwo->expects($this->once())->method('execute')->with($command);
Expand All @@ -113,7 +113,7 @@ public function testInstanceWithMultipleDecoratorsHitsNestedDecorators()

public function testInstanceWithMultipleDecoratorsHitsHandler()
{
$logger = $this->getMock('Psr\Log\LoggerInterface');
$logger = $this->createMock('Psr\Log\LoggerInterface');

$chief = new Chief(new SynchronousCommandBus, [
$decoratorOne = new LoggingDecorator($logger),
Expand All @@ -125,7 +125,7 @@ public function testInstanceWithMultipleDecoratorsHitsHandler()
}
public function testInnerBusResponseIsReturnedByChief()
{
$chief = new Chief($bus = $this->getMock('Chief\CommandBus'));
$chief = new Chief($bus = $this->createMock('Chief\CommandBus'));
$bus->expects($this->once())->method('execute')->willReturn('foo-bar');
$response = $chief->execute(new TestCommand);
$this->assertEquals($response, 'foo-bar');
Expand Down
7 changes: 5 additions & 2 deletions tests/ChiefTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Chief;

abstract class ChiefTestCase extends \PHPUnit_Framework_TestCase
{
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

abstract class ChiefTestCase extends TestCase
{
use ProphecyTrait;
}
54 changes: 40 additions & 14 deletions tests/Decorators/CachingDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ public function test_execution_return_value_is_cached_when_command_implements_ca
$inner = $this->prophesize(CommandBus::class);
$decorator->setInnerBus($inner->reveal());

$command = $this->prophesize(CacheableCommand::class)->reveal();
$command = new FakeCachableCommand('fizzbuzz');

$notCachedItem = $this->prophesize(CacheItemInterface::class);
$notCachedItem->isHit()->willReturn(false);
$notCachedItem->getKey()->willReturn(md5(serialize(($command))));
$this->cache->getItem(Argument::any())->willReturn($notCachedItem->reveal());
$cacheKey = md5(serialize(($command)));
$notCachedItem->getKey()->willReturn($cacheKey);
$this->cache->getItem($cacheKey)->willReturn($notCachedItem->reveal());

$notCachedItem->set(7)->shouldBeCalled()->willReturn($notCachedItem->reveal());
$notCachedItem->expiresAfter(3600)->shouldBeCalled()->willReturn($notCachedItem->reveal());
Expand All @@ -75,11 +76,7 @@ public function test_cache_expiry_can_be_overridden()
$inner = $this->prophesize(CommandBus::class);
$decorator->setInnerBus($inner->reveal());

$commandProphecy = $this->prophesize(HasCacheOptions::class);
$commandProphecy->getCacheExpiry()->willReturn(60*60*24*365);
$commandProphecy->getCacheKey()->willReturn(null);

$command = $commandProphecy->reveal();
$command = new FakeCachableCommandWithCacheOptions('fizzbuzz', 60*60*24*365, null);

$notCachedItem = $this->prophesize(CacheItemInterface::class);
$notCachedItem->isHit()->willReturn(false);
Expand All @@ -105,11 +102,7 @@ public function test_cache_key_can_be_overridden()
$inner = $this->prophesize(CommandBus::class);
$decorator->setInnerBus($inner->reveal());

$commandProphecy = $this->prophesize(HasCacheOptions::class);
$commandProphecy->getCacheExpiry()->willReturn(null);
$commandProphecy->getCacheKey()->willReturn('custom-cache-key');

$command = $commandProphecy->reveal();
$command = new FakeCachableCommandWithCacheOptions('fizzbuzz', null, 'custom-cache-key');

$notCachedItem = $this->prophesize(CacheItemInterface::class);
$notCachedItem->isHit()->willReturn(false);
Expand All @@ -135,7 +128,7 @@ public function test_cache_item_value_is_returned_if_cached()
$inner = $this->prophesize(CommandBus::class);
$decorator->setInnerBus($inner->reveal());

$command = $this->prophesize(CacheableCommand::class)->reveal();
$command = new FakeCachableCommand('fizzbuzz');

$cachedItem = $this->prophesize(CacheItemInterface::class);
$cachedItem->isHit()->willReturn(true);
Expand All @@ -157,3 +150,36 @@ protected function getDecorator()
return new CachingDecorator($this->cache->reveal());
}
}

class FakeCachableCommand implements CacheableCommand
{
public $data;

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

class FakeCachableCommandWithCacheOptions extends FakeCachableCommand implements HasCacheOptions
{
private $expiry;
private $cacheKey;

public function __construct($data, $expiry, $cacheKey)
{
$this->data = $data;
$this->expiry = $expiry;
$this->cacheKey = $cacheKey;
}

public function getCacheExpiry()
{
return $this->expiry;
}

public function getCacheKey()
{
return $this->cacheKey;
}
}
10 changes: 5 additions & 5 deletions tests/Decorators/CommandQueueingDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ class CommandQueueingDecoratorTest extends DecoratorTest
{
public function testInstance()
{
$this->assertTrue(new CommandQueueingDecorator($this->getMock('Chief\CommandQueuer')) instanceof CommandBus);
$this->assertTrue(new CommandQueueingDecorator($this->createMock('Chief\CommandQueuer')) instanceof CommandBus);
}

public function testExecutePutsNormalCommandInInnerBus()
{
$queuer = $this->getMock('Chief\CommandQueuer');
$innerBus = $this->getMock('Chief\CommandBus');
$queuer = $this->createMock('Chief\CommandQueuer');
$innerBus = $this->createMock('Chief\CommandBus');
$bus = new CommandQueueingDecorator($queuer, $innerBus);
$command = new TestCommand;
$queuer->expects($this->never())->method('queue');
Expand All @@ -27,7 +27,7 @@ public function testExecutePutsNormalCommandInInnerBus()

public function testExecutePutsQueueableCommandInQueuer()
{
$queuer = $this->getMock('Chief\CommandQueuer');
$queuer = $this->createMock('Chief\CommandQueuer');
$bus = new CommandQueueingDecorator($queuer);
$command = new TestQueueableCommand;
$queuer->expects($this->once())->method('queue')->with($command);
Expand All @@ -39,7 +39,7 @@ public function testExecutePutsQueueableCommandInQueuer()
*/
protected function getDecorator()
{
return new CommandQueueingDecorator($this->getMock('Chief\CommandQueuer'));
return new CommandQueueingDecorator($this->createMock('Chief\CommandQueuer'));
}


Expand Down
2 changes: 1 addition & 1 deletion tests/Decorators/DecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class DecoratorTest extends ChiefTestCase
public function testExecuteFiresInnerBusAndReturnsResponse()
{
$decorator = $this->getDecorator();
$decorator->setInnerBus($bus = $this->getMock('Chief\CommandBus'));
$decorator->setInnerBus($bus = $this->createMock('Chief\CommandBus'));
$command = new TestCommand();
$bus->expects($this->once())->method('execute')->with($command)->willReturn('Some response');
$response = $decorator->execute($command);
Expand Down
8 changes: 4 additions & 4 deletions tests/Decorators/EventDispatchingDecoratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ class EventDispatchingDecoratorTest extends DecoratorTest
public function testInstance()
{
$decorator = $this->getDecorator();
$decorator->setInnerBus($this->getMock('Chief\CommandBus'));
$decorator->setInnerBus($this->createMock('Chief\CommandBus'));
$this->assertTrue($decorator instanceof CommandBus);
}

public function testExecuteFiresEventAndInnerBus()
{
$decorator = new EventDispatchingDecorator(
$dispatcher = $this->getMock('Chief\Decorators\EventDispatcher')
$dispatcher = $this->createMock('Chief\Decorators\EventDispatcher')
);
$decorator->setInnerBus($bus = $this->getMock('Chief\CommandBus'));
$decorator->setInnerBus($bus = $this->createMock('Chief\CommandBus'));
$command = new TestCommand();
$bus->expects($this->once())->method('execute')->with($command);
$dispatcher->expects($this->once())->method('dispatch')->with('Chief.Stubs.TestCommand', [$command]);
Expand All @@ -28,6 +28,6 @@ public function testExecuteFiresEventAndInnerBus()

protected function getDecorator()
{
return new EventDispatchingDecorator($this->getMock('Chief\Decorators\EventDispatcher'));
return new EventDispatchingDecorator($this->createMock('Chief\Decorators\EventDispatcher'));
}
}
Loading

0 comments on commit a225588

Please sign in to comment.