Skip to content

Commit

Permalink
Merge pull request #11 from SuperDJ/dev-tests
Browse files Browse the repository at this point in the history
Added tests
  • Loading branch information
ajcastro authored Nov 2, 2022
2 parents 55ffe94 + e2f5449 commit e7e27ce
Show file tree
Hide file tree
Showing 30 changed files with 1,045 additions and 162 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.editorconfig export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.github export-ignore
/phpunit.xml.dist export-ignore
/tests export-ignore
/CHANGELOG.md export-ignore
/CONTRIBUTING.md export-ignore
/README.md export-ignore
41 changes: 41 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: Tests

on:
pull_request:
push:
branches: [master, main]

jobs:
test:
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php: [7.2, 7.3, 7.4, 8.0, 8.1]
stability: [prefer-lowest, prefer-stable]

name: PHP ${{ matrix.php }} - ${{ matrix.stability }} Test

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, curl, libxml, mbstring, zip, pdo
ini-values: error_reporting=E_ALL
tools: composer:v2
coverage: none

- name: Install dependencies
uses: nick-invision/retry@v1
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress

- name: Run tests
run: ./vendor/bin/phpunit
vendor/bin/phpunit --verbose
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor
composer.lock
*.cache
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@
"require": {
"php": ">=5.6.0"
},
"require-dev": {
"laravel/framework": ">= 5.0.0",
"laravel/legacy-factories": ">= 1.0.0",
"orchestra/testbench": ">= 3.0.0",
"phpunit/phpunit": ">= 6.0.0"
},
"autoload": {
"psr-4": {
"AjCastro\\EagerLoadPivotRelations\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"AjCastro\\EagerLoadPivotRelations\\Tests\\": "tests"
},
"classmap": [
"tests/Database/Migrations"
]
},
"extra": {
"laravel": {
"providers": [
Expand Down
31 changes: 31 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">./src</directory>
</include>
</coverage>
<php>
<ini name="display_errors" value="On" />
<ini name="display_startup_errors" value="On" />
<ini name="date.timezone" value="UTC" />
<ini name="intl.default_locale" value="C.UTF-8" />
<ini name="memory_limit" value="2048M" />
<env name="DB_CONNECTION" value="sqlite" />
<env name="DB_DATABASE" value=":memory:" />
<env name="APP_KEY" value="base64:2fl+Ktvkfl+Fuz4Qp/A75G2RTiWVA/ZoKZvp6fiiM10="/>
</php>
</phpunit>
160 changes: 0 additions & 160 deletions readme.md

This file was deleted.

4 changes: 2 additions & 2 deletions src/EagerLoadPivotBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ protected function isPivotAccessor($name)
/**
* Eager load pivot relations.
*
* @param string $pivotAccessor
* @param array $models
* @param string $pivotAccessor
* @return void
*/
protected function eagerLoadPivotRelations($models, $pivotAccessor)
Expand All @@ -90,7 +90,7 @@ protected function eagerLoadPivotRelations($models, $pivotAccessor)
protected function getPivotEagerLoadRelations($pivotAccessor)
{
$relations = array_filter($this->eagerLoad, function ($relation) use ($pivotAccessor) {
return $relation != $pivotAccessor && Str::contains($relation, $pivotAccessor);
return $relation !== $pivotAccessor && Str::contains($relation, $pivotAccessor);
}, ARRAY_FILTER_USE_KEY);

return array_combine(
Expand Down
19 changes: 19 additions & 0 deletions tests/Database/Factories/BrandFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace AjCastro\EagerLoadPivotRelations\Tests\Database\Factories;

use AjCastro\EagerLoadPivotRelations\Tests\Models\Brand;
use Illuminate\Database\Eloquent\Factories\Factory;

class BrandFactory extends Factory
{
protected $model = Brand::class;

public function definition()
{
return [
'name' => $this->faker->word,
'logo' => $this->faker->imageUrl,
];
}
}
26 changes: 26 additions & 0 deletions tests/Database/Factories/CarFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace AjCastro\EagerLoadPivotRelations\Tests\Database\Factories;

use AjCastro\EagerLoadPivotRelations\Tests\Models\Brand;
use AjCastro\EagerLoadPivotRelations\Tests\Models\Car;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\AjCastro\EagerLoadPivotRelations\Tests\Models\Car>
*/
class CarFactory extends Factory
{
protected $model = Car::class;

public function definition()
{
return [
'model' => $this->faker->words(rand(2, 4), true),
'brand_id' => function()
{
return Brand::factory()->create()->id;
}
];
}
}
33 changes: 33 additions & 0 deletions tests/Database/Factories/CarUserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace AjCastro\EagerLoadPivotRelations\Tests\Database\Factories;

use AjCastro\EagerLoadPivotRelations\Tests\Models\Car;
use AjCastro\EagerLoadPivotRelations\Tests\Models\CarUser;
use AjCastro\EagerLoadPivotRelations\Tests\Models\Color;
use AjCastro\EagerLoadPivotRelations\Tests\Models\Tire;
use AjCastro\EagerLoadPivotRelations\Tests\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\AjCastro\EagerLoadPivotRelations\Tests\Models\CarUser>
*/
class CarUserFactory extends Factory
{
protected $model = CarUser::class;

public function definition()
{
return [
'car_id' => function() {
return Car::factory()->create()->id;
},
'color_id' => function() {
return Color::factory()->create()->id;
},
'user_id' => function() {
return User::factory()->create()->id;
}
];
}
}
Loading

0 comments on commit e7e27ce

Please sign in to comment.