Skip to content

Commit 9b4bd68

Browse files
committed
added tests. renamed package.
1 parent 55ece67 commit 9b4bd68

File tree

5 files changed

+143
-36
lines changed

5 files changed

+143
-36
lines changed

README.md

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,44 @@
1-
PHP Resource Locator
1+
PHP Container
22
====================
33

44
Example
55
-------
66
```php
77
<?php
88

9-
use frostealth\Locator\Locator;
9+
use frostealth\Container\Container;
1010

11-
$locator = new Locator();
11+
$container = new Container();
1212

1313
// ...
1414

1515
// injecting simple values
16-
$locator->set('foo', 'bar'); // or $locator->foo = 'bar';
16+
$container->set('foo', 'bar'); // or $container->foo = 'bar';
1717

1818
// get its value
19-
$value = $locator->get('foo'); // or $value = $locator->foo;
19+
$value = $container->get('foo'); // or $value = $container->foo;
2020

2121
// ...
2222

2323
// resources
24-
$locator->set('object', function ($locator) {
25-
return new MyObject($locator->foo);
24+
$container->set('object', function ($container) {
25+
return new MyObject($container->foo);
2626
});
2727

2828
// get a new instance
29-
$object = $locator->get('object');
29+
$object = $container->get('object');
3030

3131
// ...
3232

3333
// singleton resources
34-
$locator->singleton('log', function ($locator) {
35-
return new MyLog($locator->object);
34+
$container->singleton('log', function ($container) {
35+
return new MyLog($container->object);
3636
});
3737

3838
// get log resource
39-
$log = $locator->get('log');
39+
$log = $container->get('log');
4040
```
4141

