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\matcher;
 12: 
 13: use Countable;
 14: use expect\FailedMessage;
 15: 
 16: /**
 17:  * Verify whether the value has a length that was expected.
 18:  *
 19:  * <code>
 20:  * $matcher = new ToHaveLength(3);
 21:  * $matcher->match("foo"); //return true
 22:  *
 23:  * $matcher->match("fo"); //return false
 24:  * $matcher->match([ 1, 2 ]); //return false
 25:  * $matcher->match(new ArrayIterator([ 1, 2 ])); //return false
 26:  * <code>
 27:  *
 28:  * @author Noritaka Horio <holy.shared.design@gmail.com>
 29:  * @copyright Noritaka Horio <holy.shared.design@gmail.com>
 30:  */
 31: final class ToHaveLength implements ReportableMatcher
 32: {
 33:     /**
 34:      * @var string|array|Countable
 35:      */
 36:     private $actual;
 37: 
 38:     /**
 39:      * @var int
 40:      */
 41:     private $expected;
 42: 
 43:     /**
 44:      * @var string
 45:      */
 46:     private $type;
 47: 
 48:     /**
 49:      * @var int
 50:      */
 51:     private $length;
 52: 
 53:     /**
 54:      * Create a new matcher.
 55:      *
 56:      * @param int $expected
 57:      */
 58:     public function __construct($expected)
 59:     {
 60:         $this->expected = $expected;
 61:     }
 62: 
 63:     /**
 64:      * {@inheritdoc}
 65:      */
 66:     public function match($actual)
 67:     {
 68:         $this->actual = $actual;
 69: 
 70:         if (is_string($this->actual)) {
 71:             $this->type = 'string';
 72:             $this->length = mb_strlen($this->actual);
 73:         } elseif (is_array($this->actual)) {
 74:             $this->type = 'array';
 75:             $this->length = count($this->actual);
 76:         } elseif ($this->actual instanceof Countable) {
 77:             $this->type = get_class($this->actual);
 78:             $this->length = count($this->actual);
 79:         }
 80: 
 81:         return ($this->length === $this->expected);
 82:     }
 83: 
 84:     /**
 85:      * {@inheritdoc}
 86:      */
 87:     public function reportFailed(FailedMessage $message)
 88:     {
 89:         $message->appendText('Expected ')
 90:             ->appendText($this->type)
 91:             ->appendText(' to have a length of ')
 92:             ->appendText($this->expected)
 93:             ->appendText("\n\n")
 94:             ->appendText('    expected: ')
 95:             ->appendValue($this->expected)
 96:             ->appendText("\n")
 97:             ->appendText('      length: ')
 98:             ->appendValue($this->length);
 99:     }
100: 
101:     /**
102:      * {@inheritdoc}
103:      */
104:     public function reportNegativeFailed(FailedMessage $message)
105:     {
106:         $message->appendText('Expected ')
107:             ->appendText($this->type)
108:             ->appendText(' not to have a length of ')
109:             ->appendValue($this->expected)
110:             ->appendText("\n\n")
111:             ->appendText('    expected not: ')
112:             ->appendValue($this->expected)
113:             ->appendText("\n")
114:             ->appendText('          length: ')
115:             ->appendValue($this->length);
116:     }
117: }
118: 
Expect API documentation generated by ApiGen