Skip to content

Commit

Permalink
helper functions tested;
Browse files Browse the repository at this point in the history
  • Loading branch information
hans-thomas committed Jul 15, 2023
1 parent 5808b8b commit 1350ac1
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/Helpers/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ function generate_order(): float {
/**
* Resolve the given id to a related model
*
* @param int $related
* @param int $id
* @param string $entity
*
* @return Model|false
* @throws ValravnException
*/
function resolveRelatedIdToModel( int $related, string $entity ): Model|false {
function resolveRelatedIdToModel( int $id, string $entity ): Model|false {
if ( ! ( class_exists( $entity ) and is_a( $entity, Model::class, true ) ) ) {
throw PackageException::invalidEntity( $entity );
}

try {
$model = ( new $entity )->query()->applyFilters()->findOrFail( $related );
$model = ( new $entity )->query()->applyFilters()->findOrFail( $id );
} catch ( Throwable $e ) {
return false;
}
Expand Down Expand Up @@ -85,7 +85,7 @@ function resolveMorphableToResource( ?Model $morphable ): JsonResource {
* @return void
*/
function logg( string $location, Throwable $e, array $context = [] ): void {
// TODO: log channel should be document
// TODO: log channel should be documented
Log::channel( 'valravn' )->debug( $location . ' => ' . 'message: ' . $e->getMessage(), $context );
}
}
Expand All @@ -107,12 +107,12 @@ function valravn_config( string $key ): string|array {
/**
* Make a english or non-english string to a slug
*
* @param $string
* @param $separator
* @param string $string
* @param string $separator
*
* @return array|string|null
* @return string|null
*/
function slugify( $string, $separator = '-' ): array|string|null {
function slugify( string $string, string $separator = '-' ): string|null {

$_transliteration = [
"/ö|œ/" => "e",
Expand Down
22 changes: 22 additions & 0 deletions tests/Core/Factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Hans\Valravn\Tests\Core\Factories;

use Hans\Valravn\Tests\Core\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class UserFactory extends Factory {

protected $model = User::class;

/**
* @return array
*/
public function definition(): array {
return [
'name' => fake()->name(),
'email' => fake()->email(),
'password' => bcrypt( 'password' )
];
}
}
60 changes: 60 additions & 0 deletions tests/Core/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Hans\Valravn\Tests\Core\Models;

use Hans\Valravn\Http\Resources\Contracts\ValravnJsonResource;
use Hans\Valravn\Http\Resources\Contracts\ValravnResourceCollection;
use Hans\Valravn\Models\Contracts\ResourceCollectionable;
use Hans\Valravn\Tests\Core\Factories\UserFactory;
use Hans\Valravn\Tests\Core\Resources\User\UserCollection;
use Hans\Valravn\Tests\Core\Resources\User\UserResource;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements ResourceCollectionable {

use HasFactory;

protected $fillable = [
'name',
'email',
'password',
];

/**
* Create a new factory instance for the model.
*
* @return Factory<static>
*/
protected static function newFactory() {
return UserFactory::new();
}

/**
* Return related resource class
*
* @return ValravnJsonResource
*/
public static function getResource(): ValravnJsonResource {
return UserResource::make( ...func_get_args() );
}

/**
* Convert current instance to a related resource class
*
* @return ValravnJsonResource
*/
public function toResource(): ValravnJsonResource {
return self::getResource( $this, ...func_get_args() );
}

/**
* Return related resource collection class
*
* @return ValravnResourceCollection
*/
public static function getResourceCollection(): ValravnResourceCollection {
return UserCollection::make( ...func_get_args() );
}
}
26 changes: 26 additions & 0 deletions tests/Core/Resources/User/UserCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Hans\Valravn\Tests\Core\Resources\User;

use Hans\Valravn\Http\Resources\Contracts\ValravnResourceCollection;
use Illuminate\Database\Eloquent\Model;

class UserCollection extends ValravnResourceCollection {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return null;
}

/**
* @return string
*/
public function type(): string {
return 'users';
}

}
30 changes: 30 additions & 0 deletions tests/Core/Resources/User/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Hans\Valravn\Tests\Core\Resources\User;

use Hans\Valravn\Http\Resources\Contracts\ValravnJsonResource;
use Illuminate\Database\Eloquent\Model;

class UserResource extends ValravnJsonResource {

/**
* @param Model $model
*
* @return array|null
*/
public function extract( Model $model ): ?array {
return [
'id' => $model->id,
'name' => $model->name,
'email' => $model->email,
];
}

/**
* @return string
*/
public function type(): string {
return 'users';
}

}
35 changes: 35 additions & 0 deletions tests/Core/migrations/2023_07_15_000000_create_users_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Hans\Valravn\Tests\Core\Models\Category;
use Hans\Valravn\Tests\Core\Models\Post;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up() {
Schema::create( 'users', function( Blueprint $table ) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
} );
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down() {
Schema::dropIfExists( 'users' );
}
};
129 changes: 129 additions & 0 deletions tests/Feature/Helper/FunctionsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

namespace Hans\Valravn\Tests\Feature\Helper;

use Hans\Valravn\Exceptions\ValravnException;
use Hans\Valravn\Tests\Core\Factories\PostFactory;
use Hans\Valravn\Tests\Core\Factories\UserFactory;
use Hans\Valravn\Tests\Core\Models\Post;
use Hans\Valravn\Tests\Core\Models\User;
use Hans\Valravn\Tests\Core\Resources\User\UserResource;
use Hans\Valravn\Tests\TestCase;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Optional;

class FunctionsTest extends TestCase {

private User $user;
private Post $post;

/**
* Setup the test environment.
*/
protected function setUp(): void {
parent::setUp();
$this->user = UserFactory::new()->create();
$this->post = PostFactory::new()->create();
}

/**
* @test
*
* @return void
*/
public function user(): void {
self::assertInstanceOf(
Optional::class,
\user()
);
self::assertEquals(
\optional(),
\user()
);

$this->actingAs( $this->user );

self::assertInstanceOf(
User::class,
\user()
);
self::assertEquals(
$this->user->toArray(),
\user()->toArray()
);
}

/**
* @test
*
* @return void
*/
public function generate_order(): void {
self::assertIsFloat( generate_order() );
}

/**
* @test
*
* @return void
* @throws ValravnException
*/
public function resolveRelatedIdToModel(): void {
$model = resolveRelatedIdToModel( 1, Post::class );

self::assertInstanceOf(
Post::class,
$model
);
self::assertTrue(
$this->post->is( $model )
);
}

/**
* @test
*
* @return void
*/
public function resolveMorphableToResource(): void {
$resource = resolveMorphableToResource( $this->post );

self::assertInstanceOf(
JsonResource::class,
$resource
);
}

/**
* @test
*
* @return void
*/
public function resolveMorphableToResourceWithResourceCollectionableImplemented(): void {
$resource = resolveMorphableToResource( $this->user );

self::assertInstanceOf(
UserResource::class,
$resource
);
}

/**
* @test
*
* @return void
*/
public function slugify(): void {
// poetry meaning: if you are alive now, don't let the present pass without happiness -Omar Khayyam
// p.s.: obviously it is very complex and deep, so I can't translate it properly.
$non_english_string = 'گر یک نفست ز زندگانی گذرد / مگذار ک جز به شادمانی گذرد';
$slug = slugify( $non_english_string );

self::assertIsString( $slug );
self::assertEquals(
'گر-یک-نفست-ز-زندگانی-گذرد-مگذار-ک-جز-به-شادمانی-گذرد',
$slug
);
}

}

1 comment on commit 1350ac1

@vercel
Copy link

@vercel vercel bot commented on 1350ac1 Jul 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

valravn – ./

valravn-git-master-hans-thomas.vercel.app
valravn-hans-thomas.vercel.app
valravn.vercel.app

Please sign in to comment.