diff --git a/lib/internal/Magento/Framework/Code/GeneratedFiles.php b/lib/internal/Magento/Framework/Code/GeneratedFiles.php
index 972990c4377ba2005589443baa5cf096257d72b5..4eaa437d96ef8731db5d8ebe2f00aaf59f25ce3f 100644
--- a/lib/internal/Magento/Framework/Code/GeneratedFiles.php
+++ b/lib/internal/Magento/Framework/Code/GeneratedFiles.php
@@ -57,36 +57,7 @@ class GeneratedFiles
             $configPool = new ConfigFilePool();
             $envPath = $deploymentConfig . '/' . $configPool->getPath(ConfigFilePool::APP_ENV);
             if ($this->write->isExist($this->write->getRelativePath($envPath))) {
-                $cacheData = include $envPath;
-
-                if (isset($cacheData['cache_types'])) {
-                    $enabledCacheTypes = $cacheData['cache_types'];
-                    $enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
-                            return $value;
-                        }
-                    );
-                    if (!empty($enabledCacheTypes)) {
-                        $this->write->writeFile($this->write->getRelativePath(
-                                $this->directoryList->getPath(DirectoryList::VAR_DIR)) . '/.cachestates.json',
-                            json_encode($enabledCacheTypes)
-                        );
-                        $cacheTypes = array_keys($cacheData['cache_types']);
-
-                        foreach ($cacheTypes as $cacheType) {
-                            $cacheData['cache_types'][$cacheType] = 0;
-                        }
-
-                        $formatter = new PhpFormatter();
-                        $contents = $formatter->format($cacheData);
-
-                        $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
-                        if (function_exists('opcache_invalidate')) {
-                            opcache_invalidate(
-                                $this->write->getAbsolutePath($envPath)
-                            );
-                        }
-                    }
-                }
+                $this->saveCacheStatus($envPath);
             }
             $cachePath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::CACHE));
             $generationPath = $this->write->getRelativePath($this->directoryList->getPath(DirectoryList::GENERATION));
@@ -107,6 +78,44 @@ class GeneratedFiles
         }
     }
 
+    /**
+     * Read Cache types from env.php and write to a json file.
+     *
+     * @param $envPath
+     */
+    private function saveCacheStatus($envPath)
+    {
+        $cacheData = include $envPath;
+
+        if (isset($cacheData['cache_types'])) {
+            $enabledCacheTypes = $cacheData['cache_types'];
+            $enabledCacheTypes = array_filter($enabledCacheTypes, function ($value) {
+                return $value;
+            });
+            if (!empty($enabledCacheTypes)) {
+                $this->write->writeFile($this->write->getRelativePath(
+                        $this->directoryList->getPath(DirectoryList::VAR_DIR)
+                    ) . '/.cachestates.json',
+                    json_encode($enabledCacheTypes)
+                );
+                $cacheTypes = array_keys($cacheData['cache_types']);
+
+                foreach ($cacheTypes as $cacheType) {
+                    $cacheData['cache_types'][$cacheType] = 0;
+                }
+
+                $formatter = new PhpFormatter();
+                $contents = $formatter->format($cacheData);
+
+                $this->write->writeFile($this->write->getRelativePath($envPath), $contents);
+                if (function_exists('opcache_invalidate')) {
+                    opcache_invalidate(
+                        $this->write->getAbsolutePath($envPath)
+                    );
+                }
+            }
+        }
+    }
     /**
      * Create flag for regeneration of code and di
      *
diff --git a/setup/src/Magento/Setup/Test/Unit/Model/Cron/JobUpgradeTest.php b/setup/src/Magento/Setup/Test/Unit/Model/Cron/JobUpgradeTest.php
index fd5cf8410ff312082778d983927f1df4de7fd67b..a9d8ac8dbde1410f9b53a4a04d26e8e3969aca59 100644
--- a/setup/src/Magento/Setup/Test/Unit/Model/Cron/JobUpgradeTest.php
+++ b/setup/src/Magento/Setup/Test/Unit/Model/Cron/JobUpgradeTest.php
@@ -36,7 +36,7 @@ class JobUpgradeTest extends \PHPUnit_Framework_TestCase
 
         $writeFactory->expects($this->once())->method('create')->willReturn($write);
         $directoryList = $this->getMock('\Magento\Framework\App\Filesystem\DirectoryList', [], [], '', false);
-        $directoryList->expects($this->once())->method('getPath')->willReturn( '/some/full/path' . $pathToCacheStatus);
+        $directoryList->expects($this->once())->method('getPath')->willReturn('/some/full/path' . $pathToCacheStatus);
 
         $objectManager->expects($this->any())->method('get')->will($this->returnValueMap([
             ['\Magento\Framework\Filesystem\Directory\WriteFactory', $writeFactory],