Commit 9452a158 authored by Josh Pollock's avatar Josh Pollock
Browse files

copy code from interop

parents
.DS_Store
Thumbs.db
wp-cli.local.yml
node_modules/
vendor/
*.sql
*.tar.gz
*.zip
.php_cs.cache
Tests/plugins/
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectPlainTextFileTypeManager">
<file url="file://$PROJECT_DIR$/Tests/ValidatingEntityTest.php" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/caldera-container.iml" filepath="$PROJECT_DIR$/.idea/caldera-container.iml" />
</modules>
</component>
</project>
\ No newline at end of file
This diff is collapsed.
language: php
php:
- 5.6
- 7.0
- 7.1
script: composer test
install: composer install
\ No newline at end of file
<?php
class ContainerTest extends TestCase
{
/**
*
* @covers \calderawp\CalderaContainers\Container::get()
* @covers \calderawp\CalderaContainers\Container::set()
* @covers \calderawp\CalderaContainers\Container::offsetGet()
* @covers \calderawp\CalderaContainers\Container::offsetSet()
*/
public function testSet()
{
$container = new \calderawp\CalderaContainers\Tests\Mocks\Container();
$container->set('hi', 'roy' );
$this->assertEquals( $container[ 'hi'], $container->get('hi' ) );
$container = new \calderawp\CalderaContainers\Tests\Mocks\Container();
$container[ 'x' ] = 1;
$this->assertEquals( 1, $container[ 'x' ] );
$this->assertEquals( $container->get('x'), $container[ 'x' ] );
$container = new \calderawp\CalderaContainers\Tests\Mocks\Container();
$y = new stdClass();
$y->x = 1;
$container->set( 'y', $y );
$this->assertSame( $y, $container->get( 'y' ) );
}
/**
* @covers \calderawp\CalderaContainers\Container::has()
* @covers \calderawp\CalderaContainers\Container::offsetExists()
*/
public function testHas()
{
$container = new \calderawp\CalderaContainers\Tests\Mocks\Container();
$container[ 'x' ] = 1;
$this->assertTrue( $container->has('x' ) );
$this->assertFalse( $container->has('y' ) );
}
/**
* @covers \calderawp\CalderaContainers\Container::has()
* @covers \calderawp\CalderaContainers\Container::offsetUnset()
*/
public function testUnset()
{
$container = new \calderawp\CalderaContainers\Tests\Mocks\Container();
$container[ 'x' ] = 1;
unset( $container['x'] );
$this->assertFalse( $container->has('x' ) );
}
}
\ No newline at end of file
<?php
namespace calderawp\CalderaContainers\Tests\Mocks;
class Container extends \calderawp\CalderaContainers\Container
{
}
\ No newline at end of file
<?php
namespace calderawp\CalderaContainers\Tests\Mocks;
class Something
{
public $prop;
}
\ No newline at end of file
<?php
namespace calderawp\CalderaContainers\Tests\Mocks;
class SomethingElse
{
public $prop;
}
\ No newline at end of file
<?php
class ServiceContainerTest extends TestCase
{
/**
* Test provider registration
*
* @covers Container::bind()
* @covers Container::make()
*/
public function testRegisterProvider()
{
$ref = 'roy';
$container = new \calderawp\CalderaContainers\Service\Container();
$container->bind( $ref, function (){
return new \calderawp\CalderaContainers\Tests\Mocks\Something();
});
$this->assertSame( \calderawp\CalderaContainers\Tests\Mocks\Something::class, get_class( $container->make($ref )) );
}
/**
*
* @covers Container::bind()
* @covers Container::make()
*/
public function testRegisterTwoProviders()
{
$classRef1 = \calderawp\CalderaContainers\Tests\Mocks\Something::class;
$container = new \calderawp\CalderaContainers\Service\Container();
$container->bind( $classRef1, function (){
return new \calderawp\CalderaContainers\Tests\Mocks\Something();
});
$classRef2 = \calderawp\CalderaContainers\Tests\Mocks\SomethingElse::class;
$container->bind( $classRef2, function (){
return new \calderawp\CalderaContainers\Tests\Mocks\SomethingElse();
});
$this->assertSame( $classRef1, get_class( $container->make($classRef1 )) );
$this->assertSame( $classRef2, get_class( $container->make($classRef2 )) );
}
/**
* Test that each object returned by bind, that is not set to be a singleton
*
* @covers Container::bind()
* @covers Container::make()
*/
public function testBindNotSingleton()
{
$classRef1 = \calderawp\CalderaContainers\Tests\Mocks\Something::class;
$container = new \calderawp\CalderaContainers\Service\Container();
$container->bind( $classRef1, function (){
$entity = new \calderawp\CalderaContainers\Tests\Mocks\Something();
$entity->prop = rand();
return $entity;
});
$this->assertNotEquals( $container->make($classRef1),$container->make($classRef1));
$this->assertNotEquals( $container->make($classRef1)->prop,$container->make($classRef1)->prop);
}
/**
* Test that objects bound as singletons always return the same instance
*
* @covers Container::singleton()
* @covers Container::bind()
* @covers Container::make()
*/
public function testSingleton()
{
$container = new \calderawp\CalderaContainers\Service\Container();
$classRef =\calderawp\CalderaContainers\Tests\Mocks\Something::class;
$container->singleton( $classRef, new \calderawp\CalderaContainers\Tests\Mocks\Something());
$this->assertSame( $container->make($classRef), $container->make($classRef));
}
}
\ No newline at end of file
<?php
abstract class TestCase extends PHPUnit_Framework_TestCase
{
}
\ No newline at end of file
<?php
if (file_exists(dirname(__FILE__, 2) . '/vendor/autoload.php')) {
function loader($class)
{
$file = __DIR__ . '/' .$class . '.php';
if (file_exists($file)) {
require $file;
}
}
spl_autoload_register('loader');
include_once dirname(__FILE__, 2) . '/vendor/autoload.php';
} else {
throw new Exception('No autoloader');
}
{
"name": "calderawp/caldera-containers",
"type": "libary",
"license": "GPL-2.0",
"authors": [
{
"name": "josh",
"email": "josh@calderawp.com"
}
],
"minimum-stability": "dev",
"require": {
"php": "^5.6|^7.0",
"pimple/pimple": "^3.2",
"psr/container": "^1.0@dev"
},
"autoload": {
"psr-4": {
"calderawp\\CalderaContainers\\": "src"
}
},
"require-dev": {
"phpunit/phpunit": "5.0.*",
"squizlabs/php_codesniffer": "^3.2",
"jakub-onderka/php-parallel-lint": "^1.0"
},
"autoload-dev": {
"psr-4": {
"calderawp\\CalderaContainers\\Tests\\": "Tests"
}
},
"scripts" : {
"tests" : "composer lints && composer fixes && composer test",
"test" : "phpunit Tests",
"tests-now" : "phpunit Tests --group=now",
"sniffs" : "phpcs src/",
"fixes" : "phpcbf src/",
"lints" : "parallel-lint ./src --blame --exclude vendor && parallel-lint ./Tests --blame --exclude vendor"
}
}
This diff is collapsed.
<?xml version="1.0"?>
<!--Largely Sourced Form https://gist.github.com/gsherwood/9d22f634c57f990a7c64-->
<ruleset name="CalderaLearn">
<description>PSR2 with tabs instead of spaces.</description>
<arg name="tab-width" value="4"/>
<rule ref="PSR2">
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
</rule>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="tabIndent" value="true"/>
</properties>
</rule>
<!-- The soft limit on line length MUST be 120 characters; automated style checkers MUST warn but MUST NOT error at the soft limit. -->
<rule ref="Generic.Files.LineLength">
<properties>
<property name="lineLimit" value="120"/>
<property name="absoluteLineLimit" value="0"/>
</properties>
</rule>
</ruleset>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="Tests/bootstrap.php" colors="true">
<testsuites>
<testsuite name="Interop Tests">
<directory>Tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<ini name="date.timezone" value="UTC"/>
</php>
</phpunit>
\ No newline at end of file
<?php
namespace calderawp\CalderaContainers;
use calderawp\CalderaContainers\Interfaces\Arrayable;
use Psr\Container\ContainerInterface;
/**
* Class Container
*
* Container that is just pimple + interface implimentations
*
* @package calderawp\interop
*/
abstract class Container implements \JsonSerializable, Arrayable, ContainerInterface, \ArrayAccess
{
/**
* @var \Pimple\Container
*/
private $pimple;
/**
* @return \Pimple\Container
*/
protected function getPimple()
{
if (! $this->pimple) {
$this->pimple = new \Pimple\Container();
}
return $this->pimple;
}
/**
* @inheritdoc
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* @inheritdoc
*/
public function toArray()
{
return (array)$this->getPimple();
}
/**
* @inheritdoc
*/
public function get($id)
{
if ($this->has($id)) {
return $this->getPimple()->offsetGet($id);
}
}
/**
* @inheritdoc
*/
public function has($id)
{
return $this->getPimple()->offsetExists($id);
}
/**
* @inheritdoc
*/
public function set($id, $value)
{
$this->getPimple()->offsetSet($id, $value);
return $this;
}
/**
* @inheritdoc
*/
public function offsetExists($offset)
{
return $this->getPimple()->offsetExists($offset);
}
/**
* @inheritdoc
*/
public function offsetGet($offset)
{
return $this->getPimple()->offsetGet($offset);
}
/**
* @inheritdoc
*/
public function offsetSet($offset, $value)
{
$this->getPimple()->offsetSet($offset, $value);
}
/**
* @inheritdoc
*/
public function offsetUnset($offset)
{
$this->getPimple()->offsetUnset($offset);
}
}
<?php
namespace calderawp\CalderaContainers;
/**
* Class ControlledContainer
*
* Container that only allows whitelisted attributes
*
*/
abstract class ControlledContainer extends Container
{
/**
* @var array
*/
protected $defaults;
/**
* @var array
*/
protected $attributes;
public function __construct(array $attributes = array(), array $defaults = array())
{
$this->setProps($attributes, $defaults);
}
/**
* @inheritdoc
*/
public function get($id)
{
if ($this->allowed($id)) {
if ($this->offsetExists($id)) {
return $this->getPimple()->offsetGet($id);
} elseif (array_key_exists($id, $this->defaults)) {
return $this->defaults[ $id ];
} else {
return null;
}
}
return null;
}
/**
* @param string $id
* @param mixed $value
* @return $this
*/
public function set($id, $value)
{
if ($this->allowed($id)) {
return parent::set($id, $value);
}
return $this;
}
/**
* @inheritdoc
*/
public function has($id)
{
return $this->allowed($id) && parent::has($id);
}
/**
* @param $id
* @return bool
*/
public function allowed($id)
{
return isset($id, $this->attributes);
}
/**
* @param array $attributes
* @param array $defaults
*/
private function setProps(array $attributes, array $defaults)
{
$this->attributes = $attributes;
$this->defaults = $defaults;
$this->propArrayMerge('attributes', $attributes);
$this->propArrayMerge('defaults', $defaults);
}
/**
* @param $prop
* @param array $new
*/
private function propArrayMerge($prop, array $new = array())
{
if (! empty($new)) {
if (! empty($this->$prop)) {
$this->$prop = $new;
} else {
$this->$prop = array_merge($new, $this->$prop);
}
}
}
}
<?php
namespace calderawp\CalderaContainers\Exceptions;
use Psr\Container\ContainerExceptionInterface;
/**
* Class ContainerException
*
* Exception to throw for ALL container related exceptions.
*/
class ContainerException extends Exception implements ContainerExceptionInterface
{
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment