|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\DependencyInjection\Tests\Config; |
| 13 | + |
| 14 | +use Symfony\Component\DependencyInjection\Config\AutowireServiceResource; |
| 15 | + |
| 16 | +class AutowireServiceResourceTest extends \PHPUnit_Framework_TestCase |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @var AutowireServiceResource |
| 20 | + */ |
| 21 | + private $resource; |
| 22 | + private $file; |
| 23 | + private $class; |
| 24 | + private $time; |
| 25 | + private $constructorArgs; |
| 26 | + |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $this->file = realpath(sys_get_temp_dir()).'/tmp.php'; |
| 30 | + $this->time = time(); |
| 31 | + touch($this->file, $this->time); |
| 32 | + |
| 33 | + $this->class = 'Symfony\Component\DependencyInjection\Tests\Config\FooReplicator'; |
| 34 | + $this->resource = new AutowireServiceResource( |
| 35 | + $this->class, |
| 36 | + $this->file |
| 37 | + ); |
| 38 | + $this->constructorArgs = array( |
| 39 | + null, // no type-hint on this argument |
| 40 | + 'AppBundle\Service\Foo', |
| 41 | + ); |
| 42 | + $this->resource->setConstructorArguments($this->constructorArgs); |
| 43 | + } |
| 44 | + |
| 45 | + public function testToString() |
| 46 | + { |
| 47 | + $this->assertSame('service.autowire.'.$this->class, (string) $this->resource); |
| 48 | + } |
| 49 | + |
| 50 | + public function testSerializeUnserialize() |
| 51 | + { |
| 52 | + $unserialized = unserialize(serialize($this->resource)); |
| 53 | + |
| 54 | + $this->assertEquals($this->resource, $unserialized); |
| 55 | + } |
| 56 | + |
| 57 | + public function testIsFresh() |
| 58 | + { |
| 59 | + $this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second'); |
| 60 | + $this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed'); |
| 61 | + $this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated'); |
| 62 | + } |
| 63 | + |
| 64 | + public function testIsFreshForDeletedResources() |
| 65 | + { |
| 66 | + unlink($this->file); |
| 67 | + |
| 68 | + $this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist'); |
| 69 | + } |
| 70 | + |
| 71 | + public function testIsFreshChangedConstructorArgs() |
| 72 | + { |
| 73 | + $newResource = new AutowireServiceResource( |
| 74 | + $this->class, |
| 75 | + $this->file |
| 76 | + ); |
| 77 | + // a new 3rd argument was added! |
| 78 | + $args = $this->constructorArgs; |
| 79 | + $args[] = 'AppBundle\Service\Bar'; |
| 80 | + $newResource->setConstructorArguments($args); |
| 81 | + |
| 82 | + // "fake" the return value |
| 83 | + $callback = function(\ReflectionClass $reflectionClass) use ($newResource) { |
| 84 | + return $newResource; |
| 85 | + }; |
| 86 | + $this->resource->setCreateResourceCallback($callback); |
| 87 | + |
| 88 | + $this->assertFalse($this->resource->isFresh($this->time - 10), '->isFresh() returns false if the constructor arguments have changed'); |
| 89 | + } |
| 90 | + |
| 91 | + public function testIsFreshSameConstructorArgs() |
| 92 | + { |
| 93 | + $newResource = new AutowireServiceResource( |
| 94 | + $this->class, |
| 95 | + $this->file |
| 96 | + ); |
| 97 | + |
| 98 | + $newResource->setConstructorArguments($this->constructorArgs); |
| 99 | + |
| 100 | + // "fake" the return value |
| 101 | + $callback = function(\ReflectionClass $reflectionClass) use ($newResource) { |
| 102 | + return $newResource; |
| 103 | + }; |
| 104 | + |
| 105 | + $this->resource->setCreateResourceCallback($callback); |
| 106 | + $this->assertFalse($this->resource->isFresh($this->time - 10), '->isFresh() returns false if the constructor arguments have changed'); |
| 107 | + } |
| 108 | + |
| 109 | + protected function tearDown() |
| 110 | + { |
| 111 | + if (!file_exists($this->file)) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + unlink($this->file); |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +// exists just so that some reflection in the tests doesn't fail |
| 120 | +class FooReplicator |
| 121 | +{ |
| 122 | +} |
0 commit comments