How to use TransformationExtension class

Best Behat code snippet using .TransformationExtension

Run Behat automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

BehatContainerTrait.php

Source: BehatContainerTrait.php Github

copy
1<?php
2
3namespace PHPUnitBehat\TestTraits;
4
5use Behat\Testwork\ServiceContainer\ExtensionManager;
6use Behat\Testwork\ServiceContainer\ContainerLoader;
7use Symfony\Component\DependencyInjection\ContainerBuilder;
8use Symfony\Component\Console\Input\ArrayInput;
9use Symfony\Component\Console\Output\NullOutput;
10use Symfony\Component\DependencyInjection\Definition;
11
12use Behat\Behat\Context\ServiceContainer\ContextExtension;
13use Behat\Behat\Definition\ServiceContainer\DefinitionExtension;
14use Behat\Behat\EventDispatcher\ServiceContainer\EventDispatcherExtension;
15use Behat\Behat\Gherkin\ServiceContainer\GherkinExtension;
16use Behat\Behat\Hook\ServiceContainer\HookExtension;
17use Behat\Behat\Output\ServiceContainer\Formatter\JUnitFormatterFactory;
18use Behat\Behat\Output\ServiceContainer\Formatter\PrettyFormatterFactory;
19use Behat\Behat\Output\ServiceContainer\Formatter\ProgressFormatterFactory;
20use Behat\Behat\HelperContainer\ServiceContainer\HelperContainerExtension;
21use Behat\Behat\Snippet\ServiceContainer\SnippetExtension;
22use Behat\Behat\Tester\ServiceContainer\TesterExtension;
23use Behat\Behat\Transformation\ServiceContainer\TransformationExtension;
24use Behat\Behat\Translator\ServiceContainer\GherkinTranslationsExtension;
25use Behat\Testwork\Argument\ServiceContainer\ArgumentExtension;
26use Behat\Testwork\Autoloader\ServiceContainer\AutoloaderExtension;
27use Behat\Testwork\Call\ServiceContainer\CallExtension;
28use Behat\Testwork\Cli\ServiceContainer\CliExtension;
29use Behat\Testwork\Environment\ServiceContainer\EnvironmentExtension;
30use Behat\Testwork\Exception\ServiceContainer\ExceptionExtension;
31use Behat\Testwork\Filesystem\ServiceContainer\FilesystemExtension;
32use Behat\Testwork\Ordering\ServiceContainer\OrderingExtension;
33use Behat\Testwork\Output\ServiceContainer\Formatter\FormatterFactory;
34use Behat\Testwork\Output\ServiceContainer\OutputExtension;
35use Behat\Testwork\ServiceContainer\ServiceProcessor;
36use Behat\Testwork\Specification\ServiceContainer\SpecificationExtension;
37use Behat\Testwork\Suite\ServiceContainer\SuiteExtension;
38use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
39
40
41
42trait BehatContainerTrait  {
43
44    protected static $behatContainer;
45
46    // see https://dzone.com/articles/practical-php-testing/practical-php-testing-patterns-23
47    // If this trait is used in a base test class that many test classes extend, then
48    // the container will only be built once during a phpUnit execution, not once
49    // per test class, because ::behatContainer is a static property.   
50    protected function getBehatContainer() {
51      if (self::$behatContainer === null) {
52        $this->setBehatContainer();
53      }  
54      return self::$behatContainer;
55    } 
56
57  protected function setBehatContainer() {
58    // Create the container.
59    $extensionManager = new ExtensionManager($this->getBehatExtensions());
60    $containerLoader = new ContainerLoader($extensionManager);
61    $containerBuilder = new ContainerBuilder;
62
63    // Provide basic parameters required by Behat, even though they make no sense in PhpUnit.
64    $containerBuilder->setParameter('paths.base', '');
65    $containerBuilder->set('cli.input', new ArrayInput([]));
66    $containerBuilder->set('cli.output', new NullOutput());
67
68    // Add the PhpUnit behat environment handler.
69    $definition = new Definition('PHPUnitBehat\Behat\Testwork\Environment\Handler\PHPUnitEnvironmentHandler');
70    $definition->addTag('environment.handler', array('priority' => 0));
71    $containerBuilder->setDefinition('environment.handler.phpunit', $definition);
72
73    // Finalise the container.
74    $containerLoader->load($containerBuilder, []);
75    $containerBuilder->addObjectResource($containerLoader);
76    $containerBuilder->compile();
77    self::$behatContainer = $containerBuilder;
78  }
79
80
81  /**
82   * Get an array of Behat extensions.
83   * 
84   * These have all been verified as needed for our purposes, except
85   * GherkinTranslationExtension, HookExtension and TransformationExtension 
86   * which simply look like they might be useful.
87   * 
88   * They are all heavily interdependent.
89   *
90   * @return array
91   */
92  protected function getBehatExtensions()
93  {
94      // The commented out lines are Behat default extensions which we don't need.
95      $processor = new \Behat\Testwork\ServiceContainer\ServiceProcessor();
96      return array(
97        new ArgumentExtension(),
98        new AutoloaderExtension(array('' => '%paths.base%/features/bootstrap')),
99        new SuiteExtension($processor),
100//        new ExceptionExtension($processor),
101        new GherkinExtension($processor),
102        new CallExtension($processor),
103        new TranslatorExtension(),
104        new GherkinTranslationsExtension(),
105        new TesterExtension($processor),
106//        new CliExtension($processor),
107        new EnvironmentExtension($processor),
108        new SpecificationExtension($processor),
109        new FilesystemExtension(),
110        new ContextExtension($processor),
111//        new SnippetExtension($processor),
112        new DefinitionExtension($processor),
113        new EventDispatcherExtension($processor),
114        new HookExtension(),
115        new TransformationExtension($processor),
116 //       new OrderingExtension($processor),
117//        new HelperContainerExtension($processor)
118    );
119  }
120
121}
122
Full Screen

