3 namespace Drupal\Tests\drupalmoduleupgrader\Unit\Plugin\DMU\Fixer;
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;
15 class FormCallbackToMethodTest extends TestBase {
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 ]));
22 $class = ClassNode::create('FooForm');
23 $class_indexer = $this->getMock('\Drupal\drupalmoduleupgrader\IndexerInterface');
24 $class_indexer->method('get')->with('FooForm')->willReturn(new NodeCollection([ $class ]));
28 ->get('plugin.manager.drupalmoduleupgrader.indexer')
29 ->method('createInstance')
30 ->willReturnCallback(function($which) use ($class_indexer, $function_indexer) {
33 return $class_indexer;
35 return $function_indexer;
42 'callback' => 'foo_submit',
43 'destination' => 'FooForm::submitForm',
45 $plugin = new FormCallbackToMethod($config, uniqID(), []);
46 $plugin->setTarget($this->target);
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.
53 catch (IOException $e) {}
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());