Skip to content

Commit 69683d5

Browse files
committed
Sorting: Added tests to cover AssignSortSetCommand
1 parent 37d020c commit 69683d5

File tree

4 files changed

+147
-2
lines changed

4 files changed

+147
-2
lines changed

app/Console/Commands/AssignSortSetCommand.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function handle(BookSorter $sorter): int
5050
}
5151
$query = Book::query()->where('sort_set_id', $sortId);
5252
} else {
53-
$this->error("Either the --all-books or --books-without-sort option must be provided!");
53+
$this->error("No option provided to specify target. Run with the -h option to see all available options.");
5454
return 1;
5555
}
5656

@@ -79,7 +79,7 @@ public function handle(BookSorter $sorter): int
7979
$processed = $max;
8080
});
8181

82-
$this->info("Sort applied to {$processed} books!");
82+
$this->info("Sort applied to {$processed} book(s)!");
8383

8484
return 0;
8585
}

app/Sorting/SortSet.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Entities\Models\Book;
77
use Carbon\Carbon;
88
use Illuminate\Database\Eloquent\Collection;
9+
use Illuminate\Database\Eloquent\Factories\HasFactory;
910
use Illuminate\Database\Eloquent\Model;
1011
use Illuminate\Database\Eloquent\Relations\HasMany;
1112

@@ -18,6 +19,8 @@
1819
*/
1920
class SortSet extends Model implements Loggable
2021
{
22+
use HasFactory;
23+
2124
/**
2225
* @return SortSetOperation[]
2326
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Database\Factories\Sorting;
4+
5+
use BookStack\Sorting\SortSet;
6+
use BookStack\Sorting\SortSetOperation;
7+
use Illuminate\Database\Eloquent\Factories\Factory;
8+
9+
class SortSetFactory extends Factory
10+
{
11+
/**
12+
* The name of the factory's corresponding model.
13+
*
14+
* @var string
15+
*/
16+
protected $model = SortSet::class;
17+
18+
/**
19+
* Define the model's default state.
20+
*/
21+
public function definition(): array
22+
{
23+
$cases = SortSetOperation::cases();
24+
$op = $cases[array_rand($cases)];
25+
return [
26+
'name' => $op->name . ' Sort',
27+
'sequence' => $op->value,
28+
];
29+
}
30+
}
+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Commands;
4+
5+
use BookStack\Entities\Models\Book;
6+
use BookStack\Sorting\SortSet;
7+
use Tests\TestCase;
8+
9+
class AssignSortSetCommandTest extends TestCase
10+
{
11+
public function test_no_given_sort_set_lists_options()
12+
{
13+
$sortSets = SortSet::factory()->createMany(10);
14+
15+
$commandRun = $this->artisan('bookstack:assign-sort-set')
16+
->expectsOutputToContain('Sort set ID required!')
17+
->assertExitCode(1);
18+
19+
foreach ($sortSets as $sortSet) {
20+
$commandRun->expectsOutputToContain("{$sortSet->id}: {$sortSet->name}");
21+
}
22+
}
23+
24+
public function test_run_without_options_advises_help()
25+
{
26+
$this->artisan("bookstack:assign-sort-set 100")
27+
->expectsOutput("No option provided to specify target. Run with the -h option to see all available options.")
28+
->assertExitCode(1);
29+
}
30+
31+
public function test_run_without_valid_sort_advises_help()
32+
{
33+
$this->artisan("bookstack:assign-sort-set 100342 --all-books")
34+
->expectsOutput("Sort set of provided id 100342 not found!")
35+
->assertExitCode(1);
36+
}
37+
38+
public function test_confirmation_required()
39+
{
40+
$sortSet = SortSet::factory()->create();
41+
42+
$this->artisan("bookstack:assign-sort-set {$sortSet->id} --all-books")
43+
->expectsConfirmation('Are you sure you want to continue?', 'no')
44+
->assertExitCode(1);
45+
46+
$booksWithSort = Book::query()->whereNotNull('sort_set_id')->count();
47+
$this->assertEquals(0, $booksWithSort);
48+
}
49+
50+
public function test_assign_to_all_books()
51+
{
52+
$sortSet = SortSet::factory()->create();
53+
$booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
54+
$this->assertGreaterThan(0, $booksWithoutSort);
55+
56+
$this->artisan("bookstack:assign-sort-set {$sortSet->id} --all-books")
57+
->expectsOutputToContain("This will apply sort set [{$sortSet->id}: {$sortSet->name}] to {$booksWithoutSort} book(s)")
58+
->expectsConfirmation('Are you sure you want to continue?', 'yes')
59+
->expectsOutputToContain("Sort applied to {$booksWithoutSort} book(s)")
60+
->assertExitCode(0);
61+
62+
$booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
63+
$this->assertEquals(0, $booksWithoutSort);
64+
}
65+
66+
public function test_assign_to_all_books_without_sort()
67+
{
68+
$totalBooks = Book::query()->count();
69+
$book = $this->entities->book();
70+
$sortSetA = SortSet::factory()->create();
71+
$sortSetB = SortSet::factory()->create();
72+
$book->sort_set_id = $sortSetA->id;
73+
$book->save();
74+
75+
$booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
76+
$this->assertEquals($totalBooks, $booksWithoutSort + 1);
77+
78+
$this->artisan("bookstack:assign-sort-set {$sortSetB->id} --books-without-sort")
79+
->expectsConfirmation('Are you sure you want to continue?', 'yes')
80+
->expectsOutputToContain("Sort applied to {$booksWithoutSort} book(s)")
81+
->assertExitCode(0);
82+
83+
$booksWithoutSort = Book::query()->whereNull('sort_set_id')->count();
84+
$this->assertEquals(0, $booksWithoutSort);
85+
$this->assertEquals($totalBooks, $sortSetB->books()->count() + 1);
86+
}
87+
88+
public function test_assign_to_all_books_with_sort()
89+
{
90+
$book = $this->entities->book();
91+
$sortSetA = SortSet::factory()->create();
92+
$sortSetB = SortSet::factory()->create();
93+
$book->sort_set_id = $sortSetA->id;
94+
$book->save();
95+
96+
$this->artisan("bookstack:assign-sort-set {$sortSetB->id} --books-with-sort={$sortSetA->id}")
97+
->expectsConfirmation('Are you sure you want to continue?', 'yes')
98+
->expectsOutputToContain("Sort applied to 1 book(s)")
99+
->assertExitCode(0);
100+
101+
$book->refresh();
102+
$this->assertEquals($sortSetB->id, $book->sort_set_id);
103+
$this->assertEquals(1, $sortSetB->books()->count());
104+
}
105+
106+
public function test_assign_to_all_books_with_sort_id_is_validated()
107+
{
108+
$this->artisan("bookstack:assign-sort-set 50 --books-with-sort=beans")
109+
->expectsOutputToContain("Provided --books-with-sort option value is invalid")
110+
->assertExitCode(1);
111+
}
112+
}

0 commit comments

Comments
 (0)