|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace VCR\VCRBundle\Test; |
| 5 | + |
| 6 | +use Psr\Container\ContainerInterface; |
| 7 | +use Symfony\Bundle\FrameworkBundle\KernelBrowser; |
| 8 | +use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
| 9 | +use VCR\VCRBundle\VideoRecorderBrowser; |
| 10 | +use VCR\Videorecorder; |
| 11 | + |
| 12 | +/** |
| 13 | + * Trait providing helper functions to work with \PHPUnit\Framework\TestCase in combination with |
| 14 | + * \Symfony\Bundle\FrameworkBundle\Test\WebTestCase and the php-vcr library (&bundle). |
| 15 | + * |
| 16 | + * @method ?string getName(bool $withDataSet) |
| 17 | + * @property ?string $ignoredTestSuiteNamespacePrefix Set a namespace prefix which should be ignored while generating |
| 18 | + * the cassette path |
| 19 | + */ |
| 20 | +trait VCRTestCaseTrait |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var null|string |
| 24 | + */ |
| 25 | + protected $testSuiteName; |
| 26 | + |
| 27 | + /** |
| 28 | + * |
| 29 | + * |
| 30 | + * @return string The current test suite name |
| 31 | + */ |
| 32 | + public function getTestSuiteName(): string |
| 33 | + { |
| 34 | + $this->setUpTestSuiteName(); |
| 35 | + |
| 36 | + return $this->testSuiteName; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @beforeClass |
| 41 | + * |
| 42 | + * @return void |
| 43 | + */ |
| 44 | + protected function setUpTestSuiteName(): void |
| 45 | + { |
| 46 | + if (! $this->testSuiteName) { |
| 47 | + $testSuiteName = (new \ReflectionClass($this))->getName(); |
| 48 | + if ( |
| 49 | + ! empty($this->ignoredTestSuiteNamespacePrefix) && |
| 50 | + str_starts_with($testSuiteName, $this->ignoredTestSuiteNamespacePrefix) |
| 51 | + ) { |
| 52 | + $testSuiteName = str_replace($this->ignoredTestSuiteNamespacePrefix, '', $testSuiteName); |
| 53 | + } |
| 54 | + $testSuiteNameParts = array_filter(explode('\\', $testSuiteName)); |
| 55 | + $this->testSuiteName = implode(DIRECTORY_SEPARATOR, $testSuiteNameParts); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Normalize a video recorder cassette name. |
| 61 | + * |
| 62 | + * @param string $name The name to normalize |
| 63 | + * |
| 64 | + * @return string The normalized cassette name |
| 65 | + */ |
| 66 | + protected function normalizeVideoRecorderCassetteName(string $name): string |
| 67 | + { |
| 68 | + return preg_replace(['/\s+/', '/[^a-zA-Z0-9\-_]/'], ['-', ''], $name); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get the video recorder cassette name from the current test. |
| 73 | + * |
| 74 | + * @param null|string $suffix |
| 75 | + * @param bool $withDataSet With dataset |
| 76 | + * |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + protected function getVideoRecorderCassetteName(string $suffix = null, bool $withDataSet = true): string |
| 80 | + { |
| 81 | + $name = $this->getName($withDataSet); |
| 82 | + if (! empty($suffix)) { |
| 83 | + $name .= $suffix; |
| 84 | + } |
| 85 | + |
| 86 | + return $this->normalizeVideoRecorderCassetteName($name); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Enable VCR VideoRecorder. |
| 91 | + * |
| 92 | + * @param ContainerInterface $container |
| 93 | + * @param string|null $suffix |
| 94 | + * |
| 95 | + * @param bool $withDataSet |
| 96 | + * |
| 97 | + * @return void |
| 98 | + */ |
| 99 | + protected function enableVideoRecorder( |
| 100 | + ContainerInterface $container, |
| 101 | + string $suffix = null, |
| 102 | + bool $withDataSet = true |
| 103 | + ): void { |
| 104 | + $videoRecorder = $this->getVideoRecorder($container); |
| 105 | + $videoRecorder->turnOn(); |
| 106 | + $this->insertVideoRecorderCassette($container, $suffix, $withDataSet); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Insert cassette into VCR VideoRecorder. |
| 111 | + * |
| 112 | + * @param ContainerInterface $container |
| 113 | + * @param null|string $suffix |
| 114 | + * |
| 115 | + * @param bool $withDataSet |
| 116 | + * |
| 117 | + * @return void |
| 118 | + */ |
| 119 | + protected function insertVideoRecorderCassette( |
| 120 | + ContainerInterface $container, |
| 121 | + string $suffix = null, |
| 122 | + bool $withDataSet = true |
| 123 | + ): void { |
| 124 | + $name = $this->getVideoRecorderCassetteName($suffix, $withDataSet); |
| 125 | + $name = implode( |
| 126 | + DIRECTORY_SEPARATOR, |
| 127 | + [ |
| 128 | + $this->getTestSuiteName(), |
| 129 | + $name, |
| 130 | + ] |
| 131 | + ); |
| 132 | + |
| 133 | + $videoRecorder = $this->getVideoRecorder($container); |
| 134 | + |
| 135 | + $videoRecorder->insertCassette($name); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Insert default cassette into VCR VideoRecorder. |
| 140 | + * |
| 141 | + * @param ContainerInterface $container |
| 142 | + * |
| 143 | + * @return void |
| 144 | + */ |
| 145 | + protected function insertDefaultVideoRecorderCassette(ContainerInterface $container): void |
| 146 | + { |
| 147 | + $defaultCassetteName = $container->getParameter('vcr.cassette.name'); |
| 148 | + |
| 149 | + $this->getVideoRecorder($container)->insertCassette($defaultCassetteName); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Disable VCR VideoRecorder. |
| 154 | + * |
| 155 | + * @param ContainerInterface $container |
| 156 | + * |
| 157 | + * @return void |
| 158 | + */ |
| 159 | + protected function disableVideoRecorder(ContainerInterface $container): void |
| 160 | + { |
| 161 | + $this->getVideoRecorder($container)->turnOff(); |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * @param ContainerInterface $container |
| 166 | + * |
| 167 | + * @return Videorecorder |
| 168 | + */ |
| 169 | + protected function getVideoRecorder(ContainerInterface $container): Videorecorder |
| 170 | + { |
| 171 | + return $container->get('vcr.recorder'); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Creates a Client supports the VideoRecorder framework. |
| 176 | + * |
| 177 | + * @param array $options An array of options to pass to the createKernel method |
| 178 | + * @param array $server An array of server parameters |
| 179 | + * @param null|string $suffix |
| 180 | + * @param bool $withDataSet |
| 181 | + * |
| 182 | + * @return VideoRecorderBrowser A KernelBrowser instance |
| 183 | + */ |
| 184 | + protected function createVideoRecorderClient( |
| 185 | + array $options = [], |
| 186 | + array $server = [], |
| 187 | + string $suffix = null, |
| 188 | + bool $withDataSet = true |
| 189 | + ) { |
| 190 | + if (! \method_exists(\get_called_class(), 'createClient')) { |
| 191 | + throw new \LogicException( |
| 192 | + 'Current test case has no static method to createClient(array $options, array $server).' |
| 193 | + ); |
| 194 | + } |
| 195 | + |
| 196 | + /** @var KernelBrowser $client */ |
| 197 | + $client = static::createClient($options, $server); |
| 198 | + try { |
| 199 | + /** @var VideoRecorderBrowser $client */ |
| 200 | + $client = $client->getKernel()->getContainer()->get('test.client.vcr'); |
| 201 | + } catch (ServiceNotFoundException $e) { |
| 202 | + throw new \LogicException('VideoRecorderBrowser not loaded. Did you enable the this bundle?'); |
| 203 | + } |
| 204 | + |
| 205 | + $client->setVideoRecorderCassetteBasePath($this->getTestSuiteName()); |
| 206 | + $client->enableVideoRecorder(); |
| 207 | + $client->insertVideoRecorderCassette($this->getVideoRecorderCassetteName($suffix, $withDataSet)); |
| 208 | + |
| 209 | + return $client; |
| 210 | + } |
| 211 | +} |
0 commit comments