4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\DependencyInjection\Tests\Config;
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
16 use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
21 class AutowireServiceResourceTest extends TestCase
24 * @var AutowireServiceResource
31 protected function setUp()
33 $this->file = realpath(sys_get_temp_dir()).'/tmp.php';
35 touch($this->file, $this->time);
37 $this->class = __NAMESPACE__.'\Foo';
38 $this->resource = new AutowireServiceResource(
45 public function testToString()
47 $this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
50 public function testSerializeUnserialize()
52 $unserialized = unserialize(serialize($this->resource));
54 $this->assertEquals($this->resource, $unserialized);
57 public function testIsFresh()
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');
64 public function testIsFreshForDeletedResources()
68 $this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
71 public function testIsNotFreshChangedResource()
73 $oldResource = new AutowireServiceResource(
76 array('will_be_different')
79 // test with a stale file *and* a resource that *will* be different than the actual
80 $this->assertFalse($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
83 public function testIsFreshSameConstructorArgs()
85 $oldResource = AutowirePass::createResourceForClass(
86 new \ReflectionClass(__NAMESPACE__.'\Foo')
89 // test with a stale file *but* the resource will not be changed
90 $this->assertTrue($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
93 public function testNotFreshIfClassNotFound()
95 $resource = new AutowireServiceResource(
96 'Some\Non\Existent\Class',
101 $this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
104 protected function tearDown()
106 if (!file_exists($this->file)) {
113 private function getStaleFileTime()
115 return $this->time - 10;
121 public function __construct($foo)