Skip to content

Commit e6f7f67

Browse files
committed
Added test suite
1 parent 40eb501 commit e6f7f67

File tree

8 files changed

+228
-28
lines changed

8 files changed

+228
-28
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
},
2323
"require-dev": {
2424
"illuminate/pagination": "^8.0",
25+
"orchestra/testbench": "^6.3",
2526
"phpunit/phpunit": "^8.4|^9.0"
2627
},
2728
"autoload": {
@@ -35,7 +36,7 @@
3536
}
3637
},
3738
"scripts": {
38-
"test": "vendor/bin/phpunit",
39+
"test": "vendor/bin/phpunit --colors=always",
3940
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
4041

4142
},

phpunit.xml.dist

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
4-
backupStaticAttributes="false"
5-
colors="true"
6-
verbose="true"
7-
convertErrorsToExceptions="true"
8-
convertNoticesToExceptions="true"
9-
convertWarningsToExceptions="true"
10-
processIsolation="false"
11-
stopOnFailure="false">
12-
<testsuites>
13-
<testsuite name="Test Suite">
14-
<directory>tests</directory>
15-
</testsuite>
16-
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
backupGlobals="false"
6+
backupStaticAttributes="false"
7+
colors="true"
8+
verbose="true"
9+
convertErrorsToExceptions="true"
10+
convertNoticesToExceptions="true"
11+
convertWarningsToExceptions="true"
12+
processIsolation="false"
13+
stopOnFailure="false"
14+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
15+
>
16+
<coverage/>
17+
<testsuites>
18+
<testsuite name="Test Suite">
19+
<directory>tests</directory>
20+
</testsuite>
21+
</testsuites>
22+
<logging>
23+
<junit outputFile="build/report.junit.xml"/>
24+
</logging>
2925
</phpunit>

tests/ArchivableTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
4+
namespace LaravelArchivable\Tests;
5+
6+
use LaravelArchivable\Tests\TestClasses\ArchivableModel;
7+
use LaravelArchivable\Tests\TestClasses\RegularModel;
8+
9+
class ArchivableTest extends TestCase
10+
{
11+
/** @test */
12+
function a_model_can_be_archived()
13+
{
14+
$model = ArchivableModel::factory()->create();
15+
16+
$this->assertNull($model->fresh()->archived_at);
17+
18+
$model->archive();
19+
20+
$this->assertNotNull($model->fresh()->archived_at);
21+
}
22+
23+
/** @test */
24+
function a_model_can_be_unarchived()
25+
{
26+
$model = ArchivableModel::factory()->archived()->create();
27+
28+
$this->assertNotNull($model->fresh()->archived_at);
29+
30+
$model->unarchive();
31+
32+
$this->assertNull($model->fresh()->archived_at);
33+
}
34+
35+
/** @test */
36+
function a_model_cannot_be_queried_normally_when_archived()
37+
{
38+
ArchivableModel::factory()->archived()->create();
39+
40+
ArchivableModel::factory()->create();
41+
42+
$this->assertDatabaseCount('archivable_models', 2);
43+
44+
$this->assertCount(1, ArchivableModel::all());
45+
}
46+
47+
/** @test */
48+
function all_models_can_be_found_with_the_withArchived_scope()
49+
{
50+
ArchivableModel::factory()->archived()->create();
51+
ArchivableModel::factory()->create();
52+
53+
$this->assertCount(2, ArchivableModel::withArchived()->get());
54+
}
55+
56+
/** @test */
57+
function only_archived_models_can_be_found_with_the_onlyArchived_scope()
58+
{
59+
ArchivableModel::factory()->archived()->create();
60+
ArchivableModel::factory()->create();
61+
62+
$this->assertCount(1, ArchivableModel::onlyArchived()->get());
63+
}
64+
65+
/** @test */
66+
function models_without_the_archivable_trait_are_not_scoped()
67+
{
68+
RegularModel::factory()->create();
69+
70+
$this->assertCount(1, RegularModel::all());
71+
}
72+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace LaravelArchivable\Tests\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use LaravelArchivable\Tests\TestClasses\ArchivableModel;
7+
8+
class ArchivableModelFactory extends Factory
9+
{
10+
protected $model = ArchivableModel::class;
11+
12+
public function archived()
13+
{
14+
return $this->state(function (array $attributes) {
15+
return [
16+
'archived_at' => now(),
17+
];
18+
});
19+
}
20+
21+
public function definition()
22+
{
23+
return [];
24+
}
25+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace LaravelArchivable\Tests\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use LaravelArchivable\Tests\TestClasses\RegularModel;
7+
8+
class RegularModelFactory extends Factory
9+
{
10+
protected $model = RegularModel::class;
11+
12+
public function definition()
13+
{
14+
return [];
15+
}
16+
}

tests/TestCase.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
4+
namespace LaravelArchivable\Tests;
5+
6+
use Illuminate\Database\Eloquent\Factories\Factory;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Facades\Schema;
9+
use LaravelArchivable\LaravelArchivableServiceProvider;
10+
use Orchestra\Testbench\TestCase as Orchestra;
11+
12+
13+
class TestCase extends Orchestra
14+
{
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
Factory::guessFactoryNamesUsing(
21+
function(string $modelName){
22+
return 'LaravelArchivable\\Tests\\Database\\Factories\\' . class_basename($modelName) . 'Factory';
23+
}
24+
);
25+
}
26+
27+
/**
28+
* Define environment setup.
29+
*
30+
* @param \Illuminate\Foundation\Application $app
31+
* @return void
32+
*/
33+
protected function getEnvironmentSetUp($app)
34+
{
35+
// Setup default database to use sqlite :memory:
36+
$app['config']->set('database.default', 'testbench');
37+
$app['config']->set('database.connections.testbench', [
38+
'driver' => 'sqlite',
39+
'database' => ':memory:',
40+
'prefix' => '',
41+
]);
42+
43+
Schema::create('archivable_models', function (Blueprint $table) {
44+
$table->increments('id');
45+
$table->timestamps();
46+
$table->timestamp('archived_at', 0)->nullable();
47+
});
48+
49+
Schema::create('regular_models', function (Blueprint $table) {
50+
$table->increments('id');
51+
$table->timestamps();
52+
});
53+
}
54+
55+
protected function getPackageProviders($app)
56+
{
57+
return [
58+
LaravelArchivableServiceProvider::class,
59+
];
60+
}
61+
}

tests/TestClasses/ArchivableModel.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
4+
namespace LaravelArchivable\Tests\TestClasses;
5+
6+
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
use LaravelArchivable\Archivable;
10+
11+
12+
class ArchivableModel extends Model
13+
{
14+
use Archivable;
15+
use HasFactory;
16+
}

tests/TestClasses/RegularModel.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
4+
namespace LaravelArchivable\Tests\TestClasses;
5+
6+
7+
use Illuminate\Database\Eloquent\Factories\HasFactory;
8+
use Illuminate\Database\Eloquent\Model;
9+
10+
class RegularModel extends Model
11+
{
12+
use HasFactory;
13+
}

0 commit comments

Comments
 (0)