diff --git a/.github/workflows/laravel.yml b/.github/workflows/laravel.yml new file mode 100644 index 0000000..d50e3a1 --- /dev/null +++ b/.github/workflows/laravel.yml @@ -0,0 +1,35 @@ +name: Laravel Build and Test + +on: + push: + branches: [ main ] + pull_request: + branches: [ main ] + +jobs: + build: + + runs-on: ubuntu-latest + + strategy: + matrix: + php-versions: ['8.2', '8.3'] # Adjust PHP versions as needed + laravel-versions: ['^8.0', '^9.0', '^10.0', '^11.0'] # Adjust Laravel versions as needed + + steps: + - uses: actions/checkout@v4 + + - name: Set up PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-versions }} + extensions: mbstring, dom, curl, pdo_mysql + tools: composer, phpunit + + - name: Install Dependencies + run: composer install --no-progress --prefer-dist + + - name: Run Tests + run: vendor/bin/phpunit + + # Add additional steps if necessary, like linting, code style checks, etc. diff --git a/composer.json b/composer.json index e4c3350..1740ef5 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,8 @@ } }, "require": { - "php": "^8.0", - "illuminate/support": "^8.71|^9.0|^10.0" + "php": "^8.2", + "illuminate/support": "^8.71|^9.0|^10.0|^11.0" }, "scripts": { "test": "vendor/bin/phpunit" @@ -42,6 +42,7 @@ } }, "require-dev": { - "phpunit/phpunit": "^9.6" + "phpunit/phpunit": "^11.0", + "laravel/pint": "^1.14" } } diff --git a/phpunit.xml b/phpunit.xml index c348f21..d028e81 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,13 +1,13 @@ - - - - src/ - - + - + tests + + + src/ + + diff --git a/src/Providers/CollectionRollingAverageServiceProvider.php b/src/Providers/CollectionRollingAverageServiceProvider.php index 93af5fd..e8fa85b 100644 --- a/src/Providers/CollectionRollingAverageServiceProvider.php +++ b/src/Providers/CollectionRollingAverageServiceProvider.php @@ -12,16 +12,16 @@ public function register() /** * Rolling average macro. * - * @param int $recordsToAverage How many records to look back when creating the average - * @param bool $enforceLookback Only average $recordsToAverage values, i.e. if asked to average 5 entries, - * don't attempt to average anything until the 5th entry. - * @param Collection $weightings Weightings to apply to values. This allows different importance - * to be applied to different values. First value is applied to most - * recent addition to rolling average. + * @param int $recordsToAverage How many records to look back when creating the average + * @param bool $enforceLookback Only average $recordsToAverage values, i.e. if asked to average 5 entries, + * don't attempt to average anything until the 5th entry. + * @param Collection $weightings Weightings to apply to values. This allows different importance + * to be applied to different values. First value is applied to most + * recent addition to rolling average. */ - Collection::macro('rollingAverage', function (int $recordsToAverage = null, bool $enforceLookback = false, Collection $weights = null) { + Collection::macro('rollingAverage', function (?int $recordsToAverage = null, bool $enforceLookback = false, ?Collection $weights = null) { if ($recordsToAverage === null - || ($recordsToAverage > $this->count() && !$enforceLookback)) { + || ($recordsToAverage > $this->count() && ! $enforceLookback)) { $recordsToAverage = $this->count(); } // The number of weights shouldn't exceed our recordsToAverage - trim it down if so. @@ -44,12 +44,12 @@ public function register() // Set should be this item, and X preceding // For each in set, multiply by available weight if ($weights) { - $startingPoint = max(0, ($index+1) - $recordsToAverage); - $recordsToTake = min($index+1, $recordsToAverage); + $startingPoint = max(0, ($index + 1) - $recordsToAverage); + $recordsToTake = min($index + 1, $recordsToAverage); $weighted = $this->slice($startingPoint, $recordsToTake)->reverse(); foreach ($weighted->values() as $weightIndex => $item) { - if (!empty($weights[$weightIndex])) { + if (! empty($weights[$weightIndex])) { $weightedAverage->prepend($weights[$weightIndex] * $item); } else { $weightedAverage->prepend($item); diff --git a/tests/Macros/CollectionRollingAverageTest.php b/tests/Macros/CollectionRollingAverageTest.php index e12bf0b..9873e4e 100644 --- a/tests/Macros/CollectionRollingAverageTest.php +++ b/tests/Macros/CollectionRollingAverageTest.php @@ -3,11 +3,12 @@ namespace Square1\CollectionRollingAverage\Test\Macros; use Illuminate\Support\Collection; +use PHPUnit\Framework\Attributes\Test; use Square1\CollectionRollingAverage\Test\TestCase; class CollectionRollingAverageTest extends TestCase { - /** @test */ + #[Test] public function rollling_average_is_calculated_without_limit() { $data = new Collection([1, 2, 3]); @@ -17,7 +18,7 @@ public function rollling_average_is_calculated_without_limit() $this->assertEquals($averages, $data->rollingAverage()); } - /** @test */ + #[Test] public function rollling_average_is_calculated_with_lookback_limit() { $data = new Collection([1, 2, 3, 4, 5, 6]); @@ -29,13 +30,13 @@ public function rollling_average_is_calculated_with_lookback_limit() 2.5, // 2+3 / 2 3.5, // 3+4 / 2 4.5, // 4+5 / 2 - 5.5 // 5+6 / 2 + 5.5, // 5+6 / 2 ]); $this->assertEquals($averages, $data->rollingAverage(2)); } - /** @test */ + #[Test] public function rollling_average_is_calculated_with_lookback_limit_larger_than_set() { $data = new Collection([1, 2, 3]); @@ -46,7 +47,7 @@ public function rollling_average_is_calculated_with_lookback_limit_larger_than_s $this->assertEquals($averages, $data->rollingAverage(5)); } - /** @test */ + #[Test] public function enforced_lookback_returns_smaller_set() { $data = new Collection([1, 2, 3, 4, 5, 6]); @@ -62,7 +63,7 @@ public function enforced_lookback_returns_smaller_set() $this->assertEquals($averages, $data->rollingAverage(3, $enforceLookback = true)); } - /** @test */ + #[Test] public function enforced_lookback_larger_than_set_returns_empty_collection() { $data = new Collection([1, 2, 3]); @@ -73,8 +74,7 @@ public function enforced_lookback_larger_than_set_returns_empty_collection() ); } - - /** @test */ + #[Test] public function rollling_average_applies_weights_when_weight_size_is_less_than_collection_size() { $data = new Collection([1, 2, 2, 4, 5, 6]); @@ -92,8 +92,7 @@ public function rollling_average_applies_weights_when_weight_size_is_less_than_c $this->assertEquals($averages, $data->rollingAverage(4, false, $weights)); } - - /** @test */ + #[Test] public function rollling_average_applies_weights_when_weight_size_is_larger_than_collection_size() { $data = new Collection([1, 2, 3]); @@ -108,8 +107,7 @@ public function rollling_average_applies_weights_when_weight_size_is_larger_than $this->assertEquals($averages, $data->rollingAverage(4, false, $weights)); } - - /** @test */ + #[Test] public function rollling_average_applies_weights_when_weight_size_is_less_than_collection_size_and_lookback_is_applied() { $data = new Collection([1, 2, 2, 4, 5, 6]); @@ -124,8 +122,7 @@ public function rollling_average_applies_weights_when_weight_size_is_less_than_c $this->assertEquals($averages, $data->rollingAverage(4, true, $weights)); } - - /** @test */ + #[Test] public function rollling_average_applies_weights_when_weight_size_is_larger_than_collection_size_and_lookback_is_applied() { $data = new Collection([1, 2, 3, 1, 2, 3]);