AutowireServiceResourceTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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\DependencyInjection\Tests\Config;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
  13. use Symfony\Component\DependencyInjection\Config\AutowireServiceResource;
  14. /**
  15. * @group legacy
  16. */
  17. class AutowireServiceResourceTest extends TestCase
  18. {
  19. /**
  20. * @var AutowireServiceResource
  21. */
  22. private $resource;
  23. private $file;
  24. private $class;
  25. private $time;
  26. protected function setUp()
  27. {
  28. $this->file = realpath(sys_get_temp_dir()).'/tmp.php';
  29. $this->time = time();
  30. touch($this->file, $this->time);
  31. $this->class = __NAMESPACE__.'\Foo';
  32. $this->resource = new AutowireServiceResource(
  33. $this->class,
  34. $this->file,
  35. array()
  36. );
  37. }
  38. public function testToString()
  39. {
  40. $this->assertSame('service.autowire.'.$this->class, (string) $this->resource);
  41. }
  42. public function testSerializeUnserialize()
  43. {
  44. $unserialized = unserialize(serialize($this->resource));
  45. $this->assertEquals($this->resource, $unserialized);
  46. }
  47. public function testIsFresh()
  48. {
  49. $this->assertTrue($this->resource->isFresh($this->time), '->isFresh() returns true if the resource has not changed in same second');
  50. $this->assertTrue($this->resource->isFresh($this->time + 10), '->isFresh() returns true if the resource has not changed');
  51. $this->assertFalse($this->resource->isFresh($this->time - 86400), '->isFresh() returns false if the resource has been updated');
  52. }
  53. public function testIsFreshForDeletedResources()
  54. {
  55. unlink($this->file);
  56. $this->assertFalse($this->resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the resource does not exist');
  57. }
  58. public function testIsNotFreshChangedResource()
  59. {
  60. $oldResource = new AutowireServiceResource(
  61. $this->class,
  62. $this->file,
  63. array('will_be_different')
  64. );
  65. // test with a stale file *and* a resource that *will* be different than the actual
  66. $this->assertFalse($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
  67. }
  68. public function testIsFreshSameConstructorArgs()
  69. {
  70. $oldResource = AutowirePass::createResourceForClass(
  71. new \ReflectionClass(__NAMESPACE__.'\Foo')
  72. );
  73. // test with a stale file *but* the resource will not be changed
  74. $this->assertTrue($oldResource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the constructor arguments have changed');
  75. }
  76. public function testNotFreshIfClassNotFound()
  77. {
  78. $resource = new AutowireServiceResource(
  79. 'Some\Non\Existent\Class',
  80. $this->file,
  81. array()
  82. );
  83. $this->assertFalse($resource->isFresh($this->getStaleFileTime()), '->isFresh() returns false if the class no longer exists');
  84. }
  85. protected function tearDown()
  86. {
  87. if (!file_exists($this->file)) {
  88. return;
  89. }
  90. unlink($this->file);
  91. }
  92. private function getStaleFileTime()
  93. {
  94. return $this->time - 10;
  95. }
  96. }
  97. class Foo
  98. {
  99. public function __construct($foo)
  100. {
  101. }
  102. }