mirror of
https://github.com/zhamao-robot/zhamao-framework.git
synced 2026-07-25 01:25:34 +08:00
refactor phpunit-swoole to phpunit-zm and move old test cases
This commit is contained in:
130
tests_old/ZM/Container/ContainerCallTest.php
Normal file
130
tests_old/ZM/Container/ContainerCallTest.php
Normal file
@@ -0,0 +1,130 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\ZM\Container;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ZM\Container\Container;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ContainerCallTest extends TestCase
|
||||
{
|
||||
public function testCallInvokableClass(): void
|
||||
{
|
||||
$container = new Container();
|
||||
$this->assertEquals('foo', $container->call(Invokable::class, ['echo' => 'foo']));
|
||||
}
|
||||
|
||||
public function testCallClassMethodWithDependencies(): void
|
||||
{
|
||||
$container = new Container();
|
||||
$name = 'Steve' . time();
|
||||
$container->bind(FooDependency::class, FooDependencyImpl::class);
|
||||
$container->bind(BarDependency::class, function () use ($name) {
|
||||
return new BarDependencyImpl($name);
|
||||
});
|
||||
$this->assertEquals("hello, {$name}", $container->call([Foo::class, 'sayHello']));
|
||||
}
|
||||
|
||||
public function testCallClassStaticMethodWithDependencies(): void
|
||||
{
|
||||
$container = new Container();
|
||||
$name = 'Alex' . time();
|
||||
$container->bind(FooDependency::class, FooDependencyImpl::class);
|
||||
$container->bind(BarDependency::class, function () use ($name) {
|
||||
return new BarDependencyImpl($name);
|
||||
});
|
||||
$this->assertEquals("hello, {$name}", $container->call([Foo::class, 'staticSayHello']));
|
||||
}
|
||||
|
||||
public function testCallClassMethodWithDependenciesInjectedByConstructor(): void
|
||||
{
|
||||
$container = new Container();
|
||||
$name = 'Donny' . time();
|
||||
$container->bind(FooDependency::class, FooDependencyImpl::class);
|
||||
$container->bind(BarDependency::class, function () use ($name) {
|
||||
return new BarDependencyImpl($name);
|
||||
});
|
||||
$this->assertEquals('goodbye', $container->call([Foo::class, 'sayGoodbye']));
|
||||
}
|
||||
|
||||
public function testCallClassStaticMethodViaDoubleColons(): void
|
||||
{
|
||||
$container = new Container();
|
||||
$name = 'Herobrine' . time();
|
||||
$container->bind(FooDependency::class, FooDependencyImpl::class);
|
||||
$container->bind(BarDependency::class, function () use ($name) {
|
||||
return new BarDependencyImpl($name);
|
||||
});
|
||||
$this->assertEquals("hello, {$name}", $container->call(Foo::class . '::staticSayHello'));
|
||||
}
|
||||
}
|
||||
|
||||
class Invokable
|
||||
{
|
||||
public function __invoke(string $echo)
|
||||
{
|
||||
return $echo;
|
||||
}
|
||||
}
|
||||
|
||||
interface FooDependency
|
||||
{
|
||||
public function sayGoodbye(): string;
|
||||
}
|
||||
|
||||
class FooDependencyImpl implements FooDependency
|
||||
{
|
||||
public function sayGoodbye(): string
|
||||
{
|
||||
return 'goodbye';
|
||||
}
|
||||
}
|
||||
|
||||
interface BarDependency
|
||||
{
|
||||
public function getName(): string;
|
||||
}
|
||||
|
||||
class BarDependencyImpl implements BarDependency
|
||||
{
|
||||
private $name;
|
||||
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
|
||||
class Foo
|
||||
{
|
||||
private $fooDependency;
|
||||
|
||||
public function __construct(FooDependency $fooDependency)
|
||||
{
|
||||
$this->fooDependency = $fooDependency;
|
||||
}
|
||||
|
||||
public function sayHello(BarDependency $barDependency): string
|
||||
{
|
||||
return 'hello, ' . $barDependency->getName();
|
||||
}
|
||||
|
||||
public static function staticSayHello(BarDependency $barDependency): string
|
||||
{
|
||||
return 'hello, ' . $barDependency->getName();
|
||||
}
|
||||
|
||||
public function sayGoodbye(): string
|
||||
{
|
||||
return $this->fooDependency->sayGoodbye();
|
||||
}
|
||||
}
|
||||
52
tests_old/ZM/Container/ContainerTest.php
Normal file
52
tests_old/ZM/Container/ContainerTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\ZM\Container;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ZM\Container\Container;
|
||||
use ZM\Container\WorkerContainer;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class ContainerTest extends TestCase
|
||||
{
|
||||
public function testCanInheritParentBinding(): void
|
||||
{
|
||||
$worker_container = new WorkerContainer();
|
||||
$worker_container->instance('foo', 'bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->instance('baz', 'qux');
|
||||
|
||||
// 获取父容器的实例
|
||||
$this->assertEquals('bar', $container->get('foo'));
|
||||
|
||||
// 获取自身容器的实例
|
||||
$this->assertEquals('qux', $container->get('baz'));
|
||||
}
|
||||
|
||||
public function testCanOverrideParentBinding(): void
|
||||
{
|
||||
$worker_container = new WorkerContainer();
|
||||
$worker_container->instance('foo', 'bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->instance('foo', 'qux');
|
||||
|
||||
$this->assertEquals('qux', $container->get('foo'));
|
||||
}
|
||||
|
||||
public function testCannotModifyParentBinding(): void
|
||||
{
|
||||
$worker_container = new WorkerContainer();
|
||||
$worker_container->instance('foo', 'bar');
|
||||
|
||||
$container = new Container();
|
||||
$container->instance('foo', 'qux');
|
||||
|
||||
$this->assertEquals('bar', $worker_container->get('foo'));
|
||||
}
|
||||
}
|
||||
120
tests_old/ZM/Container/WorkerContainerTest.php
Normal file
120
tests_old/ZM/Container/WorkerContainerTest.php
Normal file
@@ -0,0 +1,120 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\ZM\Container;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ZM\Container\EntryResolutionException;
|
||||
use ZM\Container\WorkerContainer;
|
||||
use ZM\Utils\MessageUtil;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class WorkerContainerTest extends TestCase
|
||||
{
|
||||
private $container;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->container = new WorkerContainer();
|
||||
$this->container->flush();
|
||||
}
|
||||
|
||||
public function testInstance(): void
|
||||
{
|
||||
$this->container->instance('test', 'test');
|
||||
$this->assertEquals('test', $this->container->make('test'));
|
||||
|
||||
$t2 = new WorkerContainer();
|
||||
$this->assertEquals('test', $t2->make('test'));
|
||||
}
|
||||
|
||||
public function testAlias(): void
|
||||
{
|
||||
$this->container->alias(MessageUtil::class, 'bar');
|
||||
$this->container->alias('bar', 'baz');
|
||||
$this->container->alias('baz', 'bas');
|
||||
$this->assertInstanceOf(MessageUtil::class, $this->container->make('bas'));
|
||||
}
|
||||
|
||||
public function testGetAlias(): void
|
||||
{
|
||||
$this->container->alias(MessageUtil::class, 'bar');
|
||||
$this->assertEquals(MessageUtil::class, $this->container->getAlias('bar'));
|
||||
}
|
||||
|
||||
public function testBindClosure(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$this->assertEquals('test', $this->container->make('test'));
|
||||
}
|
||||
|
||||
public function testFlush(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$this->container->flush();
|
||||
$this->expectException(EntryResolutionException::class);
|
||||
$this->container->make('test');
|
||||
}
|
||||
|
||||
public function testBindIf(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$this->container->bindIf('test', function () {
|
||||
return 'test2';
|
||||
});
|
||||
$this->assertEquals('test', $this->container->make('test'));
|
||||
}
|
||||
|
||||
public function testGet(): void
|
||||
{
|
||||
$this->testMake();
|
||||
}
|
||||
|
||||
public function testBound(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$this->assertTrue($this->container->bound('test'));
|
||||
$this->assertFalse($this->container->bound('test2'));
|
||||
}
|
||||
|
||||
public function testFactory(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$factory = $this->container->factory('test');
|
||||
$this->assertEquals($this->container->make('test'), $factory());
|
||||
}
|
||||
|
||||
public function testMake(): void
|
||||
{
|
||||
$this->container->bind('test', function () {
|
||||
return 'test';
|
||||
});
|
||||
$this->assertEquals('test', $this->container->make('test'));
|
||||
}
|
||||
|
||||
public function testHas(): void
|
||||
{
|
||||
$this->testBound();
|
||||
}
|
||||
|
||||
public function testBuild(): void
|
||||
{
|
||||
$this->assertEquals('test', $this->container->build(function () {
|
||||
return 'test';
|
||||
}));
|
||||
$this->assertInstanceOf(MessageUtil::class, $this->container->build(MessageUtil::class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user