Passed
Push — master ( a80842...bbbee0 )
by Caen
03:37 queued 13s
created

SeedPublicationCommandTest   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Testing\Feature;
6
7
use function glob;
8
9
use Hyde\Hyde;
10
use Hyde\Publications\Models\PublicationType;
11
use Hyde\Testing\TestCase;
12
13
/**
14
 * @covers \Hyde\Publications\Commands\SeedPublicationCommand
15
 * @covers \Hyde\Publications\Actions\SeedsPublicationFiles
16
 */
17
class SeedPublicationCommandTest extends TestCase
18
{
19
    protected function setUp(): void
20
    {
21
        parent::setUp();
22
23
        $this->directory('test-publication');
24
        $this->pubType = new PublicationType('Test Publication');
25
        $this->pubType->save();
26
    }
27
28
    public function test_can_seed_publications()
29
    {
30
        $this->artisan('seed:publications')
31
            ->expectsOutputToContain('Seeding new publications!')
32
            ->expectsQuestion('Which publication type would you like to seed?', 'test-publication')
33
            ->expectsQuestion('How many publications would you like to generate', 1)
34
            ->expectsOutputToContain('1 publication for Test Publication created!')
35
            ->assertExitCode(0);
36
37
        $this->assertPublicationsCreated();
38
    }
39
40
    public function test_can_seed_publications_using_arguments()
41
    {
42
        $this->artisan('seed:publications test-publication 1')
43
             ->expectsOutputToContain('Seeding new publications!')
44
             ->assertExitCode(0);
45
46
        $this->assertPublicationsCreated();
47
    }
48
49
    public function test_can_seed_multiple_publications()
50
    {
51
        $this->artisan('seed:publications test-publication 2')
52
             ->expectsOutputToContain('Seeding new publications!')
53
             ->expectsOutputToContain('2 publications for Test Publication created!')
54
             ->assertExitCode(0);
55
56
        $this->assertPublicationsCreated(2);
57
    }
58
59
    public function test_command_asks_to_confirm_before_creating_many_publications()
60
    {
61
        $this->artisan('seed:publications')
62
             ->expectsOutputToContain('Seeding new publications!')
63
             ->expectsQuestion('Which publication type would you like to seed?', 'test-publication')
64
             ->expectsQuestion('How many publications would you like to generate', 10000)
65
             ->expectsOutputToContain('Warning: Generating a large number of publications may take a while. Expected time: 10 seconds.')
66
             ->expectsConfirmation('Are you sure you want to continue?', false)
67
             ->assertExitCode(130);
68
69
        $this->assertPublicationsCreated(0);
70
    }
71
72
    public function test_command_asks_to_confirm_before_creating_many_publications_when_using_arguments()
73
    {
74
        $this->artisan('seed:publications test-publication 10000')
75
             ->expectsOutputToContain('Seeding new publications!')
76
             ->expectsOutputToContain('Warning: Generating a large number of publications may take a while. Expected time: 10 seconds.')
77
             ->expectsConfirmation('Are you sure you want to continue?', false)
78
             ->assertExitCode(130);
79
80
        $this->assertPublicationsCreated(0);
81
    }
82
83
    public function test_with_invalid_publication_type()
84
    {
85
        $this->artisan('seed:publications invalid-publication')
86
            ->expectsOutput('Error: Unable to locate publication type [invalid-publication]')
87
            ->assertExitCode(1);
88
    }
89
90
    public function test_with_no_publication_types()
91
    {
92
        unlink(Hyde::path('test-publication/schema.json'));
93
        $this->artisan('seed:publications')
94
            ->expectsOutput('Error: Unable to locate any publication types. Did you create any?')
95
            ->assertExitCode(1);
96
    }
97
98
    protected function assertPublicationsCreated(int $expectedCount = 1): void
99
    {
100
        $this->assertCount($expectedCount, glob(Hyde::path('test-publication/*.md')));
101
    }
102
}
103