- 
                Notifications
    You must be signed in to change notification settings 
- Fork 48
Factory service test #85
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
          
     Open
      
      
            silvioq
  wants to merge
  17
  commits into
  SymfonyTest:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
silvioq:master
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            17 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      312de09
              
                Factory test
              
              
                silvioq 7aacdbb
              
                Fix typo
              
              
                silvioq e49fdd3
              
                BC for SYMFONY_DI_VERSION=2.7|2.8|3.2
              
              
                silvioq 384d88f
              
                Backward compatibility
              
              
                silvioq 4c7905e
              
                Optimize, don't create an instance
              
              
                silvioq 16b4ece
              
                Fix a lot of CS breaks
              
              
                silvioq 323330e
              
                Merge remote-tracking branch 'upstream/master'
              
              
                silvioq a84f713
              
                Factory test
              
              
                silvioq c138ec9
              
                Fix typo
              
              
                silvioq 268b680
              
                BC for SYMFONY_DI_VERSION=2.7|2.8|3.2
              
              
                silvioq 9802ce7
              
                Backward compatibility
              
              
                silvioq 6d228af
              
                Optimize, don't create an instance
              
              
                silvioq 569d9af
              
                Fix a lot of CS breaks
              
              
                silvioq eb5bfec
              
                Merge branch 'master' of github.com:silvioq/SymfonyDependencyInjectio…
              
              
                silvioq 1c71370
              
                Changes legacy factory service interface to avoid deprecation messages
              
              
                silvioq 3fb9b74
              
                Fix service creation
              
              
                silvioq 5e84b5b
              
                Merge branch 'master' into master
              
              
                silvioq File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| <?php | ||
