cocur / slugify

Converts a string to a slug. Includes integrations for Symfony, Silex, Laravel, Zend Framework 2, Twig, Nette and Latte.
MIT License
2.87k stars 252 forks source link

TypeError with Nette 3 #255

Open NoNoNo opened 4 years ago

NoNoNo commented 4 years ago

I get a TypeError with Nette 3:

Argument 1 passed to Cocur\Slugify\Bridge\Nette\SlugifyExtension::Cocur\Slugify\Bridge\Nette\{closure}() must be an instance of Nette\DI\Definitions\ServiceDefinition, instance of Nette\DI\Definitions\FactoryDefinition given, called in vendor/cocur/slugify/src/Bridge/Nette/SlugifyExtension.php on line 42

I managed to fix this with:

diff --git a/src/Bridge/Nette/SlugifyExtension.php b/src/Bridge/Nette/SlugifyExtension.php
index 12fdbe5..34e4d35 100644
--- a/src/Bridge/Nette/SlugifyExtension.php
+++ b/src/Bridge/Nette/SlugifyExtension.php
@@ -3,7 +3,7 @@
 namespace Cocur\Slugify\Bridge\Nette;

 use Nette\DI\CompilerExtension;
-use Nette\DI\ServiceDefinition;
+use Nette\DI\Definitions\FactoryDefinition;

 /**
  * SlugifyExtension
@@ -33,8 +33,8 @@ class SlugifyExtension extends CompilerExtension
         $builder = $this->getContainerBuilder();

         $self = $this;
-        $registerToLatte = function (ServiceDefinition $def) use ($self) {
-            $def->addSetup('addFilter', ['slugify', [$self->prefix('@helper'), 'slugify']]);
+        $registerToLatte = function (FactoryDefinition $def) use ($self) {
+            $def->getResultDefinition()->addSetup('addFilter', ['slugify', [$self->prefix('@helper'), 'slugify']]);
         };

         $latteFactory = $builder->getByType('Nette\Bridges\ApplicationLatte\ILatteFactory') ?: 'nette.latteFactory';
florianeckerstorfer commented 4 years ago

@NoNoNo Could you create a PR for this? If you need any help please let me know.

NoNoNo commented 4 years ago

@florianeckerstorfer Sorry, I don’t know how to fix the unit tests:

There were 2 errors:

1) Cocur\Slugify\Tests\Bridge\Nette\SlugifyExtensionTest::testLoadConfiguration

`Mockery\Exception`: The class `\Nette\DI\ServiceDefinition` is marked final and its methods cannot be replaced. Classes marked final can be passed in to `\Mockery::mock()` as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.

[`slugify/tests/Bridge/Nette/SlugifyExtensionTest.php:31`](https://github.com/cocur/slugify/blob/master/tests/Bridge/Nette/SlugifyExtensionTest.php#L31)

2) Cocur\Slugify\Tests\Bridge\Nette\SlugifyExtensionTest::testBeforeCompile

`Mockery\Exception`: The class `\Nette\DI\ServiceDefinition` is marked final and its methods cannot be replaced. Classes marked final can be passed in to `\Mockery::mock()` as instantiated objects to create a partial mock, but only if the mock is not subject to type hinting checks.

[`slugify/tests/Bridge/Nette/SlugifyExtensionTest.php:82`](https://github.com/cocur/slugify/blob/master/tests/Bridge/Nette/SlugifyExtensionTest.php#L82)

(same problem as PR #231)

VottusCode commented 4 years ago

Yeah, I got the same errors as @NoNoNo when I was attempting to fix the unit test in my PR. I thought that there's only something wrong with me, as I write tests in Nette Tester and I don't use Mockery.