79bc64d69b9ade92dc7149582fcbf054094a356c
[yaffs-website] / vendor / symfony / config / Tests / Resource / ClassExistenceResourceTest.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\Config\Tests\Resource;
13
14 use PHPUnit\Framework\TestCase;
15 use Symfony\Component\Config\Resource\ClassExistenceResource;
16 use Symfony\Component\Config\Tests\Fixtures\BadParent;
17 use Symfony\Component\Config\Tests\Fixtures\Resource\ConditionalClass;
18
19 class ClassExistenceResourceTest extends TestCase
20 {
21     public function testToString()
22     {
23         $res = new ClassExistenceResource('BarClass');
24         $this->assertSame('BarClass', (string) $res);
25     }
26
27     public function testGetResource()
28     {
29         $res = new ClassExistenceResource('BarClass');
30         $this->assertSame('BarClass', $res->getResource());
31     }
32
33     public function testIsFreshWhenClassDoesNotExist()
34     {
35         $res = new ClassExistenceResource('Symfony\Component\Config\Tests\Fixtures\BarClass');
36
37         $this->assertTrue($res->isFresh(time()));
38
39         eval(<<<EOF
40 namespace Symfony\Component\Config\Tests\Fixtures;
41
42 class BarClass
43 {
44 }
45 EOF
46         );
47
48         $this->assertFalse($res->isFresh(time()));
49     }
50
51     public function testIsFreshWhenClassExists()
52     {
53         $res = new ClassExistenceResource('Symfony\Component\Config\Tests\Resource\ClassExistenceResourceTest');
54
55         $this->assertTrue($res->isFresh(time()));
56     }
57
58     public function testExistsKo()
59     {
60         spl_autoload_register($autoloader = function ($class) use (&$loadedClass) { $loadedClass = $class; });
61
62         try {
63             $res = new ClassExistenceResource('MissingFooClass');
64             $this->assertTrue($res->isFresh(0));
65
66             $this->assertSame('MissingFooClass', $loadedClass);
67
68             $loadedClass = 123;
69
70             $res = new ClassExistenceResource('MissingFooClass', false);
71
72             $this->assertSame(123, $loadedClass);
73         } finally {
74             spl_autoload_unregister($autoloader);
75         }
76     }
77
78     public function testBadParentWithTimestamp()
79     {
80         $res = new ClassExistenceResource(BadParent::class, false);
81         $this->assertTrue($res->isFresh(time()));
82     }
83
84     /**
85      * @expectedException \ReflectionException
86      * @expectedExceptionMessage Class Symfony\Component\Config\Tests\Fixtures\MissingParent not found
87      */
88     public function testBadParentWithNoTimestamp()
89     {
90         $res = new ClassExistenceResource(BadParent::class, false);
91         $res->isFresh(0);
92     }
93
94     public function testConditionalClass()
95     {
96         $res = new ClassExistenceResource(ConditionalClass::class, false);
97
98         $this->assertFalse($res->isFresh(0));
99     }
100 }