Drupal8.x and Symfony To the Aid Experiences: Part I Series

Screenshot 2014-05-21 00.45.27

Ok, so let’s do this. I decided to take a stab go into one of the tweets from jmolivas and picked a link and bam it was this module on drupal8x.

So i had already cloned drupal into a folder and had ran “`php -S localhost:8000“` and had gone through the installation on the web, they call it installation script which makes no sense :).

Then I had my drupal blue screen. The i was kindly oriented by yesCT and others in the IRC to plug some more modules, these are drag and drop. I know it sucks but once you download one and move a folder under modules you start to think is not too bad. We don’t do that in Symfony, is almost sinful, but bearing up with drupal legacy we just do this on this part of planet earth. So i moved the folder for the webprofile, that was neat and discovered they had a similar WDT like in Symfony. The same happen with a dependency and how they enable modules. Rather than running composer install or update package, you basically move the folder under “`modules“` and enable it in under “`Extend“` drop down on the navigation bar.

So this is the link of the repo i found in jmolivas tweet https://github.com/dmouse/vimeo_field.
What i found to my surprise was a test which was mocking Drupal core classes. The first thing that it striked me was, why would you mock all classes that you extend from your actual class under test. Well it turns out that the tests did not have a proper autoloader. I had two options, load as a require-dev drupal/drupal, or sourcing the autoload from drupal root directory and merge it with the current autoloader in a boostrap.php file. I opted for the first so that travis can work of course. At first i was looked as a rare bird by some drupalers but I went my own way knowing my theory.

    protected function setUp()
    {
        $this->formmaterItemListInterface = $this->getMockBuilder('Drupal\Core\Field\FieldItemListInterface')
            ->disableOriginalConstructor()
            ->getMock();
        $this->formmaterBase = $this->getMockBuilder('Drupal\Core\Field\FormatterBase')
            ->setMethods([])
            ->disableOriginalConstructor()
            ->getMock();
 
        $this->formatter = $this->getMockBuilder('Drupal\vimeo_field\Plugin\Field\FieldFormatter\VimeoDefaultFormatter')
            ->disableOriginalConstructor()
            ->setMethods(['t', 'getSetting'])
            ->getMock();
 
        $this->formatter->expects($this->any())
            ->method('t')
            ->with($this->isType('string'))
            ->will($this->returnCallback(function () {
                $args = func_get_args();
                return $args[0];
            }));

Notice the first mocks which have nothing to do in this test. The class variables were not even defined. But if you removed those lines you would get an error because they were there because phpunit would not know who the heck were those classes because the autoloader was not finding them. This is obviously a bad practice. Picture building a module very complex that depends on half of the classes on the core, poor you.

     "require-dev": {
 -        "phpunit/phpunit": "~4.1"
 +        "phpunit/phpunit": "~3.7",
 +        "drupal/drupal": "8.x-dev@dev"
      },

I have noticed also the test needs more integration tests, but for that more thinking is needed. For now is enough to include drupal/drupal as a dependency and make the testing of the module standalone.

After following the instructions and sending some PRs and comments I got the desired result 🙂

I also wanted to require-dev drush just in case but i rather enabled the bundle ehem module according to the instructions in the readme.

Drupal community needs our help as good PHP developers, especially if you know some Symfony it would help us a lot! now i can say i belong to two communities and is so cool!

Doing this work out of pure joy and very thankful for such undeserved favor, please enjoy too!

your friend @cordoval

Leave a Reply

Your email address will not be published. Required fields are marked *