Skip to content

Commit

Permalink
fix Twig extension (#1370)
Browse files Browse the repository at this point in the history
  • Loading branch information
garak authored Mar 7, 2023
1 parent 6ee998d commit 6674793
Show file tree
Hide file tree
Showing 9 changed files with 29 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ jobs:
with:
php-version: 8.1
- run: |
composer require --no-update liip/imagine-bundle:"^2.8" phpstan/extension-installer phpstan/phpstan-phpunit
composer require --no-update liip/imagine-bundle:"^2.8" phpstan/extension-installer phpstan/phpstan-phpunit phpstan/phpstan:1.9.*
composer config --no-plugins allow-plugins.phpstan/extension-installer true
composer install --ignore-platform-reqs
vendor/bin/phpstan
XDEBUG_MODE=off vendor/bin/phpstan
cs-fixer:
runs-on: ubuntu-22.04
name: PHP-CS-Fixer
Expand Down
4 changes: 4 additions & 0 deletions config/twig.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@

<service id="Vich\UploaderBundle\Twig\Extension\UploaderExtension" public="false">
<tag name="twig.extension" />
</service>

<service id="Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime" public="false">
<argument type="service" id="Vich\UploaderBundle\Templating\Helper\UploaderHelper" />
<tag name="twig.runtime" />
</service>

</services>
Expand Down
20 changes: 6 additions & 14 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,42 +172,34 @@ class Product
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\Column()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private ?int $id = null;
// ... other fields
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="products", fileNameProperty="imageName", size="imageSize")
*
* @var File|null
*/
private $imageFile;
private ?File $imageFile;
/**
* @ORM\Column(nullable="true")
*
* @var string|null
*/
private $imageName;
private ?string $imageName = null;
/**
* @ORM\Column(nullable="true")
*
* @var int|null
*/
private $imageSize;
private ?int $imageSize = null;
/**
* @ORM\Column(nullable="true")
*
* @var \DateTimeImmutable|null
*/
private $updatedAt;
private ?\DateTimeImmutable $updatedAt = null;
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,3 @@ parameters:
count: 4
path: tests/TestCase.php

-
message: "#^Return type of call to method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) contains unresolvable type\\.$#"
count: 1
path: tests/Twig/Extension/UploaderExtensionTest.php

2 changes: 2 additions & 0 deletions tests/Fixtures/App/templates/default/edit.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

{% block body %}
{{ form(form, {'action': path('upload_edit', {'formType': formType, 'imageId': imageId}), 'method': 'POST'}) }}

{{ vich_uploader_asset(image) }}
{% endblock %}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public function editAction(string $formType, ?int $imageId): Response
'imageId' => $imageId,
'formType' => $formType,
'form' => $form->createView(),
'image' => $this->getImage($imageId),
]);
}

Expand All @@ -70,10 +71,10 @@ public function submitAction(Request $request, string $formType, ?int $imageId =
$em->persist($image);
$em->flush();

return $this->redirect($this->generateUrl('view', [
return $this->redirectToRoute('view', [
'formType' => $formType,
'imageId' => $image->getId(),
]));
]);
}

return $this->render('default/upload.html.twig', [
Expand All @@ -93,10 +94,10 @@ public function submitWithPropertyPathAction(Request $request, string $formType,
$em->persist($image);
$em->flush();

return $this->redirect($this->generateUrl('view_with_property_path', [
return $this->redirectToRoute('view_with_property_path', [
'formType' => $formType,
'imageId' => $image->getId(),
]));
]);
}

return $this->render('default/upload_with_property_path.html.twig', [
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/UploadTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ final class UploadTest extends WebTestCase
*/
public function testFileIsUploadedWithFileType(string $uploadType, string $imageFieldName): void
{
$client = static::createClient();
$client = self::createClient();
$this->loadFixtures($client);

$crawler = $client->request('GET', \sprintf('/%s/vich_file', $uploadType));
Expand Down Expand Up @@ -50,7 +50,7 @@ public function testFileIsUploadedWithFileType(string $uploadType, string $image
*/
public function testFileIsUploadedWithImageType(string $uploadType, string $imageFieldName): void
{
$client = static::createClient();
$client = self::createClient();
$this->loadFixtures($client);

$crawler = $client->request('GET', \sprintf('/%s/vich_image', $uploadType));
Expand All @@ -59,7 +59,7 @@ public function testFileIsUploadedWithImageType(string $uploadType, string $imag
$form = $crawler->selectButton('form_save')->form();
$image = $this->getUploadedFile($client, 'symfony_black_03.png');

$crawler = $client->submit($form, [
$client->submit($form, [
'form' => [
'title' => 'Test image',
$imageFieldName => ['file' => $image],
Expand Down Expand Up @@ -89,7 +89,7 @@ public function testFileIsUploadedWithImageType(string $uploadType, string $imag
/**
* @return array<array{string, string}>
*/
public function uploadTypeDataProvider(): array
public static function uploadTypeDataProvider(): array
{
return [
['upload', 'imageFile'],
Expand Down
8 changes: 4 additions & 4 deletions tests/Functional/WebTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected function getUploadedFile(KernelBrowser $client, string $name, string $
return new UploadedFile(
$this->getImagesDir($client).\DIRECTORY_SEPARATOR.$name,
$name,
$mimeType
$mimeType,
);
}

Expand Down Expand Up @@ -62,12 +62,12 @@ protected function loadFixtures(KernelBrowser $client): void
throw new \InvalidArgumentException("Connection does not contain a 'path' or 'dbname' parameter and cannot be dropped.");
}

$metadatas = $om->getMetadataFactory()->getAllMetadata();
$metadata = $om->getMetadataFactory()->getAllMetadata();

$schemaTool = new SchemaTool($om);
$schemaTool->dropDatabase();
if (!empty($metadatas)) {
$schemaTool->createSchema($metadatas);
if (!empty($metadata)) {
$schemaTool->createSchema($metadata);
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Twig/Extension/UploaderExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Vich\UploaderBundle\Tests\Twig\Extension;

use Vich\UploaderBundle\Templating\Helper\UploaderHelper;
use Vich\UploaderBundle\Templating\Helper\UploaderHelperInterface;
use Vich\UploaderBundle\Tests\TestCase;
use Vich\UploaderBundle\Twig\Extension\UploaderExtension;
use Vich\UploaderBundle\Twig\Extension\UploaderExtensionRuntime;
Expand All @@ -23,7 +23,7 @@ public function testAssetIsRegistered(): void

public function testAssetForwardsCallsToTheHelper(): void
{
$helper = $this->createMock(UploaderHelper::class);
$helper = $this->createMock(UploaderHelperInterface::class);
$extension = new UploaderExtensionRuntime($helper);
$object = new \stdClass();

Expand Down

0 comments on commit 6674793

Please sign in to comment.