Skip to content

Commit

Permalink
non-working version
Browse files Browse the repository at this point in the history
  • Loading branch information
Nacoma committed Jun 19, 2021
1 parent 8e1d289 commit baff0a7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
14 changes: 14 additions & 0 deletions app/FooDecorator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App;

class FooDecorator implements FooContract
{
public function __construct(private FooContract $parent)
{}

public function bar(): string
{
return strrev($this->parent->bar());
}
}
9 changes: 8 additions & 1 deletion app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Foo;
use App\FooContract;
use App\FooDecorator;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
Expand All @@ -15,7 +16,13 @@ class AppServiceProvider extends ServiceProvider
*/
public function register()
{
$this->app->bind(FooContract::class, Foo::class);
$this->app->bind(FooContract::class, fn () => new Foo('any'));

$decorator = new FooDecorator(
$this->app->make(FooContract::class)
);

$this->app->instance(FooContract::class, $decorator);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
"php": "^7.3|^8.0",
"php": "8.0",
"fideloper/proxy": "^4.4",
"fruitcake/laravel-cors": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
Expand Down
26 changes: 4 additions & 22 deletions tests/Unit/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,17 @@

class ExampleTest extends TestCase
{
public function testFoo1()
{
// Works as expected
$result = resolve(FooContract::class, ['baz' => 'baz'])->bar(); // "qux"

$this->assertEquals('qux', $result);
}

public function testFoo2()
{
// Would expect to get "corge"...
$this->mock(FooContract::class, function (MockInterface $mock) {
$mock->shouldReceive('bar')->once()->andReturn('corge');
});

$result = resolve(FooContract::class, ['baz' => 'baz'])->bar(); // ... but still getting "qux"
$result = resolve(FooContract::class, ['baz' => 'baz'])->bar();

$this->assertEquals('corge', $result);
$this->assertEquals('xuq', $result);
}

public function testFoo3()
{
$this->mock(FooContract::class, function (MockInterface $mock) {
$mock->shouldReceive('bar')->once()->andReturn('corge');
});

// I just remove the constructor arguments...
$result = resolve(FooContract::class)->bar(); // ... and I now get "corge" as expected
$result = resolve(FooContract::class)->bar();

$this->assertEquals('corge', $result);
$this->assertEquals('xuq', $result);
}
}

0 comments on commit baff0a7

Please sign in to comment.