Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel 11 update #1

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/laravel.yml
Original file line number Diff line number Diff line change
@@ -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.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -42,6 +42,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^9.6"
"phpunit/phpunit": "^11.0",
"laravel/pint": "^1.14"
}
}
14 changes: 7 additions & 7 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src/</directory>
</include>
</coverage>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="vendor/autoload.php" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd">
<testsuites>
<testsuite name="Moving Average Collection Tests">
<testsuite name="Rolling Average Collection Tests">
<directory>tests</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
</phpunit>
22 changes: 11 additions & 11 deletions src/Providers/CollectionRollingAverageServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
Expand Down
25 changes: 11 additions & 14 deletions tests/Macros/CollectionRollingAverageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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]);
Expand Down