Skip to content

Commit

Permalink
detect dev for install command
Browse files Browse the repository at this point in the history
  • Loading branch information
chr-hertel committed Jan 12, 2024
1 parent 86e7e82 commit 943a092
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ These steps are provided for development purposes only.
```
3. Start the web server:
```bash
symfony serve
symfony serve -d
```
4. Start MySQL & Redis:
```bash
docker-compose up -d # or somehow run MySQL & Redis on localhost without docker
docker compose up -d # or somehow run MySQL & Redis on localhost without docker
```
5. Create 2 databases:
- `packagist` - for the web app
Expand Down
17 changes: 17 additions & 0 deletions src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,23 @@ public function getType(): string|null
return $this->type;
}

public function getInstallCommand(): string
{
$command = 'create-project';

if ('project' !== $this->getType()) {
$command = 'require';

/** @var Version $version */
$version = $this->getVersions()->first();
if ($version && $version->hasDevTag()) {
$command .= ' --dev';
}
}

return sprintf('composer %s %s', $command, $this->getName());
}

public function setRemoteId(string|null $remoteId): void
{
$this->remoteId = $remoteId;
Expand Down
6 changes: 6 additions & 0 deletions src/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ public function getName(): string
return $this->name;
}

public function isDev(): bool
{
// see Composer\Command\RequireCommand
return in_array(strtolower($this->name), ['dev', 'testing', 'static analysis'], true);
}

public function addVersions(Version $versions): void
{
$this->versions[] = $versions;
Expand Down
7 changes: 7 additions & 0 deletions src/Entity/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,13 @@ public function getTags(): Collection
return $this->tags;
}

public function hasDevTag(): bool
{
return [] !== array_filter($this->getTags()->toArray(), static function (Tag $tag) {
return $tag->isDev();
});
}

public function setUpdatedAt(DateTimeInterface $updatedAt): void
{
$this->updatedAt = $updatedAt;
Expand Down
2 changes: 1 addition & 1 deletion templates/package/view_package.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

<div class="row">
<div class="col-md-8">
<p class="requireme"><i class="glyphicon glyphicon-save"></i> <input type="text" readonly="readonly" value="composer {% if package.type == 'project' %}create-project{% else %}require{% endif %} {{ "#{package.vendor}/#{package.packageName}" }}" /></p>
<p class="requireme"><i class="glyphicon glyphicon-save"></i> <input type="text" readonly="readonly" value="{{ package.installCommand }}" /></p>

{% if not package.isAutoUpdated() and is_granted('update', package) %}
{% if "github.com" in package.repository %}
Expand Down
34 changes: 34 additions & 0 deletions tests/Entity/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
namespace App\Tests\Entity;

use App\Entity\Package;
use App\Entity\Tag;
use App\Entity\Version;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

class PackageTest extends TestCase
Expand All @@ -28,4 +31,35 @@ public function testWasUpdatedInTheLast24Hours(): void
$package->setUpdatedAt(new \DateTime('now'));
$this->assertTrue($package->wasUpdatedInTheLast24Hours());
}

#[DataProvider('providePackageScenarios')]
public function testInstallCommand(string $type, string $tag, string $expected): void
{
$version = new Version();
$version->addTag(new Tag('dev'));

$package = new Package();
$package->setName('vendor/name');
$package->setType('project');
$package->addVersion($version);

self::assertSame('composer create-project vendor/name', $package->getInstallCommand());
}

public static function providePackageScenarios(): array
{
return [
'project' => ['project', 'dev', 'composer create-project vendor/name'],
'library non-dev' => ['library', 'database', 'composer require vendor/name'],
'library dev' => ['library', 'testing', 'composer require --dev vendor/name'],
];
}

public function testInstallCommandWithoutVersion(): void
{
$package = new Package();
$package->setName('vendor/name');

self::assertSame('composer require vendor/name', $package->getInstallCommand());
}
}
46 changes: 46 additions & 0 deletions tests/Entity/TagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace App\Tests\Entity;

use App\Entity\Tag;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class TagTest extends TestCase
{
#[DataProvider('provideValidNames')]
public function testIsDevWithValidNames(string $name): void
{
$tag = new Tag($name);

self::assertTrue($tag->isDev());
}

public static function provideValidNames(): array
{
return [
['dev'],
['testing'],
['static analysis'],
];
}

#[DataProvider('provideInvalidNames')]
public function testIsDevWithInvalidNames(string $name): void
{
$tag = new Tag($name);

self::assertFalse($tag->isDev());
}

public static function provideInvalidNames(): array
{
return [
['orm'],
['project'],
['static-analysis'],
];
}
}
57 changes: 57 additions & 0 deletions tests/Entity/VersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace App\Tests\Entity;

use App\Entity\Tag;
use App\Entity\Version;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

final class VersionTest extends TestCase
{
#[DataProvider('provideValidDevTagSets')]
public function testHasDevTagWith(array $tags): void
{
$version = new Version();

foreach ($tags as $tag) {
$version->addTag(new Tag($tag));
}

self::assertTrue($version->hasDevTag());
}

public static function provideValidDevTagSets(): array
{
return [
'only dev' => [['dev']],
'dev first' => [['dev', 'database']],
'dev last' => [['database', 'dev']],
'dev middle' => [['orm', 'dev', 'database']],
'multiple' => [['dev', 'testing']],
];
}
#[DataProvider('provideInvalidDevTagSets')]
public function testHasDevTagWithout(array $tags): void
{
$version = new Version();

foreach ($tags as $tag) {
$version->addTag(new Tag($tag));
}

self::assertFalse($version->hasDevTag());
}

public static function provideInvalidDevTagSets(): array
{
return [
'none' => [[]],
'one' => [['orm']],
'two' => [['database', 'orm']],
'three' => [['currency', 'database', 'clock']],
];
}
}

0 comments on commit 943a092

Please sign in to comment.