|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Qameta\Allure\Test\Legacy\Support; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Qameta\Allure\Allure; |
| 9 | +use Qameta\Allure\AllureLifecycleInterface; |
| 10 | +use Qameta\Allure\Io\DataSourceInterface; |
| 11 | +use Qameta\Allure\Model\AttachmentResult; |
| 12 | +use Qameta\Allure\Model\ResultFactoryInterface; |
| 13 | +use Qameta\Allure\Setup\LifecycleBuilderInterface; |
| 14 | +use Yandex\Allure\Adapter\Support\AttachmentSupport; |
| 15 | + |
| 16 | +use function fclose; |
| 17 | +use function fread; |
| 18 | + |
| 19 | +/** |
| 20 | + * @covers \Yandex\Allure\Adapter\Support\AttachmentSupport |
| 21 | + */ |
| 22 | +class AttachmentSupportTest extends TestCase |
| 23 | +{ |
| 24 | + |
| 25 | + public function setUp(): void |
| 26 | + { |
| 27 | + Allure::reset(); |
| 28 | + } |
| 29 | + |
| 30 | + public function testAddAttachment_ResultFactoryProvidesAttachment_LifecycleAddsSameAttachment(): void |
| 31 | + { |
| 32 | + $attachment = new AttachmentResult('a'); |
| 33 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 34 | + Allure::setLifecycleBuilder( |
| 35 | + $this->createLifecycleBuilder( |
| 36 | + $this->createResultFactoryWithAttachment($attachment), |
| 37 | + $lifecycle, |
| 38 | + ), |
| 39 | + ); |
| 40 | + Allure::setOutputDirectory('b'); |
| 41 | + |
| 42 | + /** |
| 43 | + * @psalm-suppress DeprecatedTrait |
| 44 | + */ |
| 45 | + $object = new class () { |
| 46 | + use AttachmentSupport; |
| 47 | + }; |
| 48 | + $lifecycle |
| 49 | + ->expects(self::once()) |
| 50 | + ->method('addAttachment') |
| 51 | + ->with(self::identicalTo($attachment), self::isInstanceOf(DataSourceInterface::class)); |
| 52 | + $object->addAttachment('c', 'd'); |
| 53 | + } |
| 54 | + |
| 55 | + public function testAddAttachment_GivenCaption_AttachmentHasSameName(): void |
| 56 | + { |
| 57 | + $attachment = new AttachmentResult('a'); |
| 58 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 59 | + Allure::setLifecycleBuilder( |
| 60 | + $this->createLifecycleBuilder( |
| 61 | + $this->createResultFactoryWithAttachment($attachment), |
| 62 | + $lifecycle, |
| 63 | + ), |
| 64 | + ); |
| 65 | + Allure::setOutputDirectory('b'); |
| 66 | + |
| 67 | + /** |
| 68 | + * @psalm-suppress DeprecatedTrait |
| 69 | + */ |
| 70 | + $object = new class () { |
| 71 | + use AttachmentSupport; |
| 72 | + }; |
| 73 | + $object->addAttachment('c', 'd'); |
| 74 | + self::assertSame('d', $attachment->getName()); |
| 75 | + } |
| 76 | + |
| 77 | + public function testAddAttachment_NoTypeGiven_AttachmentHasNullType(): void |
| 78 | + { |
| 79 | + $attachment = new AttachmentResult('a'); |
| 80 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 81 | + Allure::setLifecycleBuilder( |
| 82 | + $this->createLifecycleBuilder( |
| 83 | + $this->createResultFactoryWithAttachment($attachment), |
| 84 | + $lifecycle, |
| 85 | + ), |
| 86 | + ); |
| 87 | + Allure::setOutputDirectory('b'); |
| 88 | + |
| 89 | + /** |
| 90 | + * @psalm-suppress DeprecatedTrait |
| 91 | + */ |
| 92 | + $object = new class () { |
| 93 | + use AttachmentSupport; |
| 94 | + }; |
| 95 | + $object->addAttachment('c', 'd'); |
| 96 | + self::assertNull($attachment->getType()); |
| 97 | + } |
| 98 | + |
| 99 | + public function testAddAttachment_GivenType_AttachmentHasSameType(): void |
| 100 | + { |
| 101 | + $attachment = new AttachmentResult('a'); |
| 102 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 103 | + Allure::setLifecycleBuilder( |
| 104 | + $this->createLifecycleBuilder( |
| 105 | + $this->createResultFactoryWithAttachment($attachment), |
| 106 | + $lifecycle, |
| 107 | + ), |
| 108 | + ); |
| 109 | + Allure::setOutputDirectory('b'); |
| 110 | + |
| 111 | + /** |
| 112 | + * @psalm-suppress DeprecatedTrait |
| 113 | + */ |
| 114 | + $object = new class () { |
| 115 | + use AttachmentSupport; |
| 116 | + }; |
| 117 | + $object->addAttachment('c', 'd', 'e'); |
| 118 | + self::assertSame('e', $attachment->getType()); |
| 119 | + } |
| 120 | + |
| 121 | + public function testAddAttachment_GivenFile_FileContentPassedToLifecycle(): void |
| 122 | + { |
| 123 | + $attachment = new AttachmentResult('a'); |
| 124 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 125 | + Allure::setLifecycleBuilder( |
| 126 | + $this->createLifecycleBuilder( |
| 127 | + $this->createResultFactoryWithAttachment($attachment), |
| 128 | + $lifecycle, |
| 129 | + ), |
| 130 | + ); |
| 131 | + Allure::setOutputDirectory('b'); |
| 132 | + |
| 133 | + /** |
| 134 | + * @psalm-suppress DeprecatedTrait |
| 135 | + */ |
| 136 | + $object = new class () { |
| 137 | + use AttachmentSupport; |
| 138 | + }; |
| 139 | + $lifecycle |
| 140 | + ->expects(self::once()) |
| 141 | + ->method('addAttachment') |
| 142 | + ->with( |
| 143 | + self::anything(), |
| 144 | + self::callback( |
| 145 | + function (DataSourceInterface $dataSource): bool { |
| 146 | + $resource = $dataSource->createStream(); |
| 147 | + try { |
| 148 | + $data = fread($resource, 5); |
| 149 | + } finally { |
| 150 | + fclose($resource); |
| 151 | + } |
| 152 | + |
| 153 | + return '<?php' === $data; |
| 154 | + } |
| 155 | + ), |
| 156 | + ); |
| 157 | + |
| 158 | + $object->addAttachment(__FILE__, 'd'); |
| 159 | + } |
| 160 | + |
| 161 | + public function testAddAttachment_GivenString_StringContentPassedToLifecycle(): void |
| 162 | + { |
| 163 | + $attachment = new AttachmentResult('a'); |
| 164 | + $lifecycle = $this->createMock(AllureLifecycleInterface::class); |
| 165 | + Allure::setLifecycleBuilder( |
| 166 | + $this->createLifecycleBuilder( |
| 167 | + $this->createResultFactoryWithAttachment($attachment), |
| 168 | + $lifecycle, |
| 169 | + ), |
| 170 | + ); |
| 171 | + Allure::setOutputDirectory('b'); |
| 172 | + |
| 173 | + /** |
| 174 | + * @psalm-suppress DeprecatedTrait |
| 175 | + */ |
| 176 | + $object = new class () { |
| 177 | + use AttachmentSupport; |
| 178 | + }; |
| 179 | + $lifecycle |
| 180 | + ->expects(self::once()) |
| 181 | + ->method('addAttachment') |
| 182 | + ->with( |
| 183 | + self::anything(), |
| 184 | + self::callback( |
| 185 | + function (DataSourceInterface $dataSource): bool { |
| 186 | + $resource = $dataSource->createStream(); |
| 187 | + try { |
| 188 | + $data = fread($resource, 4); |
| 189 | + } finally { |
| 190 | + fclose($resource); |
| 191 | + } |
| 192 | + |
| 193 | + return 'cdef' === $data; |
| 194 | + } |
| 195 | + ), |
| 196 | + ); |
| 197 | + |
| 198 | + $object->addAttachment('cdef', 'g'); |
| 199 | + } |
| 200 | + |
| 201 | + private function createLifecycleBuilder( |
| 202 | + ?ResultFactoryInterface $resultFactory = null, |
| 203 | + ?AllureLifecycleInterface $lifecycle = null, |
| 204 | + ): LifecycleBuilderInterface { |
| 205 | + $builder = $this->createStub(LifecycleBuilderInterface::class); |
| 206 | + if (isset($resultFactory)) { |
| 207 | + $builder |
| 208 | + ->method('getResultFactory') |
| 209 | + ->willReturn($resultFactory); |
| 210 | + } |
| 211 | + if (isset($lifecycle)) { |
| 212 | + $builder |
| 213 | + ->method('createLifecycle') |
| 214 | + ->willReturn($lifecycle); |
| 215 | + } |
| 216 | + |
| 217 | + return $builder; |
| 218 | + } |
| 219 | + |
| 220 | + private function createResultFactoryWithAttachment(AttachmentResult $attachment): ResultFactoryInterface |
| 221 | + { |
| 222 | + $resultFactory = $this->createStub(ResultFactoryInterface::class); |
| 223 | + $resultFactory |
| 224 | + ->method('createAttachment') |
| 225 | + ->willReturn($attachment); |
| 226 | + |
| 227 | + return $resultFactory; |
| 228 | + } |
| 229 | +} |
0 commit comments