FileResourceTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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\FileResource;
  13. class FileResourceTest extends TestCase
  14. {
  15. protected $resource;
  16. protected $file;
  17. protected $time;
  18. protected function setUp()
  19. {
  20. $this->file = sys_get_temp_dir().'/tmp.xml';
  21. $this->time = time();
  22. touch($this->file, $this->time);
  23. $this->resource = new FileResource($this->file);
  24. }
  25. protected function tearDown()
  26. {
  27. if (!file_exists($this->file)) {
  28. return;
  29. }
  30. unlink($this->file);
  31. }
  32. public function testGetResource()
  33. {
  34. $this->assertSame(realpath($this->file), $this->resource->getResource(), '->getResource() returns the path to the resource');
  35. }
  36. public function testGetResourceWithScheme()
  37. {
  38. $resource = new FileResource('file://'.$this->file);
  39. $this->assertSame('file://'.$this->file, $resource->getResource(), '->getResource() returns the path to the schemed resource');
  40. }
  41. public function testToString()
  42. {
  43. $this->assertSame(realpath($this->file), (string) $this->resource);
  44. }
  45. /**
  46. * @expectedException \InvalidArgumentException
  47. * @expectedExceptionMessageRegExp /The file ".*" does not exist./
  48. */
  49. public function testResourceDoesNotExist()
  50. {
  51. $resource = new FileResource('/____foo/foobar'.mt_rand(1, 999999));
  52. }
  53. public function testIsFresh()
  54. {
  55. $this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
  56. $this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
  57. $this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
  58. }
  59. public function testIsFreshForDeletedResources()
  60. {
  61. unlink($this->file);
  62. $this->assertFalse($this->resource->isFresh($this->time), '->isFresh() returns false if the resource does not exist');
  63. }
  64. public function testSerializeUnserialize()
  65. {
  66. $unserialized = unserialize(serialize($this->resource));
  67. $this->assertSame(realpath($this->file), $this->resource->getResource());
  68. }
  69. }