Skip to content

Commit

Permalink
Tests the remote github repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Halleck45 committed Jan 31, 2025
1 parent 263942c commit 8d69112
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 9 deletions.
1 change: 0 additions & 1 deletion src/Toolkit/.phpunit.cache/test-results

This file was deleted.

3 changes: 3 additions & 0 deletions src/Toolkit/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,8 @@
"name": "symfony/ux",
"url": "https://github.com/symfony/ux"
}
},
"require-dev": {
"symfony/http-client": "6.4|^7.0"
}
}
182 changes: 178 additions & 4 deletions src/Toolkit/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/Toolkit/src/ComponentRepository/GithubRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,24 @@

namespace Symfony\Ux\Toolkit\ComponentRepository;

use Symfony\Component\HttpClient\HttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;

/**
* @author Jean-François Lépine
*
* @internal
*/
class GithubRepository implements ComponentRepository
{
public function __construct(
private readonly ?HttpClientInterface $httpClient = null,
) {
if (!class_exists(HttpClient::class)) {
throw new \LogicException('You must install "symfony/http-client" to use ux-toolkit with remote component. Try running "composer require symfony/http-client".');
}
}

public function getContent(ComponentIdentity $component): string
{
$url = \sprintf(
Expand All @@ -28,6 +39,8 @@ public function getContent(ComponentIdentity $component): string
$component->getName()
);

return file_get_contents($url);
$response = $this->httpClient->request('GET', $url);

return $response->getContent();
}
}
10 changes: 7 additions & 3 deletions src/Toolkit/src/UxToolkitBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/
class UxToolkitBundle extends AbstractBundle
{

public function build(ContainerBuilder $container): void
{
parent::build($container);
Expand All @@ -35,6 +34,7 @@ public function build(ContainerBuilder $container): void
$container->autowire(RepositoryFactory::class);
$container->autowire(ComponentIdentifier::class);

// Prepare command
$container->autowire(UxToolkitInstallCommand::class);
$container
->registerForAutoconfiguration(UxToolkitInstallCommand::class)
Expand All @@ -45,7 +45,11 @@ public function build(ContainerBuilder $container): void
->setPublic(true)
->addTag('console.command')
;
}

// Inject http client (if exists) to github repository
if($container->has('http_client')) {
$container->getDefinition(GithubRepository::class)
->setArgument('$httpClient', $container->get('http_client'));
}
}
}

36 changes: 36 additions & 0 deletions src/Toolkit/tests/ComponentRepository/GithubRepositoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Ux\Toolkit\Tests\ComponentRepository;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Ux\Toolkit\ComponentRepository\ComponentIdentity;
use Symfony\Ux\Toolkit\ComponentRepository\GithubRepository;

/**
* @author Jean-François Lépine
*/
class GithubRepositoryTest extends TestCase
{
public function testGithubRepositoryUseClientAndTryToDownloadRemoteFile(): void
{
$client = new MockHttpClient();
$client->setResponseFactory(fn() => new MockResponse('My badge content from github'));
$repository = new GithubRepository($client);

$component = new ComponentIdentity('Halleck45', 'ux-toolkit', 'Badge', '1.0.0');
$content = $repository->getContent($component);

$this->assertEquals('My badge content from github', $content);
}
}

0 comments on commit 8d69112

Please sign in to comment.