Hoa central
Public Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
Hoa\Test\Bin\Generate Class Reference
Inheritance diagram for Hoa\Test\Bin\Generate:

Public Member Functions

 main ()
 
 usage ()
 
- Public Member Functions inherited from Hoa\Console\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 getOption (&$optionValue, $short=null)
 
 setOptions (Array $options)
 
 makeUsageOptionsList (Array $definitions=[])
 
 resolveOptionAmbiguity (Array $solutions)
 
 status ($text, $status)
 
 readLine ($prefix=null)
 
 readPassword ($prefix=null)
 
- Public Member Functions inherited from Hoa\Dispatcher\Kit
 __construct (Router $router, Dispatcher $dispatcher, View\Viewable $view=null)
 
 construct ()
 

Static Protected Member Functions

static info ($message, $sub=false)
 
static findClasses ($root, $namespace)
 

Protected Attributes

 $options
 
- Protected Attributes inherited from Hoa\Console\Dispatcher\Kit
 $options = null
 
 $_options = null
 

Additional Inherited Members

- Public Attributes inherited from Hoa\Console\Dispatcher\Kit
 $parser = null
 
- Public Attributes inherited from Hoa\Dispatcher\Kit
 $router = null
 
 $dispatcher = null
 
 $view = null
 
 $data = null
 

Detailed Description

Class Hoa.

Automatically generate test suites.

Definition at line 54 of file Generate.php.

Member Function Documentation

static Hoa\Test\Bin\Generate::findClasses (   $root,
  $namespace 
)
staticprotected

Find all classes from a root.

Parameters
string$rootRoot.
string$namespaceNamespace to prepend.
Returns
array

Definition at line 328 of file Generate.php.

329  {
330  $out = [];
331  $finder = new File\Finder();
332  $finder->in($root)
333  ->files()
334  ->name('#^(?!\.).+\.php#');
335 
336  foreach ($finder as $fileinfo) {
337  $out[] =
338  $namespace . '\\' .
339  str_replace(
340  DS,
341  '\\',
342  trim(
343  substr(
344  $fileinfo->getRelativePathname(),
345  0,
346  -4
347  ),
348  DS
349  )
350  );
351  }
352 
353  return $out;
354  }
static Hoa\Test\Bin\Generate::info (   $message,
  $sub = false 
)
staticprotected

Format a message for the dry-run mode.

Parameters
string$messageMessage.
bool$subWhether this is a sub-message or not.
Returns
string

Definition at line 312 of file Generate.php.

313  {
314  return
316  (false === $sub ? '# ' : '') . $message,
317  'foreground(yellow)'
318  );
319  }
static colorize($text, $attributesBefore, $attributesAfter= 'normal')
Definition: Text.php:83

Here is the call graph for this function:

Hoa\Test\Bin\Generate::main ( )

The entry method.

Returns
int

Definition at line 76 of file Generate.php.

