Skip to content

Commit

Permalink
Fix & improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
seferov committed Jun 5, 2024
1 parent af23a49 commit 57d654f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 114 deletions.
47 changes: 20 additions & 27 deletions tests/acceptance/acceptance/ContainerService.feature
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@ Feature: Container service

Background:
Given I have Symfony plugin enabled

Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'
Given I have the following code
And I have the following code preamble
"""
<?php
use \Symfony\Component\DependencyInjection\ContainerInterface;
class SomeService
{
public function do(): void {}
}
"""

class SomeController
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'
Given I have the following code
"""
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get(SomeService::class)->do();
$container->get(SomeService::class)->do();
}
}
"""
Expand All @@ -30,19 +33,11 @@ Feature: Container service
Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get()'.
Given I have the following code
"""
<?php
class SomeService
{
public function do(): void {}
}
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get(SomeService::class)->nosuchmethod();
$container->get(SomeService::class)->nosuchmethod();
}
}
"""
Expand All @@ -55,18 +50,16 @@ Feature: Container service
Scenario: Container get(self::class) should not crash
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index()
public function index(ContainerInterface $container): void
{
$this->container->get(self::class)->index();
$container->get(self::class)->index();
}
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| MissingReturnType | Method SomeController::index does not have a return type, expecting void |
| Type | Message |
| MixedMethodCall | Cannot determine the type of the object on the left hand side of this expression |
And I see no other errors
50 changes: 19 additions & 31 deletions tests/acceptance/acceptance/ContainerXml.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ Feature: Container XML config
"""
<containerXml>../../tests/acceptance/container.xml</containerXml>
"""
And I have the following code preamble
"""
<?php
use \Symfony\Component\DependencyInjection\ContainerInterface;
"""

Scenario: Asserting psalm recognizes return type of service got via 'ContainerInterface::get() using service ID'
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): bool
public function __invoke(ContainerInterface $container): bool
{
return $this->container->get('service_container')->has('lorem');
return $container->get('service_container')->has('lorem');
}
}
"""
Expand All @@ -29,15 +31,11 @@ Feature: Container XML config
Scenario: Psalm emits when service ID not found in container'
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get('not_a_service')->has('lorem');
$container->get('not_a_service')->has('lorem');
}
}
"""
Expand All @@ -49,18 +47,12 @@ Feature: Container XML config
Scenario: Using service both via alias and class const
Given I have the following code
"""
<?php
use Symfony\Component\HttpKernel\HttpKernelInterface;
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get('http_kernel');
$this->container->get(HttpKernelInterface::class);
$container->get('http_kernel');
$container->get(HttpKernelInterface::class);
}
}
"""
Expand All @@ -70,15 +62,11 @@ Feature: Container XML config
Scenario: Using private service
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get('private_service');
$container->get('private_service');
}
}
"""
Expand Down
25 changes: 13 additions & 12 deletions tests/acceptance/acceptance/HeaderBag.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Feature: Header get

Background:
Given I have issue handler "UnusedFunctionCall" suppressed
Given I have issue handler "UnusedVariable" suppressed
And I have Symfony plugin enabled
And I have the following code preamble
"""
Expand All @@ -11,24 +11,23 @@ Feature: Header get
use Symfony\Component\HttpFoundation\Request;
"""

Scenario: HeaderBag get method return type should return `?string` (unless third argument is provided for < Sf4.4)
Scenario: HeaderBag get method return type should return `?string`
Given I have the following code
"""
class App
{
public function index(Request $request): void
{
$string = $request->headers->get('nullable_string');
if (!$string) {
return;
}
trim($string);
/** @psalm-trace $nullableString */
$nullableString = $request->headers->get('nullable_string');
}
}
"""
When I run Psalm
Then I see no errors
Then I see these errors
| Type | Message |
| Trace | $nullableString: null\|string |
And I see no other errors

Scenario: HeaderBag get method return type should return `string` if default value is provided with string
Given I have the following code
Expand All @@ -37,11 +36,13 @@ Feature: Header get
{
public function index(Request $request): void
{
/** @psalm-trace $string */
$string = $request->headers->get('string', 'string');
trim($string);
}
}
"""
When I run Psalm
Then I see no errors
Then I see these errors
| Type | Message |
| Trace | $string: string |
And I see no other errors
69 changes: 25 additions & 44 deletions tests/acceptance/acceptance/NamingConventions.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ Feature: Naming conventions
"""
<containerXml>../../tests/acceptance/container.xml</containerXml>
"""
And I have the following code preamble
"""
<?php
use \Symfony\Component\DependencyInjection\ContainerInterface;
use \Symfony\Component\HttpKernel\HttpKernelInterface;
"""

Scenario: There is no service naming convention violation, so no complaint.
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): bool
public function __invoke(ContainerInterface $container): bool
{
return $this->container->get('service_container')->has('lorem');
return $container->get('service_container')->has('lorem');
}
}
"""
Expand All @@ -29,15 +32,11 @@ Feature: Naming conventions
Scenario: Detects service naming convention violation
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get('wronglyNamedService');
$container->get('wronglyNamedService');
}
}
"""
Expand All @@ -50,17 +49,11 @@ Feature: Naming conventions
Scenario: No service naming convention violation when using FQCNs
Given I have the following code
"""
<?php
use Symfony\Component\HttpKernel\HttpKernelInterface;
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function __invoke(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->get(HttpKernelInterface::class);
$container->get(HttpKernelInterface::class);
}
}
"""
Expand All @@ -70,15 +63,11 @@ Feature: Naming conventions
Scenario: No naming convention violation for parameter
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->getParameter('kernel.cache_dir');
$container->getParameter('kernel.cache_dir');
}
}
"""
Expand All @@ -88,15 +77,11 @@ Feature: Naming conventions
Scenario: Detects parameter naming convention violation
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function index(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->getParameter('wronglyNamedParameter');
$container->getParameter('wronglyNamedParameter');
}
}
"""
Expand All @@ -109,15 +94,11 @@ Feature: Naming conventions
Scenario: No parameter naming convention violation when using environment variables
Given I have the following code
"""
<?php
class SomeController
class App
{
use \Symfony\Component\DependencyInjection\ContainerAwareTrait;
public function __invoke(): void
public function __invoke(ContainerInterface $container): void
{
$this->container->getParameter('env(SOME_ENV_VAR)');
$container->getParameter('env(SOME_ENV_VAR)');
}
}
"""
Expand Down

0 comments on commit 57d654f

Please sign in to comment.