Overview

Namespaces

  • expect
    • config
    • configurator
    • context
    • factory
    • matcher
      • strategy
    • package
    • registry
    • reporter

Classes

  • expect\config\ConfigurationLoader
  • expect\config\DefaultConfiguration
  • expect\config\RuntimeConfiguration
  • expect\configurator\DefaultConfigurator
  • expect\configurator\FileConfigurator
  • expect\context\DefaultContextFactory
  • expect\context\EvaluateContext
  • expect\Dictionary
  • expect\Expect
  • expect\factory\DefaultMatcherFactory
  • expect\FailedMessage
  • expect\matcher\PatternMatcher
  • expect\matcher\strategy\ArrayInclusionStrategy
  • expect\matcher\strategy\InclusionResult
  • expect\matcher\strategy\StringInclusionStrategy
  • expect\matcher\ToBe
  • expect\matcher\ToBeA
  • expect\matcher\ToBeAbove
  • expect\matcher\ToBeAn
  • expect\matcher\ToBeAnInstanceOf
  • expect\matcher\ToBeArray
  • expect\matcher\ToBeBelow
  • expect\matcher\ToBeBoolean
  • expect\matcher\ToBeEmpty
  • expect\matcher\ToBeFalse
  • expect\matcher\ToBeFalsey
  • expect\matcher\ToBeFloat
  • expect\matcher\ToBeGreaterThan
  • expect\matcher\ToBeInteger
  • expect\matcher\ToBeLessThan
  • expect\matcher\ToBeNull
  • expect\matcher\ToBeString
  • expect\matcher\ToBeTrue
  • expect\matcher\ToBeTruthy
  • expect\matcher\ToBeWithin
  • expect\matcher\ToContain
  • expect\matcher\ToEndWith
  • expect\matcher\ToEqual
  • expect\matcher\ToHaveKey
  • expect\matcher\ToHaveLength
  • expect\matcher\ToMatch
  • expect\matcher\ToPrint
  • expect\matcher\ToStartWith
  • expect\matcher\ToThrow
  • expect\matcher\TruthyMatcher
  • expect\MatcherDictionary
  • expect\MatcherEvaluator
  • expect\MatcherPackage
  • expect\package\DefaultMatcherPackage
  • expect\package\DefaultPackageRegistrar
  • expect\package\MatcherClass
  • expect\package\ReflectionIterator
  • expect\registry\DefaultMatcherRegistry
  • expect\reporter\ExceptionReporter
  • expect\reporter\TextMessageReporter
  • expect\Result

Interfaces

  • expect\Configurable
  • expect\Configuration
  • expect\Configurator
  • expect\Context
  • expect\ContextFactory
  • expect\Evaluator
  • expect\Matcher
  • expect\matcher\ReportableMatcher
  • expect\matcher\strategy\InclusionStrategy
  • expect\MatcherContainer
  • expect\MatcherFactory
  • expect\MatcherRegistry
  • expect\Message
  • expect\PackageRegistrar
  • expect\RegisterablePackage
  • expect\ResultReporter

Traits

  • expect\config\ConfigurableConfiguration
  • expect\matcher\EqualMatcherDelegatable
  • expect\matcher\GreaterThanMatcherDelegatable
  • expect\matcher\LengthMatcherDelegatable
  • expect\matcher\LessThanMatcherDelegatable
  • expect\matcher\TypeMatcherDelegatable
  • expect\MatcherLookupTable

Exceptions

  • expect\config\ConfigurationFileNotFoundException
  • expect\config\NotAvailableException
  • expect\package\ComposerJsonNotFoundException
  • expect\registry\MatcherAlreadyRegisteredException
  • expect\registry\MatcherNotRegisteredException
  • expect\reporter\FailedException

