-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from K03413R/add_test_suite
Added test suite
- Loading branch information
Showing
9 changed files
with
220 additions
and
30 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
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 |
---|---|---|
@@ -1,29 +1,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false"> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist> | ||
<directory suffix=".php">src/</directory> | ||
</whitelist> | ||
</filter> | ||
<logging> | ||
<log type="tap" target="build/report.tap"/> | ||
<log type="junit" target="build/report.junit.xml"/> | ||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/> | ||
<log type="coverage-text" target="build/coverage.txt"/> | ||
<log type="coverage-clover" target="build/logs/clover.xml"/> | ||
</logging> | ||
<phpunit | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
bootstrap="vendor/autoload.php" | ||
backupGlobals="false" | ||
backupStaticAttributes="false" | ||
colors="true" | ||
verbose="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd" | ||
> | ||
<coverage/> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory>tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<logging> | ||
<junit outputFile="build/report.junit.xml"/> | ||
</logging> | ||
</phpunit> |
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,71 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests; | ||
|
||
use LaravelArchivable\Tests\TestClasses\ArchivableModel; | ||
use LaravelArchivable\Tests\TestClasses\RegularModel; | ||
|
||
class ArchivableTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function a_model_can_be_archived() | ||
{ | ||
$model = ArchivableModel::factory()->create(); | ||
|
||
$this->assertNull($model->fresh()->archived_at); | ||
|
||
$model->archive(); | ||
|
||
$this->assertNotNull($model->fresh()->archived_at); | ||
} | ||
|
||
/** @test */ | ||
public function a_model_can_be_unarchived() | ||
{ | ||
$model = ArchivableModel::factory()->archived()->create(); | ||
|
||
$this->assertNotNull($model->fresh()->archived_at); | ||
|
||
$model->unarchive(); | ||
|
||
$this->assertNull($model->fresh()->archived_at); | ||
} | ||
|
||
/** @test */ | ||
public function a_model_cannot_be_queried_normally_when_archived() | ||
{ | ||
ArchivableModel::factory()->archived()->create(); | ||
|
||
ArchivableModel::factory()->create(); | ||
|
||
$this->assertDatabaseCount('archivable_models', 2); | ||
|
||
$this->assertCount(1, ArchivableModel::all()); | ||
} | ||
|
||
/** @test */ | ||
public function all_models_can_be_found_with_the_withArchived_scope() | ||
{ | ||
ArchivableModel::factory()->archived()->create(); | ||
ArchivableModel::factory()->create(); | ||
|
||
$this->assertCount(2, ArchivableModel::withArchived()->get()); | ||
} | ||
|
||
/** @test */ | ||
public function only_archived_models_can_be_found_with_the_onlyArchived_scope() | ||
{ | ||
ArchivableModel::factory()->archived()->create(); | ||
ArchivableModel::factory()->create(); | ||
|
||
$this->assertCount(1, ArchivableModel::onlyArchived()->get()); | ||
} | ||
|
||
/** @test */ | ||
public function models_without_the_archivable_trait_are_not_scoped() | ||
{ | ||
RegularModel::factory()->create(); | ||
|
||
$this->assertCount(1, RegularModel::all()); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use LaravelArchivable\Tests\TestClasses\ArchivableModel; | ||
|
||
class ArchivableModelFactory extends Factory | ||
{ | ||
protected $model = ArchivableModel::class; | ||
|
||
public function archived() | ||
{ | ||
return $this->state(function (array $attributes) { | ||
return [ | ||
'archived_at' => now(), | ||
]; | ||
}); | ||
} | ||
|
||
public function definition() | ||
{ | ||
return []; | ||
} | ||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use LaravelArchivable\Tests\TestClasses\RegularModel; | ||
|
||
class RegularModelFactory extends Factory | ||
{ | ||
protected $model = RegularModel::class; | ||
|
||
public function definition() | ||
{ | ||
return []; | ||
} | ||
} |
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,58 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use LaravelArchivable\LaravelArchivableServiceProvider; | ||
use Orchestra\Testbench\TestCase as Orchestra; | ||
|
||
class TestCase extends Orchestra | ||
{ | ||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
Factory::guessFactoryNamesUsing( | ||
function (string $modelName) { | ||
return 'LaravelArchivable\\Tests\\Database\\Factories\\'.class_basename($modelName).'Factory'; | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
// Setup default database to use sqlite :memory: | ||
$app['config']->set('database.default', 'testbench'); | ||
$app['config']->set('database.connections.testbench', [ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
'prefix' => '', | ||
]); | ||
|
||
Schema::create('archivable_models', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->timestamps(); | ||
$table->timestamp('archived_at', 0)->nullable(); | ||
}); | ||
|
||
Schema::create('regular_models', function (Blueprint $table) { | ||
$table->increments('id'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return [ | ||
LaravelArchivableServiceProvider::class, | ||
]; | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests\TestClasses; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use LaravelArchivable\Archivable; | ||
|
||
class ArchivableModel extends Model | ||
{ | ||
use Archivable; | ||
use HasFactory; | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace LaravelArchivable\Tests\TestClasses; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class RegularModel extends Model | ||
{ | ||
use HasFactory; | ||
} |