Skip to content
Merged
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
5 changes: 4 additions & 1 deletion src/Mcp/Tools/ActiveThemeInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ private function listBladeTemplates(string $viewsPath, string $prefix = ''): arr
*/
private function directories(string $path): array
{
return array_values(array_filter(File::directories($path), is_string(...)));
/** @var list<string> $directories */
$directories = File::directories($path);

return $directories;
}
}
22 changes: 13 additions & 9 deletions src/Mcp/Tools/DiscoveredComponents.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,19 @@ public function handle(Request $request): Response

$type = $request->get('type');

if ($type && (! is_string($type) || ! isset(self::TYPE_MAP[$type]))) {
return Response::text('Invalid type. Valid types: '.implode(', ', array_keys(self::TYPE_MAP)));
$filteredMap = self::TYPE_MAP;

if ($type) {
if (! is_string($type) || ! isset(self::TYPE_MAP[$type])) {
return Response::text('Invalid type. Valid types: '.implode(', ', array_keys(self::TYPE_MAP)));
}

$filteredMap = [$type => self::TYPE_MAP[$type]];
}

/** @var DiscoveryManager $discoveryManager */
$discoveryManager = app(DiscoveryManager::class);

$filteredMap = is_string($type) && $type !== '' && isset(self::TYPE_MAP[$type])
? [$type => self::TYPE_MAP[$type]]
: self::TYPE_MAP;

$components = [];

foreach ($filteredMap as $typeName => $discoveryId) {
Expand Down Expand Up @@ -168,8 +170,10 @@ private function formatRecurrence(mixed $recurrence): string
return $this->formatRecurrence($recurrence->toScheduleArray());
}

return $recurrence === null || is_scalar($recurrence) || $recurrence instanceof Stringable
? (string) $recurrence
: throw new UnexpectedValueException('The recurrence of the schedule has no text.');
if ($recurrence !== null && ! is_scalar($recurrence) && ! $recurrence instanceof Stringable) {
throw new UnexpectedValueException('The recurrence of the schedule has no text.');
}

return Value::text($recurrence);
}
}
52 changes: 52 additions & 0 deletions tests/Feature/Support/ValueTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

use Pollora\Nectar\Support\Value;

/**
* A target with a static method, callable from its class name alone.
*/
final class NectarValueTarget
{
public static function answer(): string
{
return 'answered';
}
}

it('reads the text of a scalar or a stringable, and nothing from anything else', function (mixed $value, string $expected): void {
expect(Value::text($value))->toBe($expected);
})->with([
'string' => ['daily', 'daily'],
'integer' => [42, '42'],
'stringable' => [new class implements Stringable
{
public function __toString(): string
{
return 'as text';
}
}, 'as text'],
'null' => [null, ''],
'array' => [['a'], ''],
'plain object' => [new stdClass, ''],
]);

it('reads the integer of a scalar, and zero from anything else', function (mixed $value, int $expected): void {
expect(Value::integer($value))->toBe($expected);
})->with([
'numeric string' => ['25', 25],
'float' => [2.9, 2],
'null' => [null, 0],
'array' => [['25'], 0],
'plain object' => [new stdClass, 0],
]);

it('asks an object only for a method it has', function (): void {
expect(Value::ask(new NectarValueTarget, 'answer'))->toBe('answered')
->and(Value::ask(new NectarValueTarget, 'missing'))->toBeNull();
});

it('asks nothing of a class name, even one with a callable static method', function (): void {
expect(Value::ask(NectarValueTarget::class, 'answer'))->toBeNull();
});
154 changes: 154 additions & 0 deletions tests/Feature/Tools/UntypedDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
<?php

declare(strict_types=1);

use Laravel\Mcp\Request;
use Pollora\Discovery\Application\Services\DiscoveryManager;
use Pollora\Nectar\Mcp\Tools\DiscoveredComponents;
use Pollora\Nectar\Mcp\Tools\ModulesInfo;
use Pollora\Nectar\Mcp\Tools\PolloraStatus;
use Pollora\Nectar\Mcp\Tools\WpOption;
use Pollora\Nectar\Tests\Helpers\FakeDiscovery;

/*
* What the tools do with application data that does not have the expected shape.
*/

enum NectarUntypedSeconds: int
{
case Hour = 3600;
}

enum NectarUntypedFrequency
{
case Weekly;
}

/**
* @param array<string, mixed> $arguments
*/
function untypedDataPayload(object $tool, array $arguments = []): mixed
{
$content = (string) $tool->handle(new Request($arguments))->content();

return json_decode($content, true) ?? $content;
}

