Skip to content

Commit

Permalink
Add PHPUnit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
KFoobar committed Jan 31, 2024
1 parent 6d104f7 commit f53d0c0
Show file tree
Hide file tree
Showing 6 changed files with 153 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
vendor/
composer.lock
.DS_Store
.phpunit.cache/
.phpunit.result.cache
25 changes: 19 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
{
"name": "kfoobar/laravel-uuid",
"description": "Eloquent UUID trait for Laravel",
"type": "plugin",
"description": "UUID Trait for Eloquent Models in Laravel",
"type": "library",
"license": "MIT",
"authors": [
{
"name": "KFoobar",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.3|^8.0",
"illuminate/database": ">=5.5",
"illuminate/support": ">=5.5"
"ext-json": "*",
"illuminate/database": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0",
"illuminate/support": "^5.5|^6.0|^7.0|^8.0|^9.0|^10.0"
},
"require-dev": {
"orchestra/testbench": "^8.11",
"phpunit/phpunit": "^10.1"
},
"minimum-stability": "stable",
"prefer-stable": true,
"autoload": {
"psr-4": {
"KFoobar\\Uuid\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"KFoobar\\Uuid\\Test\\": "tests/"
}
},
"scripts": {
"test": "vendor/bin/phpunit"
}
}
24 changes: 24 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
backupGlobals="false"
beStrictAboutTestsThatDoNotTestAnything="false"
bootstrap="vendor/autoload.php"
colors="true"
processIsolation="false"
stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="APP_KEY" value="AckfSECXIvnK5r28GVIWUAxmbBSjTsmF"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="QUEUE_CONNECTION" value="array"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
</phpunit>
18 changes: 18 additions & 0 deletions tests/Fixtures/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace KFoobar\Uuid\Test\Fixtures;

use Illuminate\Database\Eloquent\Model;
use KFoobar\Uuid\Traits\HasUuid;

class Post extends Model
{
use HasUuid;

/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id'];
}
76 changes: 76 additions & 0 deletions tests/Unit/HasUuidTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace KFoobar\Uuid\Test\Unit;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use KFoobar\Uuid\Test\Fixtures\Post;
use KFoobar\Uuid\Test\Unit\TestCase;

class HasUuidTest extends TestCase
{
use RefreshDatabase;

protected $regex = '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i';

protected function setUp(): void
{
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->id('id');
$table->uuid('uuid');
$table->string('heading')->nullable();
$table->timestamps();
});
}

public function testModelIsModel(): void
{
$post = new Post;

$this->assertInstanceOf(Model::class, $post);
}

public function testCreateModel()
{
$post = Post::create(['heading' => 'Lorem ipsum dolor']);

$this->assertMatchesRegularExpression($this->regex, $post->uuid);

$post = Post::first();

$this->assertMatchesRegularExpression($this->regex, $post->uuid);
}

public function testUpdateModel()
{
$post = Post::create(['heading' => 'Lorem ipsum dolor']);

$firstUuid = $post->uuid;

$post->update([
'heading' => 'Dolor ipsum lorem',
]);

$secondUuid = $post->uuid;

$this->assertEquals($firstUuid, $secondUuid);
}

public function testSaveModelWithEmptyUuid()
{
$post = Post::create(['heading' => 'Lorem ipsum dolor']);

$post->uuid = '';
$post->save();

$post = Post::first();

$this->assertNotEmpty($post->uuid);
$this->assertNotNull($post->uuid);
$this->assertMatchesRegularExpression($this->regex, $post->uuid);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace KFoobar\Uuid\Test\Unit;

use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{

protected function defineEnvironment($app)
{
$app['config']->set('database.default', 'testing');
}
}

0 comments on commit f53d0c0

Please sign in to comment.