Skip to content

Commit

Permalink
Remove yiisoft/di dependency (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
devanych committed May 24, 2021
1 parent fc6a12d commit 5c453dc
Show file tree
Hide file tree
Showing 14 changed files with 81 additions and 69 deletions.
10 changes: 5 additions & 5 deletions composer.json
Expand Up @@ -19,15 +19,15 @@
"require": {
"php": "^7.4|^8.0",
"psr/container": "1.0.0",
"yiisoft/html": "^1.0",
"yiisoft/di": "^3.0@dev",
"yiisoft/html": "^1.2",
"yiisoft/factory": "^3.0@dev"
},
"require-dev": {
"phpunit/phpunit": "^9.4",
"roave/infection-static-analysis-plugin": "^1.6",
"phpunit/phpunit": "^9.5",
"roave/infection-static-analysis-plugin": "^1.8",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.2"
"vimeo/psalm": "^4.7",
"yiisoft/test-support": "^1.3"
},
"autoload": {
"psr-4": {
Expand Down
13 changes: 12 additions & 1 deletion config/providers.php
Expand Up @@ -2,6 +2,17 @@

declare(strict_types=1);

use Yiisoft\Di\Container;
use Yiisoft\Di\Contracts\ServiceProviderInterface;
use Yiisoft\Widget\WidgetFactory;

return [
'yiisoft/widget' => \Yiisoft\Widget\WidgetFactoryProvider::class,
'yiisoft/widget' => static function (): ServiceProviderInterface {
return new class() implements ServiceProviderInterface {
public function register(Container $container): void
{
WidgetFactory::initialize($container);
}
};
},
];
44 changes: 26 additions & 18 deletions phpunit.xml.dist
@@ -1,20 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" verbose="true" bootstrap="vendor/autoload.php" failOnRisky="true" failOnWarning="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory>./</directory>
</include>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<testsuites>
<testsuite name="Yii widget tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
<phpunit
bootstrap="vendor/autoload.php"
colors="true"
verbose="true"
failOnRisky="true"
failOnWarning="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false"
executionOrder="random"
resolveDependencies="true"
>
<php>
<ini name="error_reporting" value="-1"/>
</php>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Yii Widget tests">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
1 change: 1 addition & 0 deletions src/Widget.php
Expand Up @@ -7,6 +7,7 @@
use RuntimeException;
use Yiisoft\Factory\Exception\InvalidConfigException;
use Yiisoft\Html\NoEncodeStringableInterface;

use function array_key_exists;
use function get_class;
use function is_array;
Expand Down
28 changes: 25 additions & 3 deletions src/WidgetFactory.php
Expand Up @@ -6,17 +6,37 @@

use Psr\Container\ContainerInterface;
use Yiisoft\Factory\Factory;
use Yiisoft\Factory\Exception\InvalidConfigException;
use Yiisoft\Factory\Exception\NotInstantiableException;
use Yiisoft\Factory\FactoryInterface;

final class WidgetFactory extends Factory
{
private static ?FactoryInterface $factory = null;

/**
* @param ContainerInterface|null $container
* @param array<string, mixed> $definitions
*
* @throws InvalidConfigException
* @throws NotInstantiableException
*
* @see Factory::__construct()
*/
private function __construct(ContainerInterface $container = null, array $definitions = [])
{
parent::__construct($container, $definitions);
}

/**
* @param ContainerInterface|null $container
* @param array<string, mixed> $definitions
*
* @throws InvalidConfigException
* @throws NotInstantiableException
*
* @see Factory::__construct()
*/
public static function initialize(ContainerInterface $container = null, array $definitions = []): void
{
self::$factory = new self($container, $definitions);
Expand All @@ -28,19 +48,21 @@ public static function initialize(ContainerInterface $container = null, array $d
* @param array|callable|string $config parameters for creating a widget
*
* @throws \RuntimeException if factory was not initialized
* @throws \Yiisoft\Factory\Exception\InvalidConfigException
* @throws InvalidConfigException
*
* @psalm-suppress MoreSpecificReturnType
* @see Factory::create()
*
* @return Widget
*
* @psalm-suppress MixedInferredReturnType
* @psalm-suppress MixedReturnStatement
*/
public static function createWidget($config): Widget
{
if (self::$factory === null) {
throw new \RuntimeException('Widget factory should be initialized with WidgetFactory::initialize() call.');
}

/** @psalm-suppress LessSpecificReturnStatement */
return self::$factory->create($config);
}
}
16 changes: 0 additions & 16 deletions src/WidgetFactoryProvider.php

This file was deleted.

2 changes: 1 addition & 1 deletion tests/Stubs/ImmutableWidget.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Widget\Widget;

class ImmutableWidget extends Widget
final class ImmutableWidget extends Widget
{
private string $id = 'original';

Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Injectable.php
Expand Up @@ -4,6 +4,6 @@

namespace Yiisoft\Widget\Tests\Stubs;

class Injectable
final class Injectable
{
}
2 changes: 1 addition & 1 deletion tests/Stubs/TestInjectionWidget.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Widget\Widget;

class TestInjectionWidget extends Widget
final class TestInjectionWidget extends Widget
{
private Injectable $injectable;

Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/TestWidget.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Widget\Widget;

class TestWidget extends Widget
final class TestWidget extends Widget
{
private string $id;

Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/TestWidgetA.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Widget\Widget;

class TestWidgetA extends Widget
final class TestWidgetA extends Widget
{
private string $id;

Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/TestWidgetB.php
Expand Up @@ -6,7 +6,7 @@

use Yiisoft\Widget\Widget;

class TestWidgetB extends Widget
final class TestWidgetB extends Widget
{
private string $id;

Expand Down
12 changes: 6 additions & 6 deletions tests/TestCase.php
Expand Up @@ -6,19 +6,21 @@

use ReflectionClass;
use PHPUnit\Framework\TestCase as BaseTestCase;
use ReflectionException;
use Yiisoft\Di\Container;
use Yiisoft\Test\Support\Container\SimpleContainer;
use Yiisoft\Widget\Tests\Stubs\Injectable;
use Yiisoft\Widget\WidgetFactory;

abstract class TestCase extends BaseTestCase
{
private Container $container;
private SimpleContainer $container;

protected function setUp(): void
{
parent::setUp();

$this->container = new Container([]);
$this->container = new SimpleContainer([
Injectable::class => new Injectable(),
]);

WidgetFactory::initialize($this->container, []);
}
Expand All @@ -37,8 +39,6 @@ protected function tearDown(): void
* @param string $propertyName
* @param $value
* @param bool $revoke whether to make property inaccessible after setting
*
* @throws ReflectionException
*/
protected function setInaccessibleProperty(object $object, string $propertyName, $value, bool $revoke = true): void
{
Expand Down
14 changes: 0 additions & 14 deletions tests/bootstrap.php

This file was deleted.

0 comments on commit 5c453dc

Please sign in to comment.