Drupal investigation

testCommandInfo.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace Consolidation\AnnotatedCommand;
  3. use Consolidation\AnnotatedCommand\Parser\CommandInfo;
  4. use Consolidation\AnnotatedCommand\Parser\CommandInfoSerializer;
  5. use Consolidation\AnnotatedCommand\Parser\CommandInfoDeserializer;
  6. class CommandInfoTests extends \PHPUnit_Framework_TestCase
  7. {
  8. function flattenArray($actualValue)
  9. {
  10. $result = [];
  11. foreach ($actualValue as $key => $value) {
  12. if (!is_string($value)) {
  13. $value = var_export($value, true);
  14. }
  15. $result[] = "{$key}=>{$value}";
  16. }
  17. return implode("\n", $result);
  18. }
  19. /**
  20. * Test CommandInfo command annotation parsing.
  21. */
  22. function testParsing()
  23. {
  24. $commandInfo = CommandInfo::create('\Consolidation\TestUtils\ExampleCommandFile', 'testArithmatic');
  25. $this->assertCommandInfoIsAsExpected($commandInfo);
  26. $serializer = new CommandInfoSerializer();
  27. $serialized = $serializer->serialize($commandInfo);
  28. $deserializer = new CommandInfoDeserializer();
  29. $deserializedCommandInfo = $deserializer->deserialize($serialized);
  30. $this->assertCommandInfoIsAsExpected($deserializedCommandInfo);
  31. }
  32. function assertCommandInfoIsAsExpected($commandInfo)
  33. {
  34. $this->assertEquals('test:arithmatic', $commandInfo->getName());
  35. $this->assertEquals(
  36. 'This is the test:arithmatic command',
  37. $commandInfo->getDescription()
  38. );
  39. $this->assertEquals(
  40. "This command will add one and two. If the --negate flag\nis provided, then the result is negated.",
  41. $commandInfo->getHelp()
  42. );
  43. $this->assertEquals('arithmatic', implode(',', $commandInfo->getAliases()));
  44. $this->assertEquals(
  45. '2 2 --negate=>Add two plus two and then negate.',
  46. $this->flattenArray($commandInfo->getExampleUsages())
  47. );
  48. $this->assertEquals(
  49. 'The first number to add.',
  50. $commandInfo->arguments()->getDescription('one')
  51. );
  52. $this->assertEquals(
  53. 'The other number to add.',
  54. $commandInfo->arguments()->getDescription('two')
  55. );
  56. $this->assertEquals(
  57. '2',
  58. $commandInfo->arguments()->get('two')
  59. );
  60. $this->assertEquals(
  61. 'Whether or not the result should be negated.',
  62. $commandInfo->options()->getDescription('negate')
  63. );
  64. $this->assertEquals(
  65. 'bob',
  66. $commandInfo->options()->get('unused')
  67. );
  68. $this->assertEquals(
  69. 'one,two',
  70. $commandInfo->getAnnotation('dup')
  71. );
  72. $this->assertEquals(
  73. ['one','two'],
  74. $commandInfo->getAnnotationList('dup')
  75. );
  76. }
  77. function testReturnValue()
  78. {
  79. $commandInfo = CommandInfo::create('\Consolidation\TestUtils\alpha\AlphaCommandFile', 'exampleTable');
  80. $this->assertEquals('example:table', $commandInfo->getName());
  81. $this->assertEquals('\Consolidation\OutputFormatters\StructuredData\RowsOfFields', $commandInfo->getReturnType());
  82. }
  83. }