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;
18 class AutowireServiceResourceTest extends TestCase
21 * @var AutowireServiceResource
28 protected function setUp()
30 $this->file = realpath(sys_get_temp_dir()).'/tmp.php';
32 touch($this->file, $this->time);
34 $this->class = __NAMESPACE__.'\Foo';
35 $this->resource = new AutowireServiceResource(
42 public function testToString()
44 $this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
47 public function testSerializeUnserialize()
49 $unserialized = unserialize(serialize($this->resource));
51 $this->assertEquals($this->resource, $unserialized);
54 public function testIsFresh()
56 $this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
57 $this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
58 $this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
61 public function testIsFreshForDeletedResources()
65 $this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
68 public function testIsNotFreshChangedResource()
70 $oldResource = new AutowireServiceResource(
73 array('will_be_different')
76 // test with a stale file *and* a resource that *will* be different than the actual
77 $this->assertFalse($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
80 public function testIsFreshSameConstructorArgs()
82 $oldResource = AutowirePass::createResourceForClass(
83 new \ReflectionClass(__NAMESPACE__.'\Foo')
86 // test with a stale file *but* the resource will not be changed
87 $this->assertTrue($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
90 public function testNotFreshIfClassNotFound()
92 $resource = new AutowireServiceResource(
93 'Some\Non\Existent\Class',
98 $this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
101 protected function tearDown()
103 if (!file_exists($this->file)) {
110 private function getStaleFileTime()
112 return $this->time - 10;
118 public function __construct($foo)