diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml new file mode 100644 index 0000000..e001b83 --- /dev/null +++ b/.github/workflows/tests.yaml @@ -0,0 +1,85 @@ +name: Run Tests + +on: + push: + branches: + - gh-pages + - "**" + +jobs: + test: + name: Test - PHP ${{ matrix.php }} - ${{ matrix.setup }} + runs-on: ubuntu-latest + + env: + CC_TEST_REPORTER_ID: a706e45a0731ff71fe03470fefba2e1469a856018f131d0aee0be3f26ec4fc16 + + strategy: + matrix: + php: [7.1, 7.2, 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4] + setup: [lowest, stable] + include: + - php: nightly + setup: stable + fail-fast: false + + steps: + - name: Checkout Code + uses: actions/checkout@v4 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer + coverage: ${{ matrix.php == 8.4 && matrix.setup && 'pcov' || 'none' }} + + - name: Get composer cache directory + id: composer-cache + shell: bash + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache/Restore Composer + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: "php-${{ matrix.php }}-${{ matrix.setup }}-${{ matrix.os }}-${{ matrix.phpunit }}-${{ hashFiles('**/composer.json') }}" + restore-keys: "php-${{ matrix.php }}-${{ matrix.setup }}-${{ matrix.os }}-${{ matrix.phpunit }}-${{ hashFiles('**/composer.json') }}" + + - name: Set PHPUnit version + if: matrix.php >= 8.1 + run: composer require "phpunit/phpunit:^9.6.22" --no-update + + - name: Install Dependencies + run: composer update --prefer-dist --prefer-${{ matrix.setup }} --no-interaction --no-suggest + + - name: Run Tests with coverage + if: matrix.php == 8.4 && matrix.setup == 'stable' + run: vendor/bin/phpunit --coverage-clover=clover.xml --coverage-text --log-junit=test-results.xml + + - name: Run Tests + if: matrix.php != 8.4 || matrix.setup != 'stable' + run: vendor/bin/phpunit --log-junit=test-results.xml + + - name: Upload Test Results + if: matrix.php == 8.4 && matrix.setup == 'stable' + uses: actions/upload-artifact@v4 + with: + name: "${{ matrix.php-version }}-phpunit-results" + path: test-results.xml + + - name: Code Climate Test Reporter + if: matrix.php == 8.4 && matrix.setup == 'stable' + uses: aktions/codeclimate-test-reporter@v1 + with: + codeclimate-test-reporter-id: ${{ secrets.CC_TEST_REPORTER_ID }} + command: after-build -t clover + env: + ACTIONS_ALLOW_UNSECURE_COMMANDS: true + continue-on-error: true + + - name: Coverage + if: matrix.php == 8.4 && matrix.setup == 'stable' + uses: codecov/codecov-action@v5 + with: + token: ${{ secrets.CODECOV_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index eb2a1be..0000000 --- a/.travis.yml +++ /dev/null @@ -1,45 +0,0 @@ -language: php - -cache: - apt: true - directories: - - $HOME/.composer/cache - -sudo: false - -install: - - if [[ $setup = 'nightly' ]]; then travis_retry composer update --prefer-dist --no-interaction --no-suggest --ignore-platform-reqs; fi - - if [[ $setup = 'basic' ]]; then travis_retry composer update --prefer-dist --prefer-stable --no-interaction --no-suggest; fi - -before_script: - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - - chmod +x ./cc-test-reporter - - ./cc-test-reporter before-build - -script: - - vendor/bin/phpunit --coverage-clover=coverage.xml --coverage-text - -after_success: - - bash <(curl -s https://codecov.io/bash) - -after_script: - - cp coverage.xml clover.xml - - ./cc-test-reporter after-build --coverage-input-type clover --exit-code $TRAVIS_TEST_RESULT - -env: - global: - - setup=basic - - CC_TEST_REPORTER_ID=a706e45a0731ff71fe03470fefba2e1469a856018f131d0aee0be3f26ec4fc16 - -branches: - only: - - gh-pages - - /.*/ - -matrix: - include: - - php: '7.1' - - php: '7.2' - - php: '7.3' - - php: nightly - env: setup=nightly diff --git a/composer.json b/composer.json index 46c0dbc..52e48bc 100644 --- a/composer.json +++ b/composer.json @@ -14,10 +14,10 @@ "require": { "php": "^7.1 || ^8.0", "simple-cli/simple-cli": "^1.1", - "nesbot/carbon": "^2.20" + "nesbot/carbon": "^2.72.6 || ^3.0" }, "require-dev": { - "phpunit/phpunit": "^7.5 || ^8.2" + "phpunit/phpunit": "^7.5.20 || ^8.5.41 || ^9.6.22" }, "autoload": { "psr-4": { diff --git a/src/Carbon/Types/Generator.php b/src/Carbon/Types/Generator.php index 0e52959..b52e598 100644 --- a/src/Carbon/Types/Generator.php +++ b/src/Carbon/Types/Generator.php @@ -3,6 +3,7 @@ namespace Carbon\Types; use Carbon\Carbon; +use Carbon\FactoryImmutable; use Closure; use ReflectionClass; use ReflectionException; @@ -51,11 +52,19 @@ protected function getMethods($boots) $this->runBoot($boot); } + if (method_exists(FactoryImmutable::class, 'getDefaultInstance')) { + // Carbon 3 + return FactoryImmutable::getDefaultInstance()->getSettings()['macros'] ?? []; + } + + // @codeCoverageIgnoreStart + // Carbon 2 $c = new ReflectionClass(Carbon::now()); $macros = $c->getProperty('globalMacros'); $macros->setAccessible(true); return $macros->getValue(); + // @codeCoverageIgnoreEnd } /** diff --git a/tests/Carbon/Types/GeneratorTest.php b/tests/Carbon/Types/GeneratorTest.php index 8dd434d..4d9dd7d 100644 --- a/tests/Carbon/Types/GeneratorTest.php +++ b/tests/Carbon/Types/GeneratorTest.php @@ -58,8 +58,9 @@ public function testGetMethodSourceCode() $foo = new ReflectionMethod(get_class($object), 'foo'); $sourceCode = $method->invoke($generator, $foo); + $match = (preg_match('/public function foo\(\)\s*\{\s*return \'hello\';/', $sourceCode) > 0); - $this->assertRegExp('/public function foo\(\)\s*\{\s*return \'hello\';/', $sourceCode); + $this->assertTrue($match, 'Source code must contain foo method returning "hello".'); } /**