|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Auxmoney\OpentracingBundle\Tests\Functional; |
| 6 | + |
| 7 | +use GuzzleHttp\Client; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\Filesystem\Filesystem; |
| 10 | +use Symfony\Component\Process\Process; |
| 11 | + |
| 12 | +class FunctionalTest extends TestCase |
| 13 | +{ |
| 14 | + public function testSuccessfulTracing(): void |
| 15 | + { |
| 16 | + $this->setupTestProject(); |
| 17 | + |
| 18 | + $p = new Process(['symfony', 'console', 'test:zipkin'], 'build/testproject'); |
| 19 | + $p->mustRun(); |
| 20 | + $traceId = trim($p->getOutput()); |
| 21 | + self::assertNotEmpty($traceId); |
| 22 | + |
| 23 | + $spans = $this->getTraceFromZipkinAPI($traceId); |
| 24 | + self::assertCount(1, $spans); |
| 25 | + self::assertSame('test:zipkin', $spans[0]['name']); |
| 26 | + } |
| 27 | + |
| 28 | + public function setUp() |
| 29 | + { |
| 30 | + parent::setUp(); |
| 31 | + |
| 32 | + $p = new Process(['docker', 'start', 'zipkin']); |
| 33 | + $p->mustRun(); |
| 34 | + |
| 35 | + sleep(10); |
| 36 | + } |
| 37 | + |
| 38 | + protected function tearDown() |
| 39 | + { |
| 40 | + $p = new Process(['git', 'reset', '--hard', 'reset'], 'build/testproject'); |
| 41 | + $p->mustRun(); |
| 42 | + $p = new Process(['docker', 'stop', 'zipkin']); |
| 43 | + $p->mustRun(); |
| 44 | + |
| 45 | + parent::tearDown(); |
| 46 | + } |
| 47 | + |
| 48 | + private function getTraceFromZipkinAPI(string $traceId): array |
| 49 | + { |
| 50 | + $client = new Client(); |
| 51 | + $response = $client->get(sprintf('http://localhost:9411/zipkin/api/v2/trace/%s', $traceId)); |
| 52 | + return json_decode($response->getBody()->getContents(), true); |
| 53 | + } |
| 54 | + |
| 55 | + private function setupTestProject(): void |
| 56 | + { |
| 57 | + $filesystem = new Filesystem(); |
| 58 | + $filesystem->mirror(sprintf('Tests/Functional/TestProjectFiles/default/'), 'build/testproject/'); |
| 59 | + |
| 60 | + $p = new Process(['composer', 'dump-autoload'], 'build/testproject'); |
| 61 | + $p->mustRun(); |
| 62 | + $p = new Process(['symfony', 'console', 'cache:clear'], 'build/testproject'); |
| 63 | + $p->mustRun(); |
| 64 | + } |
| 65 | +} |
0 commit comments