From f53d0c09333915effd4c241fc25a9192661cc025 Mon Sep 17 00:00:00 2001 From: KFoobar Date: Wed, 31 Jan 2024 11:31:36 +0100 Subject: [PATCH] Add PHPUnit tests --- .gitignore | 2 + composer.json | 25 ++++++++++--- phpunit.xml | 24 ++++++++++++ tests/Fixtures/Post.php | 18 +++++++++ tests/Unit/HasUuidTest.php | 76 ++++++++++++++++++++++++++++++++++++++ tests/Unit/TestCase.php | 14 +++++++ 6 files changed, 153 insertions(+), 6 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/Fixtures/Post.php create mode 100644 tests/Unit/HasUuidTest.php create mode 100644 tests/Unit/TestCase.php diff --git a/.gitignore b/.gitignore index 3e7d7c4..2ef6f04 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ vendor/ composer.lock .DS_Store +.phpunit.cache/ +.phpunit.result.cache diff --git a/composer.json b/composer.json index 976beca..696e7f2 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "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": [ { @@ -9,16 +9,29 @@ "email": "david@kfoobar.se" } ], - "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" } } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..f2d176a --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,24 @@ + + + + + ./tests/Unit + + + + + + + + + + diff --git a/tests/Fixtures/Post.php b/tests/Fixtures/Post.php new file mode 100644 index 0000000..1a7a715 --- /dev/null +++ b/tests/Fixtures/Post.php @@ -0,0 +1,18 @@ +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); + } +} diff --git a/tests/Unit/TestCase.php b/tests/Unit/TestCase.php new file mode 100644 index 0000000..2b880a3 --- /dev/null +++ b/tests/Unit/TestCase.php @@ -0,0 +1,14 @@ +set('database.default', 'testing'); + } +}