/src/Shell/Task/FixtureLoadingTask.php

https://github.com/dereuromark/upgrade · PHP · 110 lines · 56 code · 17 blank · 37 comment · 6 complexity · 4784e649d153aaf773c2739b0488f735 MD5 · raw file

  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Upgrade\Shell\Task;
  16. use Cake\Utility\Inflector;
  17. /**
  18. * Updates test cases for 3.0
  19. *
  20. * @property \Cake\Upgrade\Shell\Task\StageTask $Stage
  21. */
  22. class FixtureLoadingTask extends BaseTask {
  23. use ChangeTrait;
  24. /**
  25. * @var array
  26. */
  27. public $tasks = ['Stage'];
  28. /**
  29. * Process tests regarding fixture usage and update it for 3.x
  30. *
  31. * @param string $path Path
  32. * @return bool
  33. */
  34. protected function _process($path) {
  35. $original = $contents = $this->Stage->source($path);
  36. $contents = $this->_replaceFixtureLoading($contents);
  37. return $this->Stage->change($path, $original, $contents);
  38. }
  39. /**
  40. * @param string $contents
  41. *
  42. * @return string
  43. */
  44. protected function _replaceFixtureLoading($contents) {
  45. // Serializes data from PHP data into PHP code.
  46. // Basically a code style conformant version of var_export()
  47. $export = function ($values) use (&$export) {
  48. $vals = [];
  49. if (!is_array($values)) {
  50. return $vals;
  51. }
  52. foreach ($values as $key => $val) {
  53. if (is_array($val)) {
  54. $vals[] = "'{$key}' => [" . implode(', ', $export($val)) . ']';
  55. } else {
  56. $val = var_export($val, true);
  57. if ($val === 'NULL') {
  58. $val = 'null';
  59. }
  60. if (!is_numeric($key)) {
  61. $vals[] = "'{$key}' => {$val}";
  62. } else {
  63. $vals[] = "{$val}";
  64. }
  65. }
  66. }
  67. return $vals;
  68. };
  69. // Process field property.
  70. $processor = function ($matches) use ($export) {
  71. //@codingStandardsIgnoreStart
  72. eval('$data = [' . $matches[2] . '];');
  73. //@codingStandardsIgnoreEnd
  74. $out = [];
  75. foreach ($data as $key => $fixture) {
  76. $pieces = explode('.', $fixture);
  77. $fixtureName = $pieces[count($pieces) - 1];
  78. $fixtureName = Inflector::pluralize($fixtureName);
  79. $pieces[count($pieces) - 1] = $fixtureName;
  80. $out[] = implode('.', $pieces);
  81. }
  82. return $matches[1] . "\n\t\t" . implode(",\n\t\t", $export($out)) . "\n\t" . $matches[3];
  83. };
  84. $contents = preg_replace_callback(
  85. '/(public \$fixtures\s+=\s+(?:array\(|\[))(.*?)(\);|\];)/ms',
  86. $processor,
  87. $contents,
  88. -1,
  89. $count
  90. );
  91. return $contents;
  92. }
  93. }