Functions

  • expect\expect
  • Overview
  • Namespace
  • Class
  1: <?php
  2: 
  3: /**
  4:  * This file is part of expect package.
  5:  *
  6:  * (c) Noritaka Horio <holy.shared.design@gmail.com>
  7:  *
  8:  * This source file is subject to the MIT license that is bundled
  9:  * with this source code in the file LICENSE.
 10:  */
 11: namespace expect;
 12: 
 13: use ArrayIterator;
 14: use expect\package\ComposerJsonNotFoundException;
 15: use expect\package\MatcherClass;
 16: use expect\package\ReflectionIterator;
 17: use expect\matcher\ReportableMatcher;
 18: use Noodlehaus\Config;
 19: 
 20: /**
 21:  * Matcher package
 22:  *
 23:  * @author Noritaka Horio <holy.shared.design@gmail.com>
 24:  * @copyright Noritaka Horio <holy.shared.design@gmail.com>
 25:  */
 26: final class MatcherPackage implements RegisterablePackage
 27: {
 28: 
 29:     /**
 30:      * @var string
 31:      */
 32:     private $namespace;
 33: 
 34:     /**
 35:      * @var string
 36:      */
 37:     private $namespaceDirectory;
 38: 
 39:     /**
 40:      * Create a new macther package
 41:      *
 42:      * @param string $namespace          namespace of package
 43:      * @param string $namespaceDirectory directory of package
 44:      */
 45:     public function __construct($namespace, $namespaceDirectory)
 46:     {
 47:         $this->namespace = $this->normalizeNamespace($namespace);
 48:         $this->namespaceDirectory = $namespaceDirectory;
 49:     }
 50: 
 51:     /**
 52:      * {@inheritdoc}
 53:      */
 54:     public function registerTo(MatcherRegistry $registry)
 55:     {
 56:         $provideMatchers = $this->getProvideMatchers();
 57: 
 58:         foreach ($provideMatchers as $provideMatcher) {
 59:             $registry->register($provideMatcher);
 60:         }
 61:     }
 62: 
 63:     /**
 64:      * @return ArrayIterator
 65:      */
 66:     private function getProvideMatchers()
 67:     {
 68:         $matchers = [];
 69:         $reflectionIterator = new ReflectionIterator(
 70:             $this->namespace,
 71:             $this->namespaceDirectory
 72:         );
 73: 
 74:         foreach ($reflectionIterator as $reflection) {
 75:             if ($reflection->implementsInterface(ReportableMatcher::class) === false) {
 76:                 continue;
 77:             }
 78: 
 79:             $matchers[] = new MatcherClass(
 80:                 $reflection->getNamespaceName(),
 81:                 $reflection->getShortName()
 82:             );
 83:         }
 84: 
 85:         return new ArrayIterator($matchers);
 86:     }
 87: 
 88:     private function normalizeNamespace($namespace)
 89:     {
 90:         $normalizeNamespace = $namespace;
 91:         $lastCharAt = strlen($namespace) - 1;
 92: 
 93:         if (substr($namespace, $lastCharAt) === '\\') {
 94:             $normalizeNamespace = substr($namespace, 0, $lastCharAt);
 95:         }
 96: 
 97:         return $normalizeNamespace;
 98:     }
 99: 
100:     /**
101:      * Create a new matcher package from composer.json
102:      *
103:      * @param string $composerJson composer.json path
104:      *
105:      * @throws \expect\package\ComposerJsonNotFoundException
106:      */
107:     public static function fromPackageFile($composerJson)
108:     {
109:         if (file_exists($composerJson) === false) {
110:             throw new ComposerJsonNotFoundException("File {$composerJson} not found.");
111:         }
112: 
113:         $config = Config::load($composerJson);
114:         $autoload = $config->get('autoload.psr-4');
115: 
116:         $composerJsonDirectory = dirname($composerJson);
117: 
118:         $keys = array_keys($autoload);
119:         $namespace = array_shift($keys);
120: 
121:         $values = array_values($autoload);
122:         $namespaceDirectory = array_shift($values);
123:         $namespaceDirectory = realpath($composerJsonDirectory . '/' . $namespaceDirectory);
124: 
125:         return new self($namespace, $namespaceDirectory);
126:     }
127: }
128: 
Expect API documentation generated by ApiGen