Completed
Push — master ( 446f2e...32b2c3 )
by Nelson
11:26
created

StringTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testPerformsFormatForStringWithIntegerPlaceholders() 0 22 1
A testPerformsFormatForStringsWithStringPlaceholders() 0 10 1
A testPerformsFormatWithManyData() 0 23 2
A testPerformsFormatIgnoringNotMatchingData() 0 20 1
B testPerformsFormatWithArgumentsOfDifferentTypesConvertiblesToString() 0 24 1
A testDoNotPerformsFormatWithPlaceholdersValuesNotConvertiblesToString() 0 6 1
A nonStringObjectsProvider() 0 8 1
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Content:
6
 * - Test case for: [NelsonMartell\Extensions] String
7
 *
8
 * Copyright © 2016 Nelson Martell (http://nelson6e65.github.io)
9
 *
10
 * Licensed under The MIT License (MIT)
11
 * For full copyright and license information, please see the LICENSE
12
 * Redistributions of files must retain the above copyright notice.
13
 *
14
 * @copyright 2016 Nelson Martell
15
 * @link      http://nelson6e65.github.io/php_nml/
16
 * @since     v0.6.0
17
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
18
 * */
19
namespace NelsonMartell\Test\TestCase\Extensions;
20
21
use NelsonMartell as NML;
22
use NelsonMartell\Extensions\String;
23
use NelsonMartell\Type;
24
use \PHPUnit_Framework_TestCase as TestCase;
25
use \InvalidArgumentException;
26
27
/**
28
 * Test case for `NelsonMartell\Extensions\String` class.
29
 *
30
 * @see    String
31
 * @author Nelson Martell <[email protected]>
32
 *
33
 * @internal
34
 * */
35
class StringTest extends TestCase
36
{
37
    public function testPerformsFormatForStringWithIntegerPlaceholders()
38
    {
39
        $expected = 'Mi nombre es Juan';
40
        $actual = String::format('Mi nombre es {0}', ['Juan']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
41
        $this->assertEquals($expected, $actual);
42
43
        $actual = String::format("Mi nombre es {0}", 'Juan');
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Mi nombre es {0} does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
44
        $this->assertEquals($expected, $actual);
45
46
        $name = 'Juan';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
47
        $actual = String::format("Mi nombre es {0}", $name);
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal Mi nombre es {0} does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
48
        $this->assertEquals($expected, $actual);
49
50
        $expected = 'Me llamo Nelson y tengo 29 años de edad.';
51
        $name = 'Nelson';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
52
        $age = 29;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
53
        $actual = String::format('Me llamo {0} y tengo {1} años de edad.', $name, $age);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
54
        $this->assertEquals($expected, $actual);
55
56
        $actual = String::format('Me llamo {0} y tengo {1} años de edad.', [$name, $age]);
57
        $this->assertEquals($expected, $actual);
58
    }
59
60
    public function testPerformsFormatForStringsWithStringPlaceholders()
61
    {
62
        $expected = 'Mi nombre es Juan';
63
        $actual = String::format('Mi nombre es {name}', ["name" => "Juan"]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal name does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal Juan does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
64
        $this->assertEquals($expected, $actual);
65
66
        $expected = 'Tengo 20 años de edad.';
67
        $actual = String::format('Tengo {age} años de edad.', ["name" => "Juan", 'age' => 20]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
Coding Style Comprehensibility introduced by
The string literal name does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal Juan does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
68
        $this->assertEquals($expected, $actual);
69
    }
70
71
    public function testPerformsFormatWithManyData()
72
    {
73
        $expected = 'Mi nombre es Juan y tengo 61 años de edad.';
74
        $format   = 'Mi nombre es {name} y tengo {age} años de edad.';
75
        $actual   = String::format($format, ['age' => 61, 'name' => 'Juan']);
76
        $this->assertEquals($expected, $actual);
77
78
        $format = 'Mi nombre es {1} y tengo {0} años de edad.';
79
        $actual = String::format($format, [61, 'Juan']);
80
        $this->assertEquals($expected, $actual);
81
82
        $actual = String::format($format, 61, 'Juan');
83
        $this->assertEquals($expected, $actual);
84
85
        $expected = 'Números: 1, 2, 3, 4, 5, 6, 7, 8, 9 y 10.';
86
        $format   = 'Números: ';
87
        for ($i = 0; $i < 8; $i++) {
88
            $format .= "{{$i}}, ";
0 ignored issues
show
Coding Style Best Practice introduced by
As per coding-style, please use concatenation or sprintf for the variable $i instead of interpolation.

It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings.

// Instead of
$x = "foo $bar $baz";

// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
89
        }
90
        $format .= '{8} y {9}.';
91
        $actual   = String::format($format, range(1, 10));
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 3 spaces

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
92
        $this->assertEquals($expected, $actual);
93
    }
94
95
    /**
96
     * @depends testPerformsFormatWithManyData
97
     */
98
    public function testPerformsFormatIgnoringNotMatchingData()
99
    {
100
        $expected = 'Mi nombre es {name} y tengo {age} años de edad.';
101
        $actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['fake_name' => 'Juan']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
        $this->assertEquals($expected, $actual);
103
104
        $actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', null);
105
        $this->assertEquals($expected, $actual);
106
107
        $actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', null, null, null);
108
        $this->assertEquals($expected, $actual);
109
110
        $expected = 'Mi nombre es Juan y tengo {age} años de edad.';
111
        $actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['name' => 'Juan']);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
112
        $this->assertEquals($expected, $actual);
113
114
        $expected = 'Mi nombre es {name} y tengo 54 años de edad.';
115
        $actual = String::format('Mi nombre es {name} y tengo {age} años de edad.', ['age' => 54]);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
116
        $this->assertEquals($expected, $actual);
117
    }
118
119
    public function testPerformsFormatWithArgumentsOfDifferentTypesConvertiblesToString()
120
    {
121
        $expected = 'Invalid argument type. "major" (position 0) must to be an instance of "integer"; "string" given.';
122
123
        $major = 'Non Number';
124
        $args = [
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
125
            'name'     => 'major',
126
            'expected' => NML\typeof(0),
0 ignored issues
show
Deprecated Code introduced by
The function typeof() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\typeof()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
127
            'pos'      => 0,
128
            'actual' => NML\typeof($major),
0 ignored issues
show
Deprecated Code introduced by
The function typeof() has been deprecated with message: since v0.6.0, will be removed in v0.7.0. Use `\NelsonMartell\typeof()` instead.

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
129
        ];
130
131
        // Esto produce un error fatal debido a una debilidad en el uso de la función `asort` en el método
132
        //  ``Cake\Utility\Text::.insert()``, ya que debería comparar como cadena usando el flag `SORT_STRING`
133
        //  pero que se evita convirtiendo los valores a string en el método String::format().
134
135
        $actual = String::format('Invalid argument type.', null);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
136
        $actual .= String::format(
137
            ' "{name}" (position {pos}) must to be an instance of "{expected}"; "{actual}" given.',
138
            $args
139
        );
140
141
        $this->assertEquals($expected, $actual);
142
    }
143
144
    /**
145
     * @expectedException InvalidArgumentException
146
     * @dataProvider nonStringObjectsProvider
147
     */
148
    public function testDoNotPerformsFormatWithPlaceholdersValuesNotConvertiblesToString($obj)
149
    {
150
        $format = PHP_EOL.'This test should throws an "{0}" with data: {testErrorData}.'.PHP_EOL;
151
152
        $str = String::format($format, [InvalidArgumentException::class, 'testErrorData' => $obj]);
0 ignored issues
show
Unused Code introduced by
$str is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
153
    }
154
155
    public function nonStringObjectsProvider()
156
    {
157
        return [
158
            'stdClass' => [new \stdClass],
159
            'int[]'    => [[10, 20, 30, 40]],
160
            'string[]' => [['ten', '20', '30', '40']],
161
        ];
162
    }
163
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
164