Skip to content

Commit e9daba7

Browse files
authored
Merge pull request #2 from K03413R/add_test_suite
Added test suite
2 parents 40eb501 + 6fcafb8 commit e9daba7

9 files changed

+220
-30
lines changed

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ By default, the global scope of this trait uses the `withoutArchived` extension
6666

6767
### Testing
6868

69-
Currently, a test suite doesn't exist for this package. However, I will be writing tests in line with Laravel's testing schemes for traits. If you wish to contribute any unit tests of your own, please refer to the [contribution guides](#-contributing) below
70-
69+
```composer test```
7170
### Changelog
7271

7372
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

composer.json

+2-1
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

+23-27
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

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

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

tests/TestClasses/ArchivableModel.php

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

tests/TestClasses/RegularModel.php

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

0 commit comments

Comments
 (0)