From 58ac84c2c3a73369cd7d72dc16b4ce03cbe7cf2e Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Sun, 24 Mar 2024 22:35:33 -0400 Subject: [PATCH] set postges add documents --- .env.github | 2 +- .github/workflows/ci-cd.yml | 51 ++++++++++--------- app/Domains/Documents/StatusEnum.php | 16 ++++++ app/Domains/Documents/TypesEnum.php | 26 ++++++++++ app/Helpers/EnumHelperTrait.php | 46 +++++++++++++++++ app/Models/Collection.php | 6 +++ app/Models/Document.php | 38 ++++++++++++++ database/factories/CollectionFactory.php | 2 +- database/factories/DocumentFactory.php | 30 +++++++++++ ...24_03_25_020252_create_documents_table.php | 34 +++++++++++++ tests/Feature/Models/DocumentTest.php | 21 ++++++++ tests/TestCase.php | 2 + 12 files changed, 249 insertions(+), 25 deletions(-) create mode 100644 app/Domains/Documents/StatusEnum.php create mode 100644 app/Domains/Documents/TypesEnum.php create mode 100644 app/Helpers/EnumHelperTrait.php create mode 100644 app/Models/Document.php create mode 100644 database/factories/DocumentFactory.php create mode 100644 database/migrations/2024_03_25_020252_create_documents_table.php create mode 100644 tests/Feature/Models/DocumentTest.php diff --git a/.env.github b/.env.github index 3ea335e8..69e106f1 100644 --- a/.env.github +++ b/.env.github @@ -12,7 +12,7 @@ DB_CONNECTION=pgsql DB_HOST=127.0.0.1 DB_PORT=5432 DB_DATABASE=testing -DB_USERNAME=postgres +DB_USERNAME=root DB_PASSWORD=password BROADCAST_DRIVER=log diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 3ecf412d..01dce5b5 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -1,6 +1,6 @@ name: CI-CD -on: [ push ] +on: [push] jobs: ci: @@ -13,30 +13,26 @@ jobs: APP_ENV: testing BROADCAST_DRIVER: log services: - postgres: - image: postgres:latest - env: - POSTGRES_PASSWORD: password - POSTGRES_DB: testing - ports: - - 5432/tcp - options: --health-cmd="pg_isready -U postgres" --health-interval=10s --health-timeout=5s --health-retries=5 - + database: + image: ankane/pgvector:latest + env: + POSTGRES_PASSWORD: password + POSTGRES_USER: root + POSTGRES_DB: testing + ports: + - 5432:5432 + options: >- + --health-cmd pg_isready + --health-interval 10s + --health-timeout 5s + --health-retries 5 strategy: fail-fast: false matrix: - php-versions: [ "8.2" ] + php-versions: ["8.2"] steps: - uses: actions/checkout@v4 - - name: Create the PostgreSQL database - run: | - sudo service postgresql start - PGPASSWORD=$POSTGRES_PASSWORD psql -h localhost -U $POSTGRES_USER -tc "SELECT 1 FROM pg_database WHERE datname = 'testing'" | grep -q 1 || PGPASSWORD=$POSTGRES_PASSWORD psql -h localhost -U $POSTGRES_USER -c "CREATE DATABASE testing" - env: - POSTGRES_USER: postgres - POSTGRES_PASSWORD: password - - name: Setup PHP, with composer and extensions uses: shivammathur/setup-php@v2 with: @@ -45,17 +41,26 @@ jobs: extensions: mbstring, dom, fileinfo, pgsql, grpc, :psr coverage: xdebug + - name: PHP Code Style (phpcs) + run: | + composer fix + + - name: PHP Code Style (phpcs) + run: | + composer stan + - name: Enable PostgreSQL Extensions run: | - sudo service postgresql start - sudo -u postgres psql -c "CREATE EXTENSION IF NOT EXISTS vector;" -d testing + PGPASSWORD=$DB_PASSWORD psql -h localhost -U $DB_USERNAME -d $DB_DATABASE -c "CREATE EXTENSION IF NOT EXISTS vector;" + env: + DB_PORT: ${{ job.services.postgres.ports['5432'] }} - name: Test with phpunit run: | npm install && npm run build - XDEBUG_MODE=coverage php artisan test --coverage --min=50 + XDEBUG_MODE=coverage php artisan test --coverage --min=50 env: - DB_PORT: ${{ job.services.mysql.ports['3306'] }} + DB_PORT: ${{ job.services.postgres.ports['5432'] }} cd: runs-on: ubuntu-latest diff --git a/app/Domains/Documents/StatusEnum.php b/app/Domains/Documents/StatusEnum.php new file mode 100644 index 00000000..a7d9f833 --- /dev/null +++ b/app/Domains/Documents/StatusEnum.php @@ -0,0 +1,16 @@ +getConstants(); + + $keyValueArray = []; + foreach ($cases as $case) { + $keyValueArray[$case->value] = str($case->name)->headline()->toString(); + } + + return $keyValueArray; + } + + public static function random(): string + { + $enumReflection = new ReflectionClass(self::class); + $cases = $enumReflection->getConstants(); + $randomKey = array_rand($cases); + + return $cases[$randomKey]->value; + } + + public static function selectOptions(): array + { + $enumReflection = new ReflectionClass(self::class); + $cases = $enumReflection->getConstants(); + + $keyValueArray = []; + foreach ($cases as $case) { + $keyValueArray[] = [ + 'id' => $case->value, + 'name' => str($case->name)->headline()->toString(), + ]; + } + + return $keyValueArray; + } +} diff --git a/app/Models/Collection.php b/app/Models/Collection.php index ef8a22f4..3d3e4045 100644 --- a/app/Models/Collection.php +++ b/app/Models/Collection.php @@ -5,6 +5,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\HasMany; /** * Class Project @@ -31,4 +32,9 @@ public function team(): BelongsTo { return $this->belongsTo(Team::class); } + + public function documents(): HasMany + { + return $this->hasMany(Document::class); + } } diff --git a/app/Models/Document.php b/app/Models/Document.php new file mode 100644 index 00000000..b582de98 --- /dev/null +++ b/app/Models/Document.php @@ -0,0 +1,38 @@ + TypesEnum::class, + 'status' => StatusEnum::class, + 'status' => 'string', + ]; + + public function collection(): BelongsTo + { + return $this->belongsTo(Collection::class); + } +} diff --git a/database/factories/CollectionFactory.php b/database/factories/CollectionFactory.php index 0bfd4729..ec0de092 100644 --- a/database/factories/CollectionFactory.php +++ b/database/factories/CollectionFactory.php @@ -21,7 +21,7 @@ public function definition(): array 'name' => $this->faker->sentence, 'description' => $this->faker->paragraph, 'active' => $this->faker->boolean, - 'team_id' => Team::factory() + 'team_id' => Team::factory(), ]; } } diff --git a/database/factories/DocumentFactory.php b/database/factories/DocumentFactory.php new file mode 100644 index 00000000..5390df34 --- /dev/null +++ b/database/factories/DocumentFactory.php @@ -0,0 +1,30 @@ + + */ +class DocumentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + return [ + 'type' => TypesEnum::random(), + 'status' => StatusEnum::random(), + 'summary' => $this->faker->text(), + 'file_path' => $this->faker->url(), + 'collection_id' => Collection::factory(), + ]; + } +} diff --git a/database/migrations/2024_03_25_020252_create_documents_table.php b/database/migrations/2024_03_25_020252_create_documents_table.php new file mode 100644 index 00000000..2b611ecb --- /dev/null +++ b/database/migrations/2024_03_25_020252_create_documents_table.php @@ -0,0 +1,34 @@ +id(); + $table->string('type')->nullable(); + $table->string('status')->default(StatusEnum::Pending); + $table->longText('summary')->nullable(); + $table->string('file_path')->nullable(); + $table->foreignIdFor(Collection::class); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('documents'); + } +}; diff --git a/tests/Feature/Models/DocumentTest.php b/tests/Feature/Models/DocumentTest.php new file mode 100644 index 00000000..11f8de64 --- /dev/null +++ b/tests/Feature/Models/DocumentTest.php @@ -0,0 +1,21 @@ +create(); + + $this->assertNotNull($model->collection_id); + $this->assertNotNull($model->collection->id); + $this->assertCount(1, $model->collection->documents); + $this->assertNotNull($model->collection->documents()->first()->id); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 539c7dde..a6c0ed2d 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -2,8 +2,10 @@ namespace Tests; +use Illuminate\Foundation\Testing\LazilyRefreshDatabase; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; abstract class TestCase extends BaseTestCase { + use LazilyRefreshDatabase; }