Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions commands/remove/command.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,3 @@
<?php

declare(strict_types = 1);

use Kirby\CLI\CLI;

return [
'description' => 'Removes a custom command',
'args' => [
'name' => [
'description' => 'The name of the command',
]
],
'command' => static function (CLI $cli): void {
$name = $cli->argOrPrompt('name', 'Enter a name for the command:');
$name = str_replace(':', '/', $name);

$global = $cli->root('commands.global') . '/' . $name . '.php';
$local = $cli->root('commands.local') . '/' . $name . '.php';

$files = [];

if (is_file($global) === true) {
$files[] = $global;
}

if (is_file($local) === true) {
$files[] = $local;
}

if (count($files) > 1) {
$input = $cli->checkboxes('Which commands do you want to delete?:', $files);
$trash = $input->prompt();
} else {
$trash = $files;
}

foreach ($trash as $file) {
unlink($file);
}

if (count($trash) === 1) {
$cli->success('The command has been deleted');
} else {
$cli->success('The commands have been deleted');
}
}
];
return Kirby\CLI\Commands\Remove\Command::class;
35 changes: 35 additions & 0 deletions src/CLI/BufferWriter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI;

use League\CLImate\Util\Writer\WriterInterface;

/**
* Buffer writer that captures CLI output for test assertions
*/
class BufferWriter implements WriterInterface
{
protected array $buffer = [];

public function clear(): void
{
$this->buffer = [];
}

public function getLines(): array
{
return $this->buffer;
}

public function getOutput(): string
{
return implode('', $this->buffer);
}

public function write($content): void
{
$this->buffer[] = $content;
}
}
61 changes: 61 additions & 0 deletions src/CLI/Commands/Remove/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands\Remove;

use Kirby\CLI\CLI;
use Kirby\CLI\Command as BaseCommand;

class Command extends BaseCommand
{
public static function args(): array
{
return [
'name' => [
'description' => 'The name of the command',
]
];
}

public static function command(CLI $cli): void
{
$name = $cli->argOrPrompt('name', 'Enter a name for the command:');
$name = str_replace(':', '/', $name);

$global = $cli->root('commands.global') . '/' . $name . '.php';
$local = $cli->root('commands.local') . '/' . $name . '.php';

$files = [];

if (is_file($global) === true) {
$files[] = $global;
}

if (is_file($local) === true) {
$files[] = $local;
}

if (count($files) > 1) {
$input = $cli->checkboxes('Which commands do you want to delete?:', $files);
$trash = $input->prompt();
} else {
$trash = $files;
}

foreach ($trash as $file) {
unlink($file);
}

if (count($trash) === 1) {
$cli->success('The command has been deleted');
} else {
$cli->success('The commands have been deleted');
}
}

public static function description(): string|null
{
return 'Removes a custom command';
}
}
48 changes: 19 additions & 29 deletions tests/CLI/BootstrapTest.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,40 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI;

use PHPUnit\Framework\Attributes\CoversFunction;

