Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / tests / src / Unit / Plugin / DMU / Fixer / FormCallbackToMethodTest.php
1 <?php
2
3 namespace Drupal\Tests\drupalmoduleupgrader\Unit\Plugin\DMU\Fixer;
4
5 use Drupal\drupalmoduleupgrader\IOException;
6 use Drupal\drupalmoduleupgrader\Plugin\DMU\Fixer\FormCallbackToMethod;
7 use Drupal\Tests\drupalmoduleupgrader\Unit\TestBase;
8 use Pharborist\NodeCollection;
9 use Pharborist\Objects\ClassNode;
10 use Pharborist\Parser;
11
12 /**
13  * @group DMU.Fixer
14  */
15 class FormCallbackToMethodTest extends TestBase {
16
17   public function test() {
18     $callback = Parser::parseSnippet('function foo_submit(&$form, &$form_state) {}');
19     $function_indexer = $this->getMock('\Drupal\drupalmoduleupgrader\IndexerInterface');
20     $function_indexer->method('get')->with('foo_submit')->willReturn(new NodeCollection([ $callback ]));
21
22     $class = ClassNode::create('FooForm');
23     $class_indexer = $this->getMock('\Drupal\drupalmoduleupgrader\IndexerInterface');
24     $class_indexer->method('get')->with('FooForm')->willReturn(new NodeCollection([ $class ]));
25
26     $this
27       ->container
28       ->get('plugin.manager.drupalmoduleupgrader.indexer')
29       ->method('createInstance')
30       ->willReturnCallback(function($which) use ($class_indexer, $function_indexer) {
31         switch ($which) {
32           case 'class':
33             return $class_indexer;
34           case 'function':
35             return $function_indexer;
36           default:
37             break;
38         }
39       });
40
41     $config = [
42       'callback' => 'foo_submit',
43       'destination' => 'FooForm::submitForm',
44     ];
45     $plugin = new FormCallbackToMethod($config, uniqID(), []);
46     $plugin->setTarget($this->target);
47     try {
48       // We expect a CodeManagerIOException because we're implementing the
49       // method on a class that is not officially part of the target's code.
50       // That's OK, though.
51       $plugin->execute();
52     }
53     catch (IOException $e) {}
54
55     $this->assertTrue($class->hasMethod('submitForm'));
56     $parameters = $class->getMethod('submitForm')->getParameters();
57     $this->assertCount(2, $parameters);
58     $this->assertEquals('form', $parameters[0]->getName());
59     $this->assertInstanceOf('\Pharborist\TokenNode', $parameters[0]->getTypeHint());
60     $this->assertSame(T_ARRAY, $parameters[0]->getTypeHint()->getType());
61     $this->assertInstanceOf('\Pharborist\TokenNode', $parameters[0]->getReference());
62     $this->assertEquals('form_state', $parameters[1]->getName());
63     $this->assertInstanceOf('\Pharborist\Namespaces\NameNode', $parameters[1]->getTypeHint());
64     $this->assertEquals('Drupal\Core\Form\FormStateInterface', $parameters[1]->getTypeHint()->getText());
65     $this->assertNull($parameters[1]->getReference());
66   }
67
68 }