|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace DrevOps\Vortex\Tests\Functional; |
| 6 | + |
| 7 | +use DrevOps\Vortex\Tests\Traits\CircleCiTrait; |
| 8 | +use PHPUnit\Framework\Attributes\Group; |
| 9 | + |
| 10 | +/** |
| 11 | + * Tests CircleCI post-build artifacts and test results. |
| 12 | + * |
| 13 | + * These tests verify that the build job properly generates and stores |
| 14 | + * artifacts and test results in CircleCI. |
| 15 | + */ |
| 16 | +class PostBuildTest extends FunctionalTestCase { |
| 17 | + |
| 18 | + use CircleCiTrait; |
| 19 | + |
| 20 | + /** |
| 21 | + * {@inheritdoc} |
| 22 | + */ |
| 23 | + protected function setUp(): void { |
| 24 | + if (empty(getenv('CIRCLECI'))) { |
| 25 | + $this->markTestSkipped('This test is only run on CircleCI'); |
| 26 | + } |
| 27 | + |
| 28 | + // Verify required environment variables are set. |
| 29 | + $this->assertNotEmpty(getenv('TEST_CIRCLECI_TOKEN'), 'CircleCI token is not set'); |
| 30 | + $this->assertNotEmpty(getenv('CIRCLE_PROJECT_REPONAME'), 'CircleCI project repo name is not set'); |
| 31 | + $this->assertNotEmpty(getenv('CIRCLE_PROJECT_USERNAME'), 'CircleCI project username is not set'); |
| 32 | + $this->assertNotEmpty(getenv('CIRCLE_BUILD_NUM'), 'CircleCI build number is not set'); |
| 33 | + |
| 34 | + // Skip the parent setUp as we don't need to prepare SUT for these tests. |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * {@inheritdoc} |
| 39 | + */ |
| 40 | + protected function tearDown(): void { |
| 41 | + // Skip parent tearDown as we didn't set up SUT. |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Test that CircleCI artifacts are saved correctly. |
| 46 | + * |
| 47 | + * Verifies that: |
| 48 | + * - PHPUnit coverage reports are generated for both parallel runners |
| 49 | + * - Behat feature files are saved for both parallel runners |
| 50 | + * - Feature files are split correctly between runners (e.g., clamav.feature |
| 51 | + * on runner 0 but not runner 1, search.feature on runner 1 but not |
| 52 | + * runner 0) |
| 53 | + */ |
| 54 | + #[Group('postbuild')] |
| 55 | + public function testCircleCiArtifactsAreSaved(): void { |
| 56 | + $currentJobNumber = (int) getenv('CIRCLE_BUILD_NUM'); |
| 57 | + $previousJobNumbers = $this->circleCiGetPreviousJobNumbers($currentJobNumber); |
| 58 | + |
| 59 | + $this->assertNotEmpty($previousJobNumbers, 'No previous job numbers found'); |
| 60 | + |
| 61 | + foreach ($previousJobNumbers as $previousJobNumber) { |
| 62 | + $artifactsData = $this->circleCiGetJobArtifacts($previousJobNumber); |
| 63 | + |
| 64 | + // Verify runner 0 artifacts. |
| 65 | + $artifactPathsRunner0 = $this->circleCiExtractArtifactPaths($artifactsData, 0); |
| 66 | + $artifactPathsRunner0Str = implode("\n", $artifactPathsRunner0); |
| 67 | + |
| 68 | + $this->assertStringContainsString('coverage/phpunit/cobertura.xml', $artifactPathsRunner0Str, 'Runner 0 should have PHPUnit cobertura coverage'); |
| 69 | + $this->assertStringContainsString('coverage/phpunit/.coverage-html/index.html', $artifactPathsRunner0Str, 'Runner 0 should have PHPUnit HTML coverage'); |
| 70 | + |
| 71 | + $this->assertStringContainsString('homepage.feature', $artifactPathsRunner0Str, 'Runner 0 should have homepage.feature'); |
| 72 | + $this->assertStringContainsString('login.feature', $artifactPathsRunner0Str, 'Runner 0 should have login.feature'); |
| 73 | + $this->assertStringContainsString('clamav.feature', $artifactPathsRunner0Str, 'Runner 0 should have clamav.feature'); |
| 74 | + $this->assertStringNotContainsString('search.feature', $artifactPathsRunner0Str, 'Runner 0 should NOT have search.feature'); |
| 75 | + |
| 76 | + // Verify runner 1 artifacts. |
| 77 | + $artifactPathsRunner1 = $this->circleCiExtractArtifactPaths($artifactsData, 1); |
| 78 | + $artifactPathsRunner1Str = implode("\n", $artifactPathsRunner1); |
| 79 | + |
| 80 | + $this->assertStringContainsString('coverage/phpunit/cobertura.xml', $artifactPathsRunner1Str, 'Runner 1 should have PHPUnit cobertura coverage'); |
| 81 | + $this->assertStringContainsString('coverage/phpunit/.coverage-html/index.html', $artifactPathsRunner1Str, 'Runner 1 should have PHPUnit HTML coverage'); |
| 82 | + |
| 83 | + $this->assertStringContainsString('homepage.feature', $artifactPathsRunner1Str, 'Runner 1 should have homepage.feature'); |
| 84 | + $this->assertStringContainsString('login.feature', $artifactPathsRunner1Str, 'Runner 1 should have login.feature'); |
| 85 | + $this->assertStringNotContainsString('clamav.feature', $artifactPathsRunner1Str, 'Runner 1 should NOT have clamav.feature'); |
| 86 | + $this->assertStringContainsString('search.feature', $artifactPathsRunner1Str, 'Runner 1 should have search.feature'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test that CircleCI test results are saved correctly. |
| 92 | + * |
| 93 | + * Verifies that: |
| 94 | + * - PHPUnit test results from various test suites are recorded |
| 95 | + * - Behat feature test results are recorded. |
| 96 | + */ |
| 97 | + #[Group('postbuild')] |
| 98 | + public function testCircleCiTestResultsAreSaved(): void { |
| 99 | + $currentJobNumber = (int) getenv('CIRCLE_BUILD_NUM'); |
| 100 | + $previousJobNumbers = $this->circleCiGetPreviousJobNumbers($currentJobNumber); |
| 101 | + |
| 102 | + $this->assertNotEmpty($previousJobNumbers, 'No previous job numbers found'); |
| 103 | + |
| 104 | + foreach ($previousJobNumbers as $previousJobNumber) { |
| 105 | + $testsData = $this->circleCiGetJobTestMetadata($previousJobNumber); |
| 106 | + $testPaths = $this->circleCiExtractTestPaths($testsData); |
| 107 | + $testPathsStr = implode("\n", $testPaths); |
| 108 | + |
| 109 | + // Verify PHPUnit test results. |
| 110 | + $this->assertStringContainsString('tests/phpunit/CircleCiConfigTest.php', $testPathsStr, 'Should have CircleCiConfigTest results'); |
| 111 | + $this->assertStringContainsString('tests/phpunit/Drupal/DatabaseSettingsTest.php', $testPathsStr, 'Should have DatabaseSettingsTest results'); |
| 112 | + $this->assertStringContainsString('tests/phpunit/Drupal/EnvironmentSettingsTest.php', $testPathsStr, 'Should have EnvironmentSettingsTest results'); |
| 113 | + $this->assertStringContainsString('tests/phpunit/Drupal/SwitchableSettingsTest.php', $testPathsStr, 'Should have SwitchableSettingsTest results'); |
| 114 | + $this->assertStringContainsString('web/modules/custom/ys_base/tests/src/Functional/ExampleTest.php', $testPathsStr, 'Should have custom module Functional test results'); |
| 115 | + $this->assertStringContainsString('web/modules/custom/ys_base/tests/src/Kernel/ExampleTest.php', $testPathsStr, 'Should have custom module Kernel test results'); |
| 116 | + $this->assertStringContainsString('web/modules/custom/ys_base/tests/src/Unit/ExampleTest.php', $testPathsStr, 'Should have custom module Unit test results'); |
| 117 | + |
| 118 | + // Verify Behat test results. |
| 119 | + $this->assertStringContainsString('homepage.feature', $testPathsStr, 'Should have homepage.feature results'); |
| 120 | + $this->assertStringContainsString('login.feature', $testPathsStr, 'Should have login.feature results'); |
| 121 | + $this->assertStringContainsString('clamav.feature', $testPathsStr, 'Should have clamav.feature results'); |
| 122 | + $this->assertStringContainsString('search.feature', $testPathsStr, 'Should have search.feature results'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments