Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detect dev for install command #1413

Merged
merged 3 commits into from
Mar 11, 2024
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
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
15 changes: 15 additions & 0 deletions src/Entity/Package.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,21 @@ public function getType(): string|null
return $this->type;
}

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

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

if (null !== $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
11 changes: 11 additions & 0 deletions src/Entity/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,17 @@ public function getTags(): Collection
return $this->tags;
}

public function hasDevTag(): bool
{
foreach ($this->getTags() as $tag) {
if ($tag->isDev()) {
return true;
}
}

return false;
}

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(expandedVersion) }}" /></p>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this use version instead of expandedVersion to be consistent with the sidebar showing the support action.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes true it should be version, I'll fix that.


{% if not package.isAutoUpdated() and is_granted('update', package) %}
{% if "github.com" in package.repository %}
Expand Down
33 changes: 33 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,34 @@ 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($tag));

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

self::assertSame($expected, $package->getInstallCommand($version));
}

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']],
];
}
}
Loading