PlaceholderExtension.php

Source: PlaceholderExtension.php Github

copy
1<?php
2declare(strict_types = 1);
3
4namespace espend\Behat\PlaceholderExtension;
5
6use Behat\Behat\Context\ServiceContainer\ContextExtension;
7use Behat\Behat\EventDispatcher\ServiceContainer\EventDispatcherExtension;
8use Behat\Behat\Transformation\ServiceContainer\TransformationExtension;
9use Behat\Testwork\ServiceContainer\Extension;
10use Behat\Testwork\ServiceContainer\ExtensionManager;
11use espend\Behat\PlaceholderExtension\Context\Initializer\PlaceholderBagAwareInitializer;
12use espend\Behat\PlaceholderExtension\Subscriber\ScenarioClearListener;
13use espend\Behat\PlaceholderExtension\Transformer\PlaceholderArgumentTransformer;
14use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15use Symfony\Component\DependencyInjection\ContainerBuilder;
16use Symfony\Component\DependencyInjection\Reference;
17
18/**
19 * @author Daniel Espendiller <daniel@espendiller.net>
20 */
21class PlaceholderExtension implements Extension
22{
23    /**
24     * {@inheritdoc}
25     */
26    public function process(ContainerBuilder $container)
27    {
28    }
29
30    /**
31     * {@inheritdoc}
32     */
33    public function getConfigKey()
34    {
35        return 'placeholder';
36    }
37
38    /**
39     * {@inheritdoc}
40     */
41    public function initialize(ExtensionManager $extensionManager)
42    {
43    }
44
45    /**
46     * {@inheritdoc}
47     */
48    public function configure(ArrayNodeDefinition $builder)
49    {
50        $builder
51            ->addDefaultsIfNotSet()
52            ->children()
53                ->scalarNode('mail_randomize_template')
54                    ->defaultValue('behat-%random%@example.com')
55                    ->end()
56                ->end()
57            ->end();
58    }
59
60    /**
61     * {@inheritdoc}
62     */
63    public function load(ContainerBuilder $container, array $config)
64    {
65        $container->register('espend.behat.placeholder_extension.placeholder_bag', PlaceholderBag::class);
66
67        $container
68            ->register(
69                'espend.behat.placeholder_extension.subscriber.scenario_clear_listener',
70                ScenarioClearListener::class
71            )
72            ->addTag(EventDispatcherExtension::SUBSCRIBER_TAG)
73            ->addArgument(new Reference('espend.behat.placeholder_extension.placeholder_bag'));
74
75        $this->loadPlaceholdersTransformer($container);
76        $this->loadContextInitializer($container);
77    }
78
79    /**
80     * Loads transformers
81     *
82     * @param ContainerBuilder $container
83     */
84    private function loadPlaceholdersTransformer(ContainerBuilder $container)
85    {
86        $container
87            ->register(
88                'espend.behat.placeholder_extension.transformer.placeholder_argument_transformer',
89                PlaceholderArgumentTransformer::class
90            )
91            ->addArgument(new Reference('espend.behat.placeholder_extension.placeholder_bag'))
92            ->addTag(TransformationExtension::ARGUMENT_TRANSFORMER_TAG, ['priority' => 1000]);
93    }
94
95    /**
96     * @param ContainerBuilder $container
97     */
98    private function loadContextInitializer(ContainerBuilder $container)
99    {
100        $container
101            ->register(
102                'espend.behat.placeholder_extension.context.initializer.placeholder_bag_aware_initializer',
103                PlaceholderBagAwareInitializer::class
104            )
105            ->addArgument(new Reference('espend.behat.placeholder_extension.placeholder_bag'))
106            ->addTag(ContextExtension::INITIALIZER_TAG);
107    }
108}
109
Full Screen

Transformation.php

Source: Transformation.php Github

copy
1<?php
2
3class Transformation
4{
5
6    /**
7     * @var Vector $Translate
8     */
9    protected $Translate = null;
10
11    /**
12     * @var Vector $Scale
13     */
14    protected $Scale = null;
15
16    /**
17     * @var TransformationExtension $Extension
18     */
19    protected $Extension = null;
20
21    
22    public function __construct()
23    {
24    
25    }
26
27    /**
28     * @return Vector
29     */
30    public function getTranslate()
31    {
32      return $this->Translate;
33    }
34
35    /**
36     * @param Vector $Translate
37     * @return Transformation
38     */
39    public function setTranslate($Translate)
40    {
41      $this->Translate = $Translate;
42      return $this;
43    }
44
45    /**
46     * @return Vector
47     */
48    public function getScale()
49    {
50      return $this->Scale;
51    }
52
53    /**
54     * @param Vector $Scale
55     * @return Transformation
56     */
57    public function setScale($Scale)
58    {
59      $this->Scale = $Scale;
60      return $this;
61    }
62
63    /**
64     * @return TransformationExtension
65     */
66    public function getExtension()
67    {
68      return $this->Extension;
69    }
70
71    /**
72     * @param TransformationExtension $Extension
73     * @return Transformation
74     */
75    public function setExtension($Extension)
76    {
77      $this->Extension = $Extension;
78      return $this;
79    }
80
81}
82
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)