0b3c0f84c427c9f0cbbb232b9860b974f5328e9f
[yaffs-website] / vendor / symfony / dependency-injection / Tests / Config / AutowireServiceResourceTest.php
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 PHPUnit\Framework\TestCase;
15 use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
16 use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
17
18 class AutowireServiceResourceTest extends TestCase
19 {
20     /**
21      * @var AutowireServiceResource
22      */
23     private $resource;
24     private $file;
25     private $class;
26     private $time;
27
28     protected function setUp()
29     {
30         $this->file = realpath(sys_get_temp_dir()).'/tmp.php';
31         $this->time = time();
32         touch($this->file, $this->time);
33
34         $this->class = __NAMESPACE__.'\Foo';
35         $this->resource = new AutowireServiceResource(
36             $this->class,
37             $this->file,
38             array()
39         );
40     }
41
42     public function testToString()
43     {
44         $this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
45     }
46
47     public function testSerializeUnserialize()
48     {
49         $unserialized = unserialize(serialize($this->resource));
50
51         $this->assertEquals($this->resource, $unserialized);
52     }
53
54     public function testIsFresh()
55     {
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');
59     }
60
61     public function testIsFreshForDeletedResources()
62     {
63         unlink($this->file);
64
65         $this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
66     }
67
68     public function testIsNotFreshChangedResource()
69     {
70         $oldResource = new AutowireServiceResource(
71             $this->class,
72             $this->file,
73             array('will_be_different')
74         );
75
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');
78     }
79
80     public function testIsFreshSameConstructorArgs()
81     {
82         $oldResource = AutowirePass::createResourceForClass(
83             new \ReflectionClass(__NAMESPACE__.'\Foo')
84         );
85
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');
88     }
89
90     public function testNotFreshIfClassNotFound()
91     {
92         $resource = new AutowireServiceResource(
93             'Some\Non\Existent\Class',
94             $this->file,
95             array()
96         );
97
98         $this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
99     }
100
101     protected function tearDown()
102     {
103         if (!file_exists($this->file)) {
104             return;
105         }
106
107         unlink($this->file);
108     }
109
110     private function getStaleFileTime()
111     {
112         return $this->time - 10;
113     }
114 }
115
116 class Foo
117 {
118     public function __construct($foo)
119     {
120     }
121 }