woocommerce/plugins/woocommerce-blocks/tests/php/Registry/Container.php

79 lines
2.3 KiB
PHP
Raw Normal View History

Implement PHP DI container and refactor. Also implements new Asset data interface for extendable settings passed to js. (https://github.com/woocommerce/woocommerce-blocks/pull/956) * Add dependency injection container for blocks * Add new Pacakge and Bootstrap classes. - Bootstrap for bootstrapping the plugin. - Package will replace `src/Package` and added as a dependency for any classes needing package info. * Introduce AssetsDataRegistry for managing asset data * refactor existing classes to use new DIC and Asset Data Registry - this is the bare minimum needed to make this pull viable. - further refactors will be done in more atomic smaller pulls for easier review. * add new settings handling and export `@woocommerce/settings` as an alias to wc.wcSettings - the export is exposed php side on the `wc-settings` handle. * Remove unnecessary concatenation * Fix typos and improve doc blocks * fix php linting issue * Use better escaping function. * improve jsdoc spacing * improve test assertion * use fully qualified class names in bootstrap * improve comment block to account for dynamic version string replace on build * handle exceptions a bit differently * correct dependency reference in webpack config * remove blank lines * fix doc block comment alignment * Various doc/grammar/spacing fixes from code review. Co-Authored-By: Albert Juhé Lluveras <contact@albertjuhe.com> * improve naming, documentation and logic of filter callbacks While this is intended for sanitization/validation, the callback ultimately provides flexibility for filtering the value before returning or setting in state so `filter` is a better name for this.
2019-09-23 18:07:13 +00:00
<?php
namespace Automattic\WooCommerce\Blocks\Tests\Registry;
use Automattic\WooCommerce\Blocks\Registry\Container as ContainerTest;
use Automattic\WooCommerce\Blocks\Registry\FactoryType;
use Automattic\WooCommerce\Blocks\Tests\Mocks\MockTestDependency;
use PHPUnit\Framework\TestCase;
/**
* Tests the Container functionality
*
* This also implicitly tests the FactoryType and SharedType classes.
*
* @since $VID:$
* @group testing
*/
class Container extends TestCase {
private $container;
public function setUp() {
$this->container = new ContainerTest;
}
public function test_factory() {
$factory = $this->container->factory( function () { return 'foo'; } );
$this->assertInstanceOf( FactoryType::class, $factory );
}
public function test_registering_factory_type() {
$this->container->register(
MockTestDependency::class,
$this->container->factory(
function () { return new MockTestDependency; }
)
);
$instanceA = $this->container->get( MockTestDependency::class );
$instanceB = $this->container->get( MockTestDependency::class );
// should not be the same instance;
$this->assertNotSame( $instanceA, $instanceB );
}
public function test_registering_shared_type() {
$this->container->register(
MockTestDependency::class,
function () { return new MockTestDependency; }
);
$instanceA = $this->container->get( MockTestDependency::class );
$instanceB = $this->container->get( MockTestDependency::class );
// should not be the same instance;
$this->assertSame( $instanceA, $instanceB );
}
public function test_registering_shared_type_dependent_on_another_shared_type() {
$this->container->register(
MockTestDependency::class . 'A',
function() { return new MockTestDependency; }
);
$this->container->register(
MockTestDependency::class . 'B',
function( $container ) {
return new MockTestDependency(
$container->get( MockTestDependency::class . 'A' )
);
}
);
$instanceA = $this->container->get( MockTestDependency::class . 'A' );
$instanceB = $this->container->get( MockTestDependency::class . 'B' );
// should not be the same instance
$this->assertNotSame( $instanceA, $instanceB );
// dependency on B should be the same as A
$this->assertSame( $instanceA, $instanceB->dependency );
}
}