/**
* The details discovered_components gives a schedule declared with this recurrence.
*
* @return array<string, mixed>
*/
function untypedScheduleDetails(mixed $recurrence): array
{
$attribute = Mockery::mock();
$attribute->shouldReceive('newInstance')->andReturn((object) ['hook' => 'job', 'recurrence' => $recurrence]);

app()->instance(DiscoveryManager::class, FakeDiscovery::manager(['schedules' => [['attribute' => $attribute]]]));

return untypedDataPayload(new DiscoveredComponents, ['type' => 'schedule'])['components']['schedule'][0]['details'];
}

afterEach(function (): void {
app()->forgetInstance(DiscoveryManager::class);
@unlink(base_path('composer.lock'));
});

it('shows a recurrence declared as an enum or a stringable', function (mixed $recurrence, string $expected): void {
expect(untypedScheduleDetails($recurrence))->toBe(['hook' => 'job', 'recurrence' => $expected]);
})->with([
'integer-backed enum' => [NectarUntypedSeconds::Hour, '3600'],
'pure enum' => [NectarUntypedFrequency::Weekly, 'Weekly'],
'stringable' => [new class implements Stringable
{
public function __toString(): string
{
return 'fortnightly';
}
}, 'fortnightly'],
]);

it('gives no details for a recurrence that has no text', function (): void {
expect(untypedScheduleDetails(new stdClass))->toBe([]);
});

it('lists every component type when the type filter is empty', function (): void {
app()->instance(DiscoveryManager::class, FakeDiscovery::manager());

expect(array_keys(untypedDataPayload(new DiscoveredComponents, ['type' => ''])['components']))
->toBe(['post_type', 'taxonomy', 'hook', 'schedule', 'rest_route']);
});

it('refuses a type filter that is not text', function (): void {
app()->instance(DiscoveryManager::class, FakeDiscovery::manager());

expect(untypedDataPayload(new DiscoveredComponents, ['type' => ['hook']]))->toStartWith('Invalid type.');
});

it('reports no Pollora package from a composer.lock without a package list', function (string $lock): void {
file_put_contents(base_path('composer.lock'), $lock);

$payload = untypedDataPayload(new PolloraStatus);

expect($payload['pollora_version'])->toBe('unknown')
->and($payload['pollora_packages'])->toBe([]);
})->with([
'no packages key' => ['{}'],
'packages is not a list' => ['{"packages": "none"}'],
'not an object' => ['"lock"'],
'not JSON' => ['<<<<<<< HEAD'],
]);

it('skips the composer.lock entries without a text name and version, and reads the others', function (): void {
file_put_contents(base_path('composer.lock'), json_encode(['packages' => [
'garbage',
['version' => 'v1.0.0'],
['name' => 'pollora/unversioned'],
['name' => 'pollora/numbered', 'version' => 2],
['name' => 'pollora/framework', 'version' => 'v13.4.0'],
]]));

$payload = untypedDataPayload(new PolloraStatus);

expect($payload['pollora_version'])->toBe('v13.4.0')
->and($payload['pollora_packages'])->toBe([['name' => 'pollora/framework', 'version' => 'v13.4.0']]);
});

it('reads a module without the expected methods as empty and unknown', function (): void {
app()->instance('modules', new class
{
/**
* @return list<object>
*/
public function all(): array
{
return [new stdClass];
}
});

expect(untypedDataPayload(new ModulesInfo)['modules'])->toBe([
'' => ['name' => '', 'path' => '', 'enabled' => null],
]);
});

it('lists no module when the module manager cannot list them', function (): void {
app()->instance('modules', new stdClass);

expect(untypedDataPayload(new ModulesInfo))->toBe(['available' => true, 'count' => 0, 'modules' => []]);
});

it('refuses a WordPress security key even with spaces around its name', function (): void {
expect(untypedDataPayload(new WpOption, ['key' => ' nonce_key ']))->toBeString()->toContain('looks like a secret');
});

it('reads a secret-looking option allowed by a single name, spaces and case aside', function (): void {
config()->set('nectar.wp_option.allow', ' My_Api_Key ');

expect(untypedDataPayload(new WpOption, ['key' => ' my_api_key '])['key'])->toBe(' my_api_key ');
});

it('ignores the allow-list entries that are not an option name', function (): void {
config()->set('nectar.wp_option.allow', [null, 12, ' ', 'my_api_key']);

expect(untypedDataPayload(new WpOption, ['key' => 'my_api_key'])['key'])->toBe('my_api_key');
});
Loading