77  {
78  $dryRun = false;
79  $classes = [];
80 
81  while (false !== $c = $this->getOption($v)) {
82  switch ($c) {
83  case 'n':
84  foreach ($this->parser->parseSpecialValue($v) as $namespace) {
85  $namespace = trim(str_replace('.', '\\', $namespace), '\\');
86 
87  if (false === $pos = strpos($namespace, '\\')) {
88  throw new Console\Exception(
89  'Namespace %s is too short.',
90  0,
91  $namespace
92  );
93  }
94 
95  $tail = substr($namespace, strpos($namespace, '\\') + 1);
96  $root = resolve('hoa://Library/' . str_replace('\\', '/', $tail));
97 
98  $classes = array_merge(
99  $classes,
100  static::findClasses($root, $namespace)
101  );
102  }
103 
104  break;
105 
106  case 'c':
107  foreach ($this->parser->parseSpecialValue($v) as $class) {
108  $classes[] = $class;
109  }
110 
111  break;
112 
113  case 'd':
114  $dryRun = $v;
115 
116  break;
117 
118  case '__ambiguous':
119  $this->resolveOptionAmbiguity($v);
120 
121  break;
122 
123  case 'h':
124  case '?':
125  default:
126  return $this->usage();
127  }
128  }
129 
130  if (empty($classes)) {
131  return $this->usage();
132  }
133 
134  foreach ($classes as $i => $class) {
135  $classes[$i] = str_replace('.', '\\', $class);
136  }
137 
138  $generator = new \Atoum\PraspelExtension\Praspel\Generator();
139  $generator->setTestNamespacer(function ($namespace) {
140  $parts = explode('\\', $namespace);
141 
142  return
143  implode('\\', array_slice($parts, 0, 2)) .
144  '\\Test\\Praspel\\Unit' .
145  (isset($parts[2])
146  ? '\\' . implode('\\', array_slice($parts, 2))
147  : '');
148  });
149 
150  $phpBinary = Core::getPHPBinary() ?: Console\Processus::localte('php');
151 
152  $envVariable = '__HOA_ATOUM_PRASPEL_EXTENSION_' . md5(Core::uuid());
153  $reflection = null;
154  $buffer = null;
155  $reflectionner = new Console\Processus($phpBinary);
156  $reflectionner->on('input', function (Core\Event\Bucket $bucket) use ($envVariable) {
157  $bucket->getSource()->writeAll(
158  '<?php' . "\n" .
159  'require_once \'' . dirname(__DIR__) . DS . '.bootstrap.atoum.php\';' . "\n" .
160  '$class = getenv(\'' . $envVariable . '\');' . "\n" .
161  'if (class_exists(\'\mageekguy\atoum\scripts\runner\', false)) {' . "\n" .
162  ' \atoum\scripts\runner::disableAutorun();' . "\n" .
163  '}' . "\n" .
164  '$reflection = new \Atoum\PraspelExtension\Praspel\Reflection\RClass($class);' . "\n" .
165  'echo serialize($reflection), "\n";'
166  );
167 
168  return false;
169  });
170  $reflectionner->on('output', function (Core\Event\Bucket $bucket) use (&$buffer) {
171  $data = $bucket->getData();
172  $buffer .= $data['line'] . "\n";
173 
174  return;
175  });
176  $reflectionner->on('stop', function () use (&$buffer, &$reflection) {
177  $handle = @unserialize($buffer);
178 
179  if (false === $handle) {
180  echo $buffer, "\n";
181 
182  return;
183  }
184 
185  $reflection = $handle;
186 
187  return;
188  });
189 
190  foreach ($classes as $class) {
191  $status = $class . ' (in ';
192  echo ' ⌛ ' , $status;
193 
194  putenv($envVariable . '=' . $class);
195  $buffer = null;
196  $reflection = null;
197  $reflectionner->run();
198  $output = $generator->generate($reflection);
199 
200  $parts = explode('\\', $class);
201  $paths = resolve(
202  'hoa://Library/' .
203  $parts[1] . '/' .
204  'Test/Praspel/Unit/' .
205  implode(
206  '/',
207  array_slice($parts, 2)
208  ) .
209  '.php',
210  false,
211  true
212  );
213 
214  $max = 0;
215  $thePath = 0;
216 
217  foreach ($paths as $path) {
218  $length = Ustring\Search::lcp(
219  $reflection->getFilename(),
220  $path
221  );
222 
223  if ($length > $max) {
224  $thePath = $path;
225  }
226  }
227 
228  $statusTail =
229  (40 < strlen($thePath)
230  ? '…' . substr($thePath, -39)
231  : $thePath) . ')';
232  echo $statusTail;
233  $status .= $statusTail;
234 
235  if (false === $reflection->isInstantiable()) {
236  Console\Cursor::clear('↔');
237  echo ' ⚡️ ', $status, '; not instantiable.', "\n";
238 
239  continue;
240  }
241 
242  $dirname = dirname($thePath);
243 
244  if (false === is_dir($dirname)) {
245  if (false === $dryRun) {
246  mkdir($dirname, 0755, true);
247  } else {
248  echo
249  "\n",
250  static::info('Creating directory: ' . $dirname . '.');
251  }
252  }
253 
254  if (false === $dryRun) {
255  file_put_contents($thePath, $output);
256  } else {
257  echo
258  "\n",
259  static::info('Content of the ' . $thePath . ':'),
260  "\n";
261  Console\Cursor::colorize('foreground(yellow)');
262  echo
263  ' ┏', "\n",
264  ' ┃ ' ,
265  str_replace(
266  "\n",
267  "\n" . ' ┃ ',
268  trim($output)
269  ),
270  "\n",
271  ' ┗', "\n";
272  Console\Cursor::colorize('foreground(normal)');
273  }
274 
275  Console\Cursor::clear('↔');
276  echo
277  ' ', Console\Chrome\Text::colorize('✔︎', 'foreground(green)'),
278  ' ', $status, "\n";
279  }
280 
281  return;
282  }
static colorize($attributes)
Definition: Cursor.php:379
static uuid()
Definition: Core.php:444
static getPHPBinary()
Definition: Core.php:420
static clear($parts= 'all')
Definition: Cursor.php:271
static lcp($x, $y)
Definition: Search.php:104
getOption(&$optionValue, $short=null)
Definition: Kit.php:104
resolveOptionAmbiguity(Array $solutions)
Definition: Kit.php:190
static colorize($text, $attributesBefore, $attributesAfter= 'normal')
Definition: Text.php:83

Here is the call graph for this function:

Hoa\Test\Bin\Generate::usage ( )

The command usage.

Returns
int

Definition at line 289 of file Generate.php.

290  {
291  echo
292  'Usage : test:generate <options>', "\n",
293  'Options :', "\n",
294  $this->makeUsageOptionsList([
295  'n' => 'Generate tests of some namespaces.',
296  'c' => 'Generate tests of some classes.',
297  'd' => 'Generate tests but output them instead of save ' .
298  'them.',
299  'help' => 'This help.'
300  ]), "\n";
301 
302  return;
303  }
makeUsageOptionsList(Array $definitions=[])
Definition: Kit.php:149

Here is the call graph for this function:

Here is the caller graph for this function:

Member Data Documentation

Hoa\Test\Bin\Generate::$options
protected
Initial value:

Definition at line 61 of file Generate.php.


The documentation for this class was generated from the following file: