From 3979519f3f224fae2439c0b246360782a7b39c92 Mon Sep 17 00:00:00 2001 From: Artur Eshenbrener Date: Tue, 13 Mar 2018 19:09:28 +0300 Subject: [PATCH 1/5] [FrameworkBundle] Respect debug mode when warm up annotations --- .../FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php | 7 +++++-- .../FrameworkBundle/Resources/config/annotations.xml | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index ec55e9c6fc93a..d52e29d46bda6 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -28,17 +28,20 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer { private $annotationReader; private $excludeRegexp; + private $debug; /** * @param Reader $annotationReader * @param string $phpArrayFile The PHP file where annotations are cached * @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached + * @param bool $debug Run in debug mode */ - public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null) + public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null, $debug) { parent::__construct($phpArrayFile, $fallbackPool); $this->annotationReader = $annotationReader; $this->excludeRegexp = $excludeRegexp; + $this->debug = $debug; } /** @@ -53,7 +56,7 @@ protected function doWarmUp($cacheDir, ArrayAdapter $arrayAdapter) } $annotatedClasses = include $annotatedClassPatterns; - $reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter)); + $reader = new CachedReader($this->annotationReader, new DoctrineProvider($arrayAdapter), $this->debug); foreach ($annotatedClasses as $class) { if (null !== $this->excludeRegexp && preg_match($this->excludeRegexp, $class)) { diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml index 6e8cc4f9e6639..2b4ea429628e3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/annotations.xml @@ -38,6 +38,7 @@ %kernel.cache_dir%/annotations.php #^Symfony\\(?:Component\\HttpKernel\\|Bundle\\FrameworkBundle\\Controller\\(?!AbstractController$|Controller$))# + %kernel.debug% From 36228cda432560d5a571d6abb9661d9ceeaa2321 Mon Sep 17 00:00:00 2001 From: Artur Eshenbrener Date: Thu, 15 Mar 2018 15:07:29 +0300 Subject: [PATCH 2/5] [FrameworkBundle] Respect debug mode when warm up annotations Make new argument for AnnotationsCacheWarmer::__construct optional to prevent BC break Add tests --- .../CacheWarmer/AnnotationsCacheWarmer.php | 2 +- .../AnnotationsCacheWarmerTest.php | 103 ++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php diff --git a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php index d52e29d46bda6..3c32cb1c4a33a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php +++ b/src/Symfony/Bundle/FrameworkBundle/CacheWarmer/AnnotationsCacheWarmer.php @@ -36,7 +36,7 @@ class AnnotationsCacheWarmer extends AbstractPhpFileCacheWarmer * @param CacheItemPoolInterface $fallbackPool The pool where runtime-discovered annotations are cached * @param bool $debug Run in debug mode */ - public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null, $debug) + public function __construct(Reader $annotationReader, $phpArrayFile, CacheItemPoolInterface $fallbackPool, $excludeRegexp = null, $debug = false) { parent::__construct($phpArrayFile, $fallbackPool); $this->annotationReader = $annotationReader; diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php new file mode 100644 index 0000000000000..4099a1ade0e73 --- /dev/null +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -0,0 +1,103 @@ +cacheDir = sys_get_temp_dir() . '/' . uniqid(); + $fs = new Filesystem(); + $fs->mkdir($this->cacheDir); + parent::setUp(); + } + + protected function tearDown() + { + $fs = new Filesystem(); + $fs->remove($this->cacheDir); + parent::tearDown(); + } + + public function testAnnotationsCacheWarmerWithDebugDisabled() + { + file_put_contents($this->cacheDir.'/annotations.map', sprintf('cacheDir, __FUNCTION__); + $reader = new AnnotationReader(); + $fallbackPool = new ArrayAdapter(); + $warmer = new AnnotationsCacheWarmer( + $reader, + $cacheFile, + $fallbackPool, + null + ); + $warmer->warmUp($this->cacheDir); + $this->assertFileExists($cacheFile); + + // Assert cache is valid + /** @var Reader|\PHPUnit_Framework_MockObject_MockObject $readerMock */ + $readerMock = $this->createMock(Reader::class); + $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); + $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotations'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotation'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); + $reader = new CachedReader( + $readerMock, + new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())) + ); + $refClass = new \ReflectionClass($this); + $reader->getClassAnnotations($refClass); + $reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__)); + $reader->getPropertyAnnotations($refClass->getProperty('cacheDir')); + } + + public function testAnnotationsCacheWarmerWithDebugEnabled() + { + file_put_contents($this->cacheDir.'/annotations.map', sprintf('cacheDir, __FUNCTION__); + $reader = new AnnotationReader(); + $fallbackPool = new ArrayAdapter(); + $warmer = new AnnotationsCacheWarmer( + $reader, + $cacheFile, + $fallbackPool, + null, + true + ); + $warmer->warmUp($this->cacheDir); + $this->assertFileExists($cacheFile); + // Assert cache is valid + /** @var Reader|\PHPUnit_Framework_MockObject_MockObject $readerMock */ + $readerMock = $this->createMock(Reader::class); + $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); + $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotations'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotation'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); + $reader = new CachedReader( + $readerMock, + new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())), + true + ); + $refClass = new \ReflectionClass($this); + $reader->getClassAnnotations($refClass); + $reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__)); + $reader->getPropertyAnnotations($refClass->getProperty('cacheDir')); + } +} From 4d251a29218786a7a86eb4416e66f9d82d0f42f6 Mon Sep 17 00:00:00 2001 From: Artur Eshenbrener Date: Thu, 15 Mar 2018 15:09:35 +0300 Subject: [PATCH 3/5] [FrameworkBundle] Respect debug mode when warm up annotations Code style --- .../Tests/CacheWarmer/AnnotationsCacheWarmerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php index 4099a1ade0e73..d63466a4516c4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -19,7 +19,7 @@ class AnnotationsCacheWarmerTest extends TestCase protected function setUp() { - $this->cacheDir = sys_get_temp_dir() . '/' . uniqid(); + $this->cacheDir = sys_get_temp_dir().'/'.uniqid(); $fs = new Filesystem(); $fs->mkdir($this->cacheDir); parent::setUp(); @@ -34,7 +34,7 @@ protected function tearDown() public function testAnnotationsCacheWarmerWithDebugDisabled() { - file_put_contents($this->cacheDir.'/annotations.map', sprintf('cacheDir.'/annotations.map', sprintf('cacheDir, __FUNCTION__); $reader = new AnnotationReader(); $fallbackPool = new ArrayAdapter(); @@ -68,7 +68,7 @@ public function testAnnotationsCacheWarmerWithDebugDisabled() public function testAnnotationsCacheWarmerWithDebugEnabled() { - file_put_contents($this->cacheDir.'/annotations.map', sprintf('cacheDir.'/annotations.map', sprintf('cacheDir, __FUNCTION__); $reader = new AnnotationReader(); $fallbackPool = new ArrayAdapter(); From 247ea1a8b781dc05d5960be96b16fd2727209954 Mon Sep 17 00:00:00 2001 From: Artur Eshenbrener Date: Fri, 16 Mar 2018 10:04:49 +0300 Subject: [PATCH 4/5] [FrameworkBundle] Respect debug mode when warm up annotations Extracted reader mock to method, made test compatible with old php-unit --- .../AnnotationsCacheWarmerTest.php | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php index d63466a4516c4..87200e7c38198 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -57,7 +57,7 @@ public function testAnnotationsCacheWarmerWithDebugDisabled() $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); $reader = new CachedReader( - $readerMock, + $this->getReadOnlyReader(), new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())) ); $refClass = new \ReflectionClass($this); @@ -82,16 +82,8 @@ public function testAnnotationsCacheWarmerWithDebugEnabled() $warmer->warmUp($this->cacheDir); $this->assertFileExists($cacheFile); // Assert cache is valid - /** @var Reader|\PHPUnit_Framework_MockObject_MockObject $readerMock */ - $readerMock = $this->createMock(Reader::class); - $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); - $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); - $readerMock->expects($this->exactly(0))->method('getMethodAnnotations'); - $readerMock->expects($this->exactly(0))->method('getMethodAnnotation'); - $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); - $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); $reader = new CachedReader( - $readerMock, + $this->getReadOnlyReader(), new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())), true ); @@ -100,4 +92,20 @@ public function testAnnotationsCacheWarmerWithDebugEnabled() $reader->getMethodAnnotations($refClass->getMethod(__FUNCTION__)); $reader->getPropertyAnnotations($refClass->getProperty('cacheDir')); } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Reader + */ + private function getReadOnlyReader() + { + $readerMock = $this->getMockBuilder(Reader::class)->getMock(); + $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); + $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotations'); + $readerMock->expects($this->exactly(0))->method('getMethodAnnotation'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); + $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); + + return $readerMock; + } } From b7fa3f7414489a45c98d8c48044c77f3c8e3af5c Mon Sep 17 00:00:00 2001 From: Artur Eshenbrener Date: Fri, 16 Mar 2018 13:56:46 +0300 Subject: [PATCH 5/5] [FrameworkBundle] Respect debug mode when warm up annotations tests compatibility with php 5.5 --- .../Tests/CacheWarmer/AnnotationsCacheWarmerTest.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php index 87200e7c38198..b32274e7e7a80 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/CacheWarmer/AnnotationsCacheWarmerTest.php @@ -48,14 +48,6 @@ public function testAnnotationsCacheWarmerWithDebugDisabled() $this->assertFileExists($cacheFile); // Assert cache is valid - /** @var Reader|\PHPUnit_Framework_MockObject_MockObject $readerMock */ - $readerMock = $this->createMock(Reader::class); - $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); - $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); - $readerMock->expects($this->exactly(0))->method('getMethodAnnotations'); - $readerMock->expects($this->exactly(0))->method('getMethodAnnotation'); - $readerMock->expects($this->exactly(0))->method('getPropertyAnnotations'); - $readerMock->expects($this->exactly(0))->method('getPropertyAnnotation'); $reader = new CachedReader( $this->getReadOnlyReader(), new DoctrineProvider(new PhpArrayAdapter($cacheFile, new NullAdapter())) @@ -98,7 +90,7 @@ public function testAnnotationsCacheWarmerWithDebugEnabled() */ private function getReadOnlyReader() { - $readerMock = $this->getMockBuilder(Reader::class)->getMock(); + $readerMock = $this->getMockBuilder('Doctrine\Common\Annotations\Reader')->getMock(); $readerMock->expects($this->exactly(0))->method('getClassAnnotations'); $readerMock->expects($this->exactly(0))->method('getClassAnnotation'); $readerMock->expects($this->exactly(0))->method('getMethodAnnotations');