How to use BadOutputPathException class

Best Behat code snippet using .BadOutputPathException

Run Behat automation tests on LambdaTest cloud grid

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

Printer.php

Source: Printer.php Github

copy
1<?php
2
3namespace BehatJUnitExtension;
4
5use Behat\Testwork\Output\Exception\BadOutputPathException;
6use Behat\Testwork\Output\Printer\OutputPrinter;
7
8class Printer implements OutputPrinter
9{
10    /**
11     * @var string
12     */
13    private $path;
14
15    /**
16     * @var string
17     */
18    private $fileName;
19
20    /**
21     * Sets file name.
22     *
23     * @param string $fileName
24     * @param string $extension The file extension, defaults to "xml"
25     */
26    public function setFileName($fileName, $extension = 'xml')
27    {
28        if ('.'.$extension !== substr($fileName, strlen($extension) + 1)) {
29            $fileName .= '.'.$extension;
30        }
31
32        $this->fileName = $fileName;
33    }
34
35    /**
36     * {@inheritdoc}
37     */
38    public function setOutputPath($path)
39    {
40        $realPath = realpath($path);
41
42        if ($realPath === false) {
43            throw new BadOutputPathException('Directory expected for the `outputDir` option, but a non-existent or invalid path was given.', $path);
44        }
45        if (!is_dir($realPath)) {
46            throw new BadOutputPathException('Directory expected for the `outputDir` option, but a filename was given.', $path);
47        }
48        $this->path = $realPath;
49    }
50
51    /**
52     * {@inheritdoc}
53     */
54    public function getOutputPath()
55    {
56        return $this->path;
57    }
58
59    /**
60     * {@inheritdoc}
61     */
62    public function setOutputStyles(array $styles)
63    {
64    }
65
66    /**
67     * {@inheritdoc}
68     */
69    public function getOutputStyles()
70    {
71        return [];
72    }
73
74    /**
75     * {@inheritdoc}
76     */
77    public function setOutputDecorated($decorated)
78    {
79    }
80
81    /**
82     * {@inheritdoc}
83     */
84    public function isOutputDecorated()
85    {
86        return true;
87    }
88
89    /**
90     * {@inheritdoc}
91     */
92    public function setOutputVerbosity($level)
93    {
94    }
95
96    /**
97     * {@inheritdoc}
98     */
99    public function getOutputVerbosity()
100    {
101        return 0;
102    }
103
104    /**
105     * {@inheritdoc}
106     */
107    public function write($messages)
108    {
109        $outputPath = $this->getOutputPath();
110        if (empty($outputPath)) {
111            throw new \RuntimeException('Output path for moodle_junit format is missing, please specify one with the -o option');
112        }
113
114        $file = $outputPath.DIRECTORY_SEPARATOR.$this->fileName;
115        file_put_contents($file, $messages);
116    }
117
118    /**
119     * {@inheritdoc}
120     */
121    public function writeln($messages = '')
122    {
123    }
124
125    /**
126     * {@inheritdoc}
127     */
128    public function flush()
129    {
130    }
131}
132
Full Screen

ConsoleOutputFactory.php

Source: ConsoleOutputFactory.php Github

copy
1<?php
2
3/*
4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <[email protected]>
6 *
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
9 */
10
11namespace Behat\Testwork\Output\Printer\Factory;
12
13use Behat\Testwork\Output\Exception\BadOutputPathException;
14use Symfony\Component\Console\Formatter\OutputFormatter;
15use Symfony\Component\Console\Formatter\OutputFormatterStyle;
16use Symfony\Component\Console\Output\OutputInterface;
17use Symfony\Component\Console\Output\StreamOutput;
18
19/**
20 * Creates an output stream for the console.
21 *
22 * @author Wouter J <wouter@wouterj.nl>
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
24 */
25class ConsoleOutputFactory extends OutputFactory
26{
27    /**
28     * Creates output formatter that is used to create a stream.
29     *
30     * @return OutputFormatter
31     */
32    protected function createOutputFormatter()
33    {
34        return new OutputFormatter();
35    }
36
37    /**
38     * Configure output stream parameters.
39     *
40     * @param OutputInterface $output
41     */
42    protected function configureOutputStream(OutputInterface $output)
43    {
44        $verbosity = $this->getOutputVerbosity() ? OutputInterface::VERBOSITY_VERBOSE : OutputInterface::VERBOSITY_NORMAL;
45        $output->setVerbosity($verbosity);
46
47        if (null !== $this->isOutputDecorated()) {
48            $output->getFormatter()->setDecorated($this->isOutputDecorated());
49        }
50    }
51
52    /**
53     * Returns new output stream.
54     *
55     * Override this method & call flush() to write output in another stream
56     *
57     * @return resource
58     *
59     * @throws BadOutputPathException
60     */
61    protected function createOutputStream()
62    {
63        if (null === $this->getOutputPath()) {
64            $stream = fopen('php://stdout', 'w');
65        } elseif (!is_dir($this->getOutputPath())) {
66            $stream = fopen($this->getOutputPath(), 'w');
67        } else {
68            throw new BadOutputPathException(sprintf(
69                'Filename expected as `output_path` parameter, but got `%s`.',
70                $this->getOutputPath()
71            ), $this->getOutputPath());
72        }
73
74        return $stream;
75    }
76
77    /**
78     * {@inheritdoc}
79     */
80    public function createOutput($stream = null)
81    {
82        $stream = $stream ? : $this->createOutputStream();
83        $format = $this->createOutputFormatter();
84
85        // set user-defined styles
86        foreach ($this->getOutputStyles() as $name => $options) {
87            $style = new OutputFormatterStyle();
88
89            if (isset($options[0])) {
90                $style->setForeground($options[0]);
91            }
92            if (isset($options[1])) {
93                $style->setBackground($options[1]);
94            }
95            if (isset($options[2])) {
96                $style->setOptions($options[2]);
97            }
98
99            $format->setStyle($name, $style);
100        }
101
102        $output = new StreamOutput(
103            $stream,
104            StreamOutput::VERBOSITY_NORMAL,
105            $this->isOutputDecorated(),
106            $format
107        );
108        $this->configureOutputStream($output);
109
110        return $output;
111    }
112}
113
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

Most used methods in BadOutputPathException

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)