42-
Note
43-
----
44-
[Use Locator as singleton](https://github.com/frostealth/php-singleton-trait)
45-
4642
Requirements
4743
------------
4844
* PHP >= 5.4
49-
* [PHP Data Storage v1.0](https://github.com/frostealth/php-data-storage/releases)

composer.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
{
2-
"name": "frostealth/php-resource-locator",
3-
"description": "PHP Resource Locator",
4-
"version" : "1.0.0",
2+
"name": "frostealth/php-resource-container",
3+
"description": "PHP Container",
4+
"keywords": ["di", "container"],
5+
"license": "MIT",
56
"authors": [
67
{
78
"name": "Kudinov Ivan",
8-
"email": "frostealth@gmail.com"
9+
"email": "frostealth@gmail.com",
10+
"role": "Developer"
911
}
1012
],
1113
"autoload": {
1214
"psr-4": {
13-
"frostealth\\Locator\\": "src/"
15+
"frostealth\\": "src/"
1416
}
1517
},
16-
"minimum-stability": "dev",
1718
"require": {
1819
"php": ">=5.4.0",
1920
"frostealth/php-data-storage": "1.0.*"
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"frostealth\\Container\\tests\\": "tests/"
25+
}
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "4.2.*"
2029
}
2130
}

phpunit.xml.dist

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
8+
9+
<testsuites>
10+
<testsuite name="Container Tests">
11+
<directory>./tests</directory>
12+
</testsuite>
13+
</testsuites>
14+
15+
</phpunit>

src/Locator.php renamed to src/Container/Container.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
<?php
22

3-
namespace frostealth\Locator;
3+
namespace frostealth\Container;
44

55
use frostealth\Storage\Data;
66

77
/**
8-
* Class Locator
8+
* Class Container
99
*
10-
* @package frostealth\Locator
10+
* @package frostealth\Container
1111
*/
12-
class Locator
12+
class Container
1313
{
1414
/** @var Data */
15-
protected $container;
15+
protected $storage;
1616

1717
public function __construct()
1818
{
19-
$this->container = new Data();
19+
$this->storage = new Data();
2020
}
2121

2222
/**
@@ -26,7 +26,7 @@ public function __construct()
2626
*/
2727
public function get($name)
2828
{
29-
$value = $this->container->get($name);
29+
$value = $this->storage->get($name);
3030
if (is_callable($value)) {
3131
$value = $value($this);
3232
}
@@ -40,7 +40,7 @@ public function get($name)
4040
*/
4141
public function set($name, $value)
4242
{
43-
$this->container->set($name, $value);
43+
$this->storage->set($name, $value);
4444
}
4545

4646
/**
@@ -50,28 +50,28 @@ public function set($name, $value)
5050
*/
5151
public function has($name)
5252
{
53-
return $this->container->has($name);
53+
return $this->storage->has($name);
5454
}
5555

5656
/**
5757
* @param string $name
5858
*/
5959
public function remove($name)
6060
{
61-
$this->container->remove($name);
61+
$this->storage->remove($name);
6262
}
6363

6464
/**
6565
* @param string $name
66-
* @param callable $value
66+
* @param callable $callable
6767
*/
68-
public function singleton($name, \Closure $value)
68+
public function singleton($name, \Closure $callable)
6969
{
70-
$this->container->set($name, function () use ($value) {
70+
$this->storage->set($name, function () use ($callable) {
7171
static $instance = null;
7272

7373
if ($instance === null) {
74-
$instance = $value($this);
74+
$instance = $callable($this);
7575
}
7676

7777
return $instance;

tests/ContainerTest.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
namespace frostealth\Container\tests;
4+
5+
use frostealth\Container\Container;
6+
7+
/**
8+
* Class ContainerTest
9+
*
10+
* @package frostealth\ContainerTest\tests
11+
*/
12+
class ContainerTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/** @var Container */
15+
private $container;
16+
17+
public function setUp()
18+
{
19+
$this->container = new Container();
20+
}
21+
22+
public function testSetAndGet()
23+
{
24+
$this->assertNull($this->container->get('unregistered'));
25+
26+
$obj = $this->getMock('stdClass');
27+
28+
$this->container->set('obj', $obj);
29+
$this->assertSame($obj, $this->container->get('obj'));
30+
31+
$this->container->set('name', 'Robert');
32+
$this->container->set('obj', function (Container $locator) {
33+
$obj = $this->getMock('stdClass');
34+
$obj->name = $locator->get('name');
35+
36+
return $obj;
37+
});
38+
39+
$this->assertNotEquals($obj, $this->container->get('obj'));
40+
41+
$obj->name = $this->container->get('name');
42+
43+
$this->assertEquals($obj, $this->container->get('obj'));
44+
$this->assertNotSame($this->container->get('obj'), $this->container->get('obj'));
45+
}
46+
47+
public function testHas()
48+
{
49+
$this->assertFalse($this->container->has('simple'));
50+
51+
$this->container->set('simple', 'simple-def');
52+
$this->assertTrue($this->container->has('simple'));
53+
}
54+
55+
public function testRemove()
56+
{
57+
$this->container->set('name', 'Robert');
58+
$this->container->remove('name');
59+
60+
$this->assertFalse($this->container->has('name'));
61+
}
62+
63+
public function testSingleton()
64+
{
65+
$this->container->set('string', 'example');
66+
67+
$obj = $this->getMock('stdClass');
68+
$obj->string = 'example';
69+
70+
$this->container->singleton('singleton', function (Container $Container) {
71+
$object = $this->getMock('stdClass');
72+
$object->string = $Container->get('string');
73+
74+
return $object;
75+
});
76+
77+
$this->assertEquals($obj, $this->container->get('singleton'));
78+
$this->assertSame($this->container->get('singleton'), $this->container->get('singleton'));
79+
80+
$this->container->singleton('singleton', function () {
81+
return $this->getMock('stdClass');
82+
});
83+
84+
$this->assertInstanceOf('stdClass', $this->container->get('singleton'));
85+
$this->assertNotEquals($obj, $this->container->get('singleton'));
86+
}
87+
}
88+

0 commit comments

Comments
 (0)