|  | ||
| namespace Matthias\SymfonyDependencyInjectionTest\PhpUnit; | ||
|  | ||
| use PHPUnit\Framework\Constraint\Constraint; | ||
| use PHPUnit\Framework\Constraint\IsEqual; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
|  | ||
| class ContainerBuilderHasFactoryConstraint extends Constraint | ||
| { | ||
| private $serviceId; | ||
| private $expectedFactoryClass; | ||
| private $expectedFactoryMethod; | ||
|  | ||
| public function __construct($serviceId, $expectedFactoryClass = null, $expectedFactoryMethod = null) | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here | ||
| { | ||
| parent::__construct(); | ||
|  | ||
| if (!is_string($serviceId)) { | ||
| throw new \InvalidArgumentException('The $serviceId argument should be a string'); | ||
| } | ||
|  | ||
| if ($expectedFactoryClass !== null && !is_string($expectedFactoryClass)) { | ||
| throw new \InvalidArgumentException('The $expectedFactoryClass argument should be a string'); | ||
| } | ||
|  | ||
| if (null !== $expectedFactoryMethod && null === $expectedFactoryClass) { | ||
| throw new \InvalidArgumentException('When argument $expectedFactoryMethod is set, must inform $expectedFactoryClass'); | ||
| } | ||
|  | ||
| if (null !== $expectedFactoryMethod && !is_string($expectedFactoryMethod)) { | ||
| throw new \InvalidArgumentException('The $expectedFactoryMethod argument should be a string'); | ||
| } | ||
|  | ||
| $this->serviceId = $serviceId; | ||
| $this->expectedFactoryClass = $expectedFactoryClass; | ||
| $this->expectedFactoryMethod = $expectedFactoryMethod; | ||
| } | ||
|  | ||
| public function toString() | ||
| { | ||
| if (null === $this->expectedFactoryClass) { | ||
| return sprintf('"%s" has factory', $this->serviceId); | ||
| } | ||
|  | ||
| return sprintf('"%s" has factory "@%s:%s"', $this->serviceId, $this->expectedFactoryClass, $this->expectedFactoryMethod); | ||
| } | ||
|  | ||
| public function evaluate($other, $description = '', $returnResult = false) | ||
| { | ||
| if (!($other instanceof ContainerBuilder)) { | ||
| throw new \InvalidArgumentException( | ||
| 'Expected an instance of Symfony\Component\DependencyInjection\ContainerBuilder' | ||
| ); | ||
| } | ||
|  | ||
| if (!$this->evaluateServiceId($other, $returnResult)) { | ||
| return false; | ||
| } | ||
|  | ||
| if (!$this->evaluateFactory($other, $returnResult)) { | ||
| return false; | ||
| } | ||
|  | ||
| if ($this->expectedFactoryClass !== null && !$this->evaluateFactoryClass($other, $returnResult)) { | ||
| return false; | ||
| } | ||
|  | ||
| return true; | ||
| } | ||
|  | ||
| private function evaluateServiceId(ContainerBuilder $containerBuilder, $returnResult) | ||
| { | ||
| if (!$containerBuilder->hasDefinition($this->serviceId)) { | ||
| if ($returnResult) { | ||
| return false; | ||
| } | ||
|  | ||
| $this->fail( | ||
| $containerBuilder, | ||
| sprintf( | ||
| 'The container builder has no service "%s"', | ||
| $this->serviceId | ||
| ) | ||
| ); | ||
| } | ||
|  | ||
| return true; | ||
| } | ||
|  | ||
| private function evaluateFactory(ContainerBuilder $containerBuilder, $returnResult) | ||
| { | ||
| /** @var Definition */ | ||
| $definition = $containerBuilder->getDefinition($this->serviceId); | ||
|  | ||
| $factory = $this->getFactoryData($definition); | ||
|  | ||
| if (!is_array($factory)) { | ||
| if ($returnResult) { | ||
| return false; | ||
| } | ||
|  | ||
| $this->fail( | ||
| $containerBuilder, | ||
| sprintf( | ||
| 'The container builder has service "%s" with not "%s" factory', | ||
| $this->serviceId, | ||
| $this->expectedFactoryClass | ||
| ) | ||
| ); | ||
| } | ||
|  | ||
| return true; | ||
| } | ||
|  | ||
| private function evaluateFactoryClass(ContainerBuilder $containerBuilder, $returnResult) | ||
| { | ||
| /** @var Definition */ | ||
| $definition = $containerBuilder->getDefinition($this->serviceId); | ||
|  | ||
| $factory = $this->getFactoryData($definition); | ||
|  | ||
| list($factoryDefinition, $factoryMethod) = $factory; | ||
|  | ||
| if ($factoryDefinition instanceof Reference) { | ||
| $factoryClass = (string)$factoryDefinition; | ||
| } elseif (is_string($factoryDefinition)) { | ||
| $factoryClass = $factoryDefinition; | ||
| } else { | ||
| if ($returnResult) { | ||
| return false; | ||
| } | ||
|  | ||
| $this->fail( | ||
| $containerBuilder, | ||
| sprintf( | ||
| 'The container builder has service "%s" with not service "%s" factory', | ||
| $this->serviceId, | ||
| $this->expectedFactoryClass | ||
| ) | ||
| ); | ||
| } | ||
|  | ||
| $constraint = new IsEqual($this->expectedFactoryClass); | ||
| if (!$constraint->evaluate($factoryClass, '', true)) { | ||
| if ($returnResult) { | ||
| return false; | ||
| } | ||
|  | ||
| $this->fail( | ||
| $containerBuilder, | ||
| sprintf( | ||
| 'The container builder has service "%s" with not service class "%s" factory', | ||
| $this->serviceId, | ||
| $this->expectedFactoryClass | ||
| ) | ||
| ); | ||
| } | ||
|  | ||
| if ($this->expectedFactoryMethod) { | ||
| $constraint = new IsEqual($this->expectedFactoryMethod); | ||
| if (!$constraint->evaluate($factoryMethod, '', true)) { | ||
| if ($returnResult) { | ||
| return false; | ||
| } | ||
|  | ||
| $this->fail( | ||
| $containerBuilder, | ||
| sprintf( | ||
| 'The container builder has service "%s" with not service class method "%s::%s" factory', | ||
| $this->serviceId, | ||
| $this->expectedFactoryClass, | ||
| $this->expectedFactoryMethod | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|  | ||
| return true; | ||
| } | ||
|  | ||
| private function getFactoryData(Definition $definition) | ||
| { | ||
| if (self::isLegacySymfonyDI()) { | ||
| $factoryService = $definition->getFactoryService(); | ||
| $factoryMethod = $definition->getFactoryMethod(); | ||
| $factoryClass = $definition->getFactoryClass(); | ||
| if (!$factoryService && !$factoryClass) { | ||
| return null; | ||
| } | ||
|  | ||
| return array( $factoryClass ? $factoryClass : $factoryService, $factoryMethod ); | ||
| } else { | ||
| $factory = $definition->getFactory(); | ||
| if (is_array($factory)) { | ||
| return $factory; | ||
| } | ||
|  | ||
| if (is_string($factory) && false !== strpos($factory, ':')) { | ||
| return preg_split('/:/', $factory, 2); | ||
| } | ||
|  | ||
| return $factory; | ||
| } | ||
| } | ||
|  | ||
|  | ||
| public static function isLegacySymfonyDI() | ||
| { | ||
| return !method_exists(Definition::class, 'getFactory') && | ||
| method_exists(Definition::class, 'getFactoryService'); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| <?xml version="1.0" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <service id="factory_service"> | ||
| </service> | ||
|  | ||
| <service id="created_by_factory_service" factory-service="factory_service" factory-method="factoryMethod"> | ||
| </service> | ||
| </services> | ||
| </container> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| services: | ||
| created_with_factory_with_old_syntax: | ||
| factory: ['@factory_service', 'factoryMethod'] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <?xml version="1.0" ?> | ||
| <container xmlns="http://symfony.com/schema/dic/services" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
| <services> | ||
| <service id="factory_service"> | ||
| </service> | ||
|  | ||
| <service id="created_by_factory_service"> | ||
| <factory service="factory_service" method="factoryMethod" /> | ||
| </service> | ||
| </services> | ||
| </container> | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -26,5 +26,6 @@ | |
|  | ||
| <service id="synthetic_service" synthetic="true"> | ||
| </service> | ||
|  | ||
| </services> | ||
| </container> | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
now that the library requires PHP 7 we can use scalar type hints here