FileExistenceResourceTest.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Config\Tests\Resource;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Config\Resource\FileExistenceResource;
  13. class FileExistenceResourceTest extends TestCase
  14. {
  15. protected $resource;
  16. protected $file;
  17. protected $time;
  18. protected function setUp()
  19. {
  20. $this->file = realpath(sys_get_temp_dir()).'/tmp.xml';
  21. $this->time = time();
  22. $this->resource = new FileExistenceResource($this->file);
  23. }
  24. protected function tearDown()
  25. {
  26. if (file_exists($this->file)) {
  27. unlink($this->file);
  28. }
  29. }
  30. public function testToString()
  31. {
  32. $this->assertSame($this->file, (string) $this->resource);
  33. }
  34. public function testGetResource()
  35. {
  36. $this->assertSame($this->file, $this->resource->getResource(), '->getResource() returns the path to the resource');
  37. }
  38. public function testIsFreshWithExistingResource()
  39. {
  40. touch($this->file, $this->time);
  41. $serialized = serialize(new FileExistenceResource($this->file));
  42. $resource = unserialize($serialized);
  43. $this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still present');
  44. unlink($this->file);
  45. $resource = unserialize($serialized);
  46. $this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been deleted');
  47. }
  48. public function testIsFreshWithAbsentResource()
  49. {
  50. $serialized = serialize(new FileExistenceResource($this->file));
  51. $resource = unserialize($serialized);
  52. $this->assertTrue($resource->isFresh($this->time), '->isFresh() returns true if the resource is still absent');
  53. touch($this->file, $this->time);
  54. $resource = unserialize($serialized);
  55. $this->assertFalse($resource->isFresh($this->time), '->isFresh() returns false if the resource has been created');
  56. }
  57. }