-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5808b8b
commit 1350ac1
Showing
7 changed files
with
310 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ) | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
tests/Core/migrations/2023_07_15_000000_create_users_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
|
||
} |
1350ac1
There was a problem hiding this comment.
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