#[CoversFunction('bootstrap')]
#[CoversFunction('index')]
class BootstrapTest extends TestCase
{
/**
* @covers bootstrap
* @covers index
*/
public function testIndexInRoot()
public function testIndexInRoot(): void
{
chdir($root = __DIR__ . '/bootstrap/a');
chdir($root = __DIR__ . '/fixtures/bootstrap/a');
$this->assertSame($root . '/index.php', index());
$this->assertSame($root . '/index.php', bootstrap());
}

/**
* @covers bootstrap
* @covers index
*/
public function testIndexInWww()
{
chdir($root = __DIR__ . '/bootstrap/b');
$this->assertSame($root . '/www/index.php', index());
$this->assertSame($root . '/www/index.php', bootstrap());
}

/**
* @covers bootstrap
* @covers index
*/
public function testIndexInPublic()
public function testIndexInPublic(): void
{
chdir($root = __DIR__ . '/bootstrap/c');
chdir($root = __DIR__ . '/fixtures/bootstrap/c');
$this->assertSame($root . '/public/index.php', index());
$this->assertSame($root . '/public/index.php', bootstrap());
}

/**
* @covers bootstrap
* @covers index
*/
public function testIndexInPublicHtml()
public function testIndexInPublicHtml(): void
{
chdir($root = __DIR__ . '/bootstrap/d');
chdir($root = __DIR__ . '/fixtures/bootstrap/d');
$this->assertSame($root . '/public_html/index.php', index());
$this->assertSame($root . '/public_html/index.php', bootstrap());
}

public function testIndexInWww(): void
{
chdir($root = __DIR__ . '/fixtures/bootstrap/b');
$this->assertSame($root . '/www/index.php', index());
$this->assertSame($root . '/www/index.php', bootstrap());
}
}
12 changes: 6 additions & 6 deletions tests/CLI/CLITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class CLITest extends TestCase
{
public function setUp(): void
{
chdir(__DIR__);
chdir(__DIR__ . '/fixtures');
}

/**
Expand All @@ -36,7 +36,7 @@ public function testCommandsInDirectory()
$this->assertSame([], $commands);

// existing command directory
$commands = $cli->commandsInDirectory(__DIR__ . '/commands');
$commands = $cli->commandsInDirectory(__DIR__ . '/fixtures/commands');
$expected = [
'invalid-action',
'invalid-format',
Expand All @@ -55,11 +55,11 @@ public function testDir()
$cli = new CLI();

// current working directory
$this->assertSame(__DIR__, $cli->dir());
$this->assertSame(__DIR__ . '/fixtures', $cli->dir());

// relative
$this->assertSame(__DIR__ . '/./commands', $cli->dir('./commands'));
$this->assertSame(__DIR__ . '/../commands', $cli->dir('../commands'));
$this->assertSame(__DIR__ . '/fixtures/./commands', $cli->dir('./commands'));
$this->assertSame(__DIR__ . '/fixtures/../commands', $cli->dir('../commands'));

// absolute
$this->assertSame('/test', $cli->dir('/test'));
Expand Down Expand Up @@ -211,7 +211,7 @@ public function testRoot()

$this->assertSame(dirname(__DIR__, 2) . '/commands', $cli->root('commands.core'));
$this->assertSame($cli->home() . '/commands', $cli->root('commands.global'));
$this->assertSame(__DIR__ . '/commands', $cli->root('commands.local'));
$this->assertSame(__DIR__ . '/fixtures/commands', $cli->root('commands.local'));
}

/**
Expand Down
25 changes: 25 additions & 0 deletions tests/CLI/Commands/BackupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands;

use Kirby\CLI\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Backup::class)]
class BackupTest extends TestCase
{
public function testArgs(): void
{
$args = Backup::args();

$this->assertArrayHasKey('root', $args);
$this->assertSame('Selects the kirby root, which should be backuped', $args['root']['description']);
}

public function testDescription(): void
{
$this->assertSame('Creates backup of application files', Backup::description());
}
}
26 changes: 26 additions & 0 deletions tests/CLI/Commands/Clean/ContentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands\Clean;

use Kirby\CLI\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Content::class)]
class ContentTest extends TestCase
{
public function testArgs(): void
{
$args = Content::args();

$this->assertArrayHasKey('dry-run', $args);
$this->assertSame('Run the command without actually updating content', $args['dry-run']['description']);
$this->assertTrue($args['dry-run']['noValue']);
}

public function testDescription(): void
{
$this->assertSame('Deletes all fields from page, file or user content files that are not defined in the blueprint, no matter if they contain content or not.', Content::description());
}
}
25 changes: 25 additions & 0 deletions tests/CLI/Commands/Clear/CacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands\Clear;

use Kirby\CLI\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Cache::class)]
class CacheTest extends TestCase
{
public function testArgs(): void
{
$args = Cache::args();

$this->assertArrayHasKey('name', $args);
$this->assertSame('The name of the cache', $args['name']['description']);
}

public function testDescription(): void
{
$this->assertSame('Clears the cache', Cache::description());
}
}
22 changes: 22 additions & 0 deletions tests/CLI/Commands/Clear/LockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands\Clear;

use Kirby\CLI\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Lock::class)]
class LockTest extends TestCase
{
public function testArgs(): void
{
$this->assertSame([], Lock::args());
}

public function testDescription(): void
{
$this->assertSame('Deletes the content `.lock` files', Lock::description());
}
}
22 changes: 22 additions & 0 deletions tests/CLI/Commands/Clear/LoginsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types = 1);

namespace Kirby\CLI\Commands\Clear;

use Kirby\CLI\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(Logins::class)]
class LoginsTest extends TestCase
{
public function testArgs(): void
{
$this->assertSame([], Logins::args());
}

public function testDescription(): void
{
$this->assertSame('Deletes the users `.logins` file', Logins::description());
}
}
Loading
Loading