~tcuthbert/wordpress/openstack-objectstorage

« back to all changes in this revision

Viewing changes to vendor/phpunit/phpunit/src/Framework/Constraint/StringContains.php

  • Committer: Jacek Nykis
  • Date: 2015-02-11 15:35:31 UTC
  • Revision ID: jacek.nykis@canonical.com-20150211153531-hmy6zi0ov2qfkl0b
Initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 * This file is part of PHPUnit.
 
4
 *
 
5
 * (c) Sebastian Bergmann <sebastian@phpunit.de>
 
6
 *
 
7
 * For the full copyright and license information, please view the LICENSE
 
8
 * file that was distributed with this source code.
 
9
 */
 
10
 
 
11
/**
 
12
 * Constraint that asserts that the string it is evaluated for contains
 
13
 * a given string.
 
14
 *
 
15
 * Uses strpos() to find the position of the string in the input, if not found
 
16
 * the evaluation fails.
 
17
 *
 
18
 * The sub-string is passed in the constructor.
 
19
 *
 
20
 * @package    PHPUnit
 
21
 * @subpackage Framework_Constraint
 
22
 * @author     Sebastian Bergmann <sebastian@phpunit.de>
 
23
 * @author     Bernhard Schussek <bschussek@2bepublished.at>
 
24
 * @copyright  Sebastian Bergmann <sebastian@phpunit.de>
 
25
 * @license    http://www.opensource.org/licenses/BSD-3-Clause  The BSD 3-Clause License
 
26
 * @link       http://www.phpunit.de/
 
27
 * @since      Class available since Release 3.0.0
 
28
 */
 
29
class PHPUnit_Framework_Constraint_StringContains extends PHPUnit_Framework_Constraint
 
30
{
 
31
    /**
 
32
     * @var string
 
33
     */
 
34
    protected $string;
 
35
 
 
36
    /**
 
37
     * @var boolean
 
38
     */
 
39
    protected $ignoreCase;
 
40
 
 
41
    /**
 
42
     * @param string  $string
 
43
     * @param boolean $ignoreCase
 
44
     */
 
45
    public function __construct($string, $ignoreCase = false)
 
46
    {
 
47
        parent::__construct();
 
48
 
 
49
        $this->string     = $string;
 
50
        $this->ignoreCase = $ignoreCase;
 
51
    }
 
52
 
 
53
    /**
 
54
     * Evaluates the constraint for parameter $other. Returns true if the
 
55
     * constraint is met, false otherwise.
 
56
     *
 
57
     * @param  mixed $other Value or object to evaluate.
 
58
     * @return bool
 
59
     */
 
60
    protected function matches($other)
 
61
    {
 
62
        if ($this->ignoreCase) {
 
63
            return stripos($other, $this->string) !== false;
 
64
        } else {
 
65
            return strpos($other, $this->string) !== false;
 
66
        }
 
67
    }
 
68
 
 
69
    /**
 
70
     * Returns a string representation of the constraint.
 
71
     *
 
72
     * @return string
 
73
     */
 
74
    public function toString()
 
75
    {
 
76
        if ($this->ignoreCase) {
 
77
            $string = strtolower($this->string);
 
78
        } else {
 
79
            $string = $this->string;
 
80
        }
 
81
 
 
82
        return sprintf(
 
83
            'contains "%s"',
 
84
            $string
 
85
        );
 
86
    }
 
87
}