Completed
Push — master ( e6d5b5...0573bb )
by Martijn
22s
created
SwaggerGen/Swagger/Type/StringType.php 2 patches
Indentation   +202 added lines, -202 removed lines patch added patch discarded remove patch
@@ -15,207 +15,207 @@
 block discarded – undo
15 15
 class StringType extends AbstractType
16 16
 {
17 17
 
18
-    private static $formats = array(
19
-        'string' => '',
20
-        'byte' => 'byte',
21
-        'binary' => 'binary',
22
-        'password' => 'password',
23
-        'enum' => '',
24
-    );
25
-
26
-    /**
27
-     * Name of the type
28
-     * @var string
29
-     */
30
-    protected $format = '';
31
-    protected $pattern;
32
-    protected $default;
33
-    protected $maxLength;
34
-    protected $minLength;
35
-    protected $enum = [];
36
-
37
-    /**
38
-     * @param string $command The comment command
39
-     * @param string $data Any data added after the command
40
-     * @return AbstractType|boolean
41
-     * @throws Exception
42
-     * @throws Exception
43
-     * @throws Exception
44
-     */
45
-    public function handleCommand($command, $data = null)
46
-    {
47
-        switch (strtolower($command)) {
48
-            case 'default':
49
-                $this->default = $this->validateDefault($data);
50
-                return $this;
51
-
52
-            case 'pattern':
53
-                $this->pattern = $data;
54
-                return $this;
55
-
56
-            case 'enum':
57
-                if ($this->minLength !== null || $this->maxLength !== null) {
58
-                    throw new Exception("Enumeration not allowed in ranged string: '{$data}'");
59
-                }
60
-                $words = self::wordSplit($data);
61
-                $this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words;
62
-                return $this;
63
-        }
64
-
65
-        return parent::handleCommand($command, $data);
66
-    }
67
-
68
-    /**
69
-     * Validate a default string value, depending on subtype
70
-     *
71
-     * @param string $value the value to validate
72
-     * @return string the value after validation (might become trimmed)
73
-     * @throws Exception
74
-     */
75
-    protected function validateDefault($value): string
76
-    {
77
-        if (empty($value)) {
78
-            if ($this->format) {
79
-                $type = $this->format;
80
-            } else {
81
-                $type = $this->enum ? 'enum' : 'string';
82
-            }
83
-            throw new Exception("Empty {$type} default");
84
-        }
85
-
86
-        // trim and unescape quotes if needed
87
-        if (mb_strlen($value) >= 2
88
-            && $value[0] === '"'
89
-            && $value[mb_strlen($value) - 1] === '"'
90
-        ) {
91
-            $value = stripcslashes(mb_substr($value, 1, -1));
92
-        }
93
-
94
-        if (!empty($this->enum) && !in_array($value, $this->enum, true)) {
95
-            throw new Exception("Invalid enum default: '{$value}'");
96
-        }
97
-
98
-        if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) {
99
-            if ($this->format) {
100
-                $type = $this->format;
101
-            } else {
102
-                $type = $this->enum ? 'enum' : 'string';
103
-            }
104
-            throw new Exception("Default {$type} length beyond maximum: '{$value}'");
105
-        }
106
-
107
-        if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
108
-            if ($this->format) {
109
-                $type = $this->format;
110
-            } else {
111
-                $type = $this->enum ? 'enum' : 'string';
112
-            }
113
-            throw new Exception("Default {$type} length beyond minimum: '{$value}'");
114
-        }
115
-
116
-        return $value;
117
-    }
118
-
119
-    public function toArray(): array
120
-    {
121
-        return self::arrayFilterNull(array_merge(array(
122
-            'type' => 'string',
123
-            'format' => empty($this->format) ? null : $this->format,
124
-            'pattern' => $this->pattern,
125
-            'default' => $this->default,
126
-            'minLength' => $this->minLength ? (int)$this->minLength : null,
127
-            'maxLength' => $this->maxLength ? (int)$this->maxLength : null,
128
-            'enum' => $this->enum,
129
-        ), parent::toArray()));
130
-    }
131
-
132
-    public function __toString()
133
-    {
134
-        return __CLASS__;
135
-    }
136
-
137
-    /**
138
-     * @throws Exception
139
-     */
140
-    protected function parseDefinition($definition): void
141
-    {
142
-        $definition = self::trim($definition);
143
-
144
-        $match = [];
145
-        if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
146
-            throw new Exception("Unparseable string definition: '{$definition}'");
147
-        }
148
-
149
-        $this->parseFormat($definition, $match);
150
-        $this->parseContent($definition, $match);
151
-        $this->parseRange($definition, $match);
152
-        $this->parseDefault($definition, $match);
153
-    }
154
-
155
-    /**
156
-     * @param string $definition
157
-     * @param string[] $match
158
-     * @throws Exception
159
-     */
160
-    private function parseFormat($definition, $match): void
161
-    {
162
-        $type = strtolower($match[1]);
163
-        if (!isset(self::$formats[$type])) {
164
-            throw new Exception("Not a string: '{$definition}'");
165
-        }
166
-        $this->format = self::$formats[$type];
167
-    }
168
-
169
-    /**
170
-     * @param string $definition
171
-     * @param string[] $match
172
-     */
173
-    private function parseContent($definition, $match): void
174
-    {
175
-        if (strtolower($match[1]) === 'enum') {
176
-            $this->enum = explode(',', $match[2]);
177
-        } else {
178
-            $this->pattern = empty($match[2]) ? null : $match[2];
179
-        }
180
-    }
181
-
182
-    /**
183
-     * @param string $definition
184
-     * @param string[] $match
185
-     * @throws Exception
186
-     * @throws Exception
187
-     */
188
-    private function parseRange($definition, $match): void
189
-    {
190
-
191
-        if (!empty($match[3])) {
192
-            if ($match[1] === 'enum') {
193
-                throw new Exception("Range not allowed in enumeration definition: '{$definition}'");
194
-            }
195
-            if ($match[4] === '' && $match[5] === '') {
196
-                throw new Exception("Empty string range: '{$definition}'");
197
-            }
198
-            $exclusiveMinimum = $match[3] === '<';
199
-            $this->minLength = $match[4] === '' ? null : $match[4];
200
-            $this->maxLength = $match[5] === '' ? null : $match[5];
201
-            $exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null;
202
-            if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) {
203
-                self::swap($this->minLength, $this->maxLength);
204
-                self::swap($exclusiveMinimum, $exclusiveMaximum);
205
-            }
206
-            $this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength);
207
-            $this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength);
208
-        }
209
-    }
210
-
211
-    /**
212
-     * @param string $definition
213
-     * @param string[] $match
214
-     * @throws Exception
215
-     */
216
-    private function parseDefault($definition, $match): void
217
-    {
218
-        $this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null;
219
-    }
18
+	private static $formats = array(
19
+		'string' => '',
20
+		'byte' => 'byte',
21
+		'binary' => 'binary',
22
+		'password' => 'password',
23
+		'enum' => '',
24
+	);
25
+
26
+	/**
27
+	 * Name of the type
28
+	 * @var string
29
+	 */
30
+	protected $format = '';
31
+	protected $pattern;
32
+	protected $default;
33
+	protected $maxLength;
34
+	protected $minLength;
35
+	protected $enum = [];
36
+
37
+	/**
38
+	 * @param string $command The comment command
39
+	 * @param string $data Any data added after the command
40
+	 * @return AbstractType|boolean
41
+	 * @throws Exception
42
+	 * @throws Exception
43
+	 * @throws Exception
44
+	 */
45
+	public function handleCommand($command, $data = null)
46
+	{
47
+		switch (strtolower($command)) {
48
+			case 'default':
49
+				$this->default = $this->validateDefault($data);
50
+				return $this;
51
+
52
+			case 'pattern':
53
+				$this->pattern = $data;
54
+				return $this;
55
+
56
+			case 'enum':
57
+				if ($this->minLength !== null || $this->maxLength !== null) {
58
+					throw new Exception("Enumeration not allowed in ranged string: '{$data}'");
59
+				}
60
+				$words = self::wordSplit($data);
61
+				$this->enum = is_array($this->enum) ? array_merge($this->enum, $words) : $words;
62
+				return $this;
63
+		}
64
+
65
+		return parent::handleCommand($command, $data);
66
+	}
67
+
68
+	/**
69
+	 * Validate a default string value, depending on subtype
70
+	 *
71
+	 * @param string $value the value to validate
72
+	 * @return string the value after validation (might become trimmed)
73
+	 * @throws Exception
74
+	 */
75
+	protected function validateDefault($value): string
76
+	{
77
+		if (empty($value)) {
78
+			if ($this->format) {
79
+				$type = $this->format;
80
+			} else {
81
+				$type = $this->enum ? 'enum' : 'string';
82
+			}
83
+			throw new Exception("Empty {$type} default");
84
+		}
85
+
86
+		// trim and unescape quotes if needed
87
+		if (mb_strlen($value) >= 2
88
+			&& $value[0] === '"'
89
+			&& $value[mb_strlen($value) - 1] === '"'
90
+		) {
91
+			$value = stripcslashes(mb_substr($value, 1, -1));
92
+		}
93
+
94
+		if (!empty($this->enum) && !in_array($value, $this->enum, true)) {
95
+			throw new Exception("Invalid enum default: '{$value}'");
96
+		}
97
+
98
+		if ($this->maxLength !== null && mb_strlen($value) > $this->maxLength) {
99
+			if ($this->format) {
100
+				$type = $this->format;
101
+			} else {
102
+				$type = $this->enum ? 'enum' : 'string';
103
+			}
104
+			throw new Exception("Default {$type} length beyond maximum: '{$value}'");
105
+		}
106
+
107
+		if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
108
+			if ($this->format) {
109
+				$type = $this->format;
110
+			} else {
111
+				$type = $this->enum ? 'enum' : 'string';
112
+			}
113
+			throw new Exception("Default {$type} length beyond minimum: '{$value}'");
114
+		}
115
+
116
+		return $value;
117
+	}
118
+
119
+	public function toArray(): array
120
+	{
121
+		return self::arrayFilterNull(array_merge(array(
122
+			'type' => 'string',
123
+			'format' => empty($this->format) ? null : $this->format,
124
+			'pattern' => $this->pattern,
125
+			'default' => $this->default,
126
+			'minLength' => $this->minLength ? (int)$this->minLength : null,
127
+			'maxLength' => $this->maxLength ? (int)$this->maxLength : null,
128
+			'enum' => $this->enum,
129
+		), parent::toArray()));
130
+	}
131
+
132
+	public function __toString()
133
+	{
134
+		return __CLASS__;
135
+	}
136
+
137
+	/**
138
+	 * @throws Exception
139
+	 */
140
+	protected function parseDefinition($definition): void
141
+	{
142
+		$definition = self::trim($definition);
143
+
144
+		$match = [];
145
+		if (preg_match(self::REGEX_START . self::REGEX_FORMAT . self::REGEX_CONTENT . self::REGEX_RANGE . self::REGEX_DEFAULT . self::REGEX_END, $definition, $match) !== 1) {
146
+			throw new Exception("Unparseable string definition: '{$definition}'");
147
+		}
148
+
149
+		$this->parseFormat($definition, $match);
150
+		$this->parseContent($definition, $match);
151
+		$this->parseRange($definition, $match);
152
+		$this->parseDefault($definition, $match);
153
+	}
154
+
155
+	/**
156
+	 * @param string $definition
157
+	 * @param string[] $match
158
+	 * @throws Exception
159
+	 */
160
+	private function parseFormat($definition, $match): void
161
+	{
162
+		$type = strtolower($match[1]);
163
+		if (!isset(self::$formats[$type])) {
164
+			throw new Exception("Not a string: '{$definition}'");
165
+		}
166
+		$this->format = self::$formats[$type];
167
+	}
168
+
169
+	/**
170
+	 * @param string $definition
171
+	 * @param string[] $match
172
+	 */
173
+	private function parseContent($definition, $match): void
174
+	{
175
+		if (strtolower($match[1]) === 'enum') {
176
+			$this->enum = explode(',', $match[2]);
177
+		} else {
178
+			$this->pattern = empty($match[2]) ? null : $match[2];
179
+		}
180
+	}
181
+
182
+	/**
183
+	 * @param string $definition
184
+	 * @param string[] $match
185
+	 * @throws Exception
186
+	 * @throws Exception
187
+	 */
188
+	private function parseRange($definition, $match): void
189
+	{
190
+
191
+		if (!empty($match[3])) {
192
+			if ($match[1] === 'enum') {
193
+				throw new Exception("Range not allowed in enumeration definition: '{$definition}'");
194
+			}
195
+			if ($match[4] === '' && $match[5] === '') {
196
+				throw new Exception("Empty string range: '{$definition}'");
197
+			}
198
+			$exclusiveMinimum = $match[3] === '<';
199
+			$this->minLength = $match[4] === '' ? null : $match[4];
200
+			$this->maxLength = $match[5] === '' ? null : $match[5];
201
+			$exclusiveMaximum = isset($match[6]) ? ($match[6] === '>') : null;
202
+			if ($this->minLength !== null && $this->maxLength !== null && $this->minLength > $this->maxLength) {
203
+				self::swap($this->minLength, $this->maxLength);
204
+				self::swap($exclusiveMinimum, $exclusiveMaximum);
205
+			}
206
+			$this->minLength = $this->minLength === null ? null : max(0, $exclusiveMinimum ? $this->minLength + 1 : $this->minLength);
207
+			$this->maxLength = $this->maxLength === null ? null : max(0, $exclusiveMaximum ? $this->maxLength - 1 : $this->maxLength);
208
+		}
209
+	}
210
+
211
+	/**
212
+	 * @param string $definition
213
+	 * @param string[] $match
214
+	 * @throws Exception
215
+	 */
216
+	private function parseDefault($definition, $match): void
217
+	{
218
+		$this->default = isset($match[7]) && $match[7] !== '' ? $this->validateDefault($match[7]) : null;
219
+	}
220 220
 
221 221
 }
Please login to merge, or discard this patch.
Upper-Lower-Casing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -80,7 +80,7 @@  discard block
 block discarded – undo
80 80
             } else {
81 81
                 $type = $this->enum ? 'enum' : 'string';
82 82
             }
83
-            throw new Exception("Empty {$type} default");
83
+            throw new Exception("empty {$type} default");
84 84
         }
85 85
 
86 86
         // trim and unescape quotes if needed
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
             } else {
102 102
                 $type = $this->enum ? 'enum' : 'string';
103 103
             }
104
-            throw new Exception("Default {$type} length beyond maximum: '{$value}'");
104
+            throw new Exception("default {$type} length beyond maximum: '{$value}'");
105 105
         }
106 106
 
107 107
         if ($this->minLength !== null && mb_strlen($value) < $this->minLength) {
@@ -110,7 +110,7 @@  discard block
 block discarded – undo
110 110
             } else {
111 111
                 $type = $this->enum ? 'enum' : 'string';
112 112
             }
113
-            throw new Exception("Default {$type} length beyond minimum: '{$value}'");
113
+            throw new Exception("default {$type} length beyond minimum: '{$value}'");
114 114
         }
115 115
 
116 116
         return $value;
Please login to merge, or discard this patch.
tests/Swagger/Type/StringTypeTest.php 1 patch
Indentation   +303 added lines, -303 removed lines patch added patch discarded remove patch
@@ -9,308 +9,308 @@
 block discarded – undo
9 9
 class StringTypeTest extends TestCase
10 10
 {
11 11
 
12
-    protected $parent;
13
-
14
-    /**
15
-     * @covers StringType::__construct
16
-     */
17
-    public function testConstructNotAString()
18
-    {
19
-        $this->expectException(SwaggerException::class, "Not a string: 'wrong'");
20
-
21
-        $object = new StringType($this->parent, 'wrong');
22
-    }
23
-
24
-    /**
25
-     * @covers StringType::__construct
26
-     */
27
-    public function testConstructEmptyRange()
28
-    {
29
-        $this->expectException(SwaggerException::class, "Empty string range: 'string[,]=1'");
30
-
31
-        $object = new StringType($this->parent, 'string[,]=1');
32
-    }
33
-
34
-    /**
35
-     * @covers StringType::__construct
36
-     */
37
-    public function testConstructDefaultTooLongInclusive()
38
-    {
39
-        $this->expectException(SwaggerException::class, "Default string length beyond maximum: 'long'");
40
-
41
-        $object = new StringType($this->parent, 'string[,3]=long');
42
-    }
43
-
44
-    /**
45
-     * @covers StringType::__construct
46
-     */
47
-    public function testConstructDefaultTooLongExclusive()
48
-    {
49
-        $this->expectException(SwaggerException::class, "Default string length beyond maximum: 'long'");
50
-
51
-        $object = new StringType($this->parent, 'string[,4>=long');
52
-    }
53
-
54
-    /**
55
-     * @covers StringType::__construct
56
-     */
57
-    public function testConstructDefaultTooShortInclusive()
58
-    {
59
-        $this->expectException(SwaggerException::class, "Default string length beyond minimum: 'short'");
60
-
61
-        $object = new StringType($this->parent, 'string[6,]=short');
62
-    }
63
-
64
-    /**
65
-     * @covers StringType::__construct
66
-     */
67
-    public function testConstructDefaultTooShortExclusive()
68
-    {
69
-        $this->expectException(SwaggerException::class, "Default string length beyond minimum: 'short'");
70
-
71
-        $object = new StringType($this->parent, 'string<5,]=short');
72
-    }
73
-
74
-    /**
75
-     * @covers StringType::__construct
76
-     */
77
-    public function testConstructString()
78
-    {
79
-        $object = new StringType($this->parent, 'string');
80
-
81
-        $this->assertInstanceOf(StringType::class, $object);
82
-
83
-        $this->assertSame(array(
84
-            'type' => 'string',
85
-        ), $object->toArray());
86
-    }
87
-
88
-    /**
89
-     * @covers StringType::__construct
90
-     */
91
-    public function testConstructStringEmptyDefault()
92
-    {
93
-        $this->expectException(SwaggerException::class, "Unparseable string definition: 'string='");
94
-
95
-        $object = new StringType($this->parent, 'string= ');
96
-    }
97
-
98
-    /**
99
-     * @covers StringType::__construct
100
-     */
101
-    public function testConstructStringDefaultLengthInclusive()
102
-    {
103
-        $object = new StringType($this->parent, 'string[4,4]=word');
104
-
105
-        $this->assertInstanceOf(StringType::class, $object);
106
-
107
-        $this->assertSame(array(
108
-            'type' => 'string',
109
-            'default' => 'word',
110
-            'minLength' => 4,
111
-            'maxLength' => 4,
112
-        ), $object->toArray());
113
-    }
114
-
115
-    /**
116
-     * @covers StringType::__construct
117
-     */
118
-    public function testConstructStringDefaultLengthExclusive()
119
-    {
120
-        $object = new StringType($this->parent, 'string<3,5>=word');
121
-
122
-        $this->assertInstanceOf(StringType::class, $object);
123
-
124
-        $this->assertSame(array(
125
-            'type' => 'string',
126
-            'default' => 'word',
127
-            'minLength' => 4,
128
-            'maxLength' => 4,
129
-        ), $object->toArray());
130
-    }
131
-
132
-    /**
133
-     * @covers StringType::__construct
134
-     */
135
-    public function testConstructStringDefaultWithWhitespace()
136
-    {
137
-        $object = new StringType($this->parent, 'string="white space"');
138
-
139
-        $this->assertInstanceOf(StringType::class, $object);
140
-
141
-        $this->assertSame(array(
142
-            'type' => 'string',
143
-            'default' => 'white space',
144
-        ), $object->toArray());
145
-    }
146
-
147
-    /**
148
-     * @covers StringType::__construct
149
-     */
150
-    public function testConstructBinary()
151
-    {
152
-        $object = new StringType($this->parent, 'binary');
153
-
154
-        $this->assertInstanceOf(StringType::class, $object);
155
-
156
-        $this->assertSame(array(
157
-            'type' => 'string',
158
-            'format' => 'binary',
159
-        ), $object->toArray());
160
-    }
161
-
162
-    /**
163
-     * @covers StringType::__construct
164
-     */
165
-    public function testConstructEnumRange()
166
-    {
167
-        $this->expectException(SwaggerException::class, "Range not allowed in enumeration definition: 'enum(a,b)[,]'");
168
-
169
-        $object = new StringType($this->parent, 'enum(a,b)[,]');
170
-    }
171
-
172
-    /**
173
-     * @covers StringType::__construct
174
-     */
175
-    public function testConstructEnumInvalidDefault()
176
-    {
177
-        $this->expectException(SwaggerException::class, "Invalid enum default: 'c'");
178
-
179
-        $object = new StringType($this->parent, 'enum(a,b)=c');
180
-    }
181
-
182
-    /**
183
-     * @covers StringType::__construct
184
-     */
185
-    public function testConstructEnum()
186
-    {
187
-        $object = new StringType($this->parent, 'enum(a,b)');
188
-
189
-        $this->assertInstanceOf(StringType::class, $object);
190
-
191
-        $this->assertSame(array(
192
-            'type' => 'string',
193
-            'enum' => array('a', 'b'),
194
-        ), $object->toArray());
195
-    }
196
-
197
-    /**
198
-     * @covers StringType::__construct
199
-     */
200
-    public function testConstructEnumWithDefault()
201
-    {
202
-        $object = new StringType($this->parent, 'enum(a,b)=a');
203
-
204
-        $this->assertInstanceOf(StringType::class, $object);
205
-
206
-        $this->assertSame(array(
207
-            'type' => 'string',
208
-            'default' => 'a',
209
-            'enum' => array('a', 'b'),
210
-        ), $object->toArray());
211
-    }
212
-
213
-    /**
214
-     * @covers StringType::__construct
215
-     */
216
-    public function testConstructPattern()
217
-    {
218
-        $object = new StringType($this->parent, 'string([a-z])');
219
-
220
-        $this->assertInstanceOf(StringType::class, $object);
221
-
222
-        $this->assertSame(array(
223
-            'type' => 'string',
224
-            'pattern' => '[a-z]',
225
-        ), $object->toArray());
226
-    }
227
-
228
-    /**
229
-     * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
230
-     */
231
-    public function testCommandDefaultNoValue()
232
-    {
233
-        $object = new StringType($this->parent, 'string');
234
-
235
-        $this->assertInstanceOf(StringType::class, $object);
236
-
237
-        $this->expectException(SwaggerException::class, "Empty string default");
238
-        $object->handleCommand('default', '');
239
-    }
240
-
241
-    /**
242
-     * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
243
-     */
244
-    public function testCommandDefault()
245
-    {
246
-        $object = new StringType($this->parent, 'string');
247
-
248
-        $this->assertInstanceOf(StringType::class, $object);
249
-
250
-        $object->handleCommand('default', 'word');
251
-
252
-        $this->assertSame(array(
253
-            'type' => 'string',
254
-            'default' => 'word',
255
-        ), $object->toArray());
256
-    }
257
-
258
-    /**
259
-     * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
260
-     */
261
-    public function testCommandPattern()
262
-    {
263
-        $object = new StringType($this->parent, 'string');
264
-
265
-        $this->assertInstanceOf(StringType::class, $object);
266
-
267
-        $object->handleCommand('pattern', '[a-z]');
268
-
269
-        $this->assertSame(array(
270
-            'type' => 'string',
271
-            'pattern' => '[a-z]',
272
-        ), $object->toArray());
273
-    }
274
-
275
-    /**
276
-     * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
277
-     */
278
-    public function testCommandEnumWhenRange()
279
-    {
280
-        $object = new StringType($this->parent, 'string[0,]');
281
-
282
-        $this->assertInstanceOf(StringType::class, $object);
283
-
284
-        $this->expectException(SwaggerException::class, "Enumeration not allowed in ranged string: 'red green blue'");
285
-
286
-        $object->handleCommand('enum', 'red green blue');
287
-    }
288
-
289
-    /**
290
-     * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
291
-     */
292
-    public function testCommandEnum()
293
-    {
294
-        $object = new StringType($this->parent, 'string');
295
-
296
-        $this->assertInstanceOf(StringType::class, $object);
297
-
298
-        $object->handleCommand('enum', 'red green blue');
299
-
300
-        $this->assertSame(array(
301
-            'type' => 'string',
302
-            'enum' => array('red', 'green', 'blue'),
303
-        ), $object->toArray());
304
-    }
305
-
306
-    protected function setUp(): void
307
-    {
308
-        $this->parent = $this->getMockForAbstractClass(AbstractObject::class);
309
-    }
310
-
311
-    protected function assertPreConditions(): void
312
-    {
313
-        $this->assertInstanceOf(AbstractObject::class, $this->parent);
314
-    }
12
+	protected $parent;
13
+
14
+	/**
15
+	 * @covers StringType::__construct
16
+	 */
17
+	public function testConstructNotAString()
18
+	{
19
+		$this->expectException(SwaggerException::class, "Not a string: 'wrong'");
20
+
21
+		$object = new StringType($this->parent, 'wrong');
22
+	}
23
+
24
+	/**
25
+	 * @covers StringType::__construct
26
+	 */
27
+	public function testConstructEmptyRange()
28
+	{
29
+		$this->expectException(SwaggerException::class, "Empty string range: 'string[,]=1'");
30
+
31
+		$object = new StringType($this->parent, 'string[,]=1');
32
+	}
33
+
34
+	/**
35
+	 * @covers StringType::__construct
36
+	 */
37
+	public function testConstructDefaultTooLongInclusive()
38
+	{
39
+		$this->expectException(SwaggerException::class, "Default string length beyond maximum: 'long'");
40
+
41
+		$object = new StringType($this->parent, 'string[,3]=long');
42
+	}
43
+
44
+	/**
45
+	 * @covers StringType::__construct
46
+	 */
47
+	public function testConstructDefaultTooLongExclusive()
48
+	{
49
+		$this->expectException(SwaggerException::class, "Default string length beyond maximum: 'long'");
50
+
51
+		$object = new StringType($this->parent, 'string[,4>=long');
52
+	}
53
+
54
+	/**
55
+	 * @covers StringType::__construct
56
+	 */
57
+	public function testConstructDefaultTooShortInclusive()
58
+	{
59
+		$this->expectException(SwaggerException::class, "Default string length beyond minimum: 'short'");
60
+
61
+		$object = new StringType($this->parent, 'string[6,]=short');
62
+	}
63
+
64
+	/**
65
+	 * @covers StringType::__construct
66
+	 */
67
+	public function testConstructDefaultTooShortExclusive()
68
+	{
69
+		$this->expectException(SwaggerException::class, "Default string length beyond minimum: 'short'");
70
+
71
+		$object = new StringType($this->parent, 'string<5,]=short');
72
+	}
73
+
74
+	/**
75
+	 * @covers StringType::__construct
76
+	 */
77
+	public function testConstructString()
78
+	{
79
+		$object = new StringType($this->parent, 'string');
80
+
81
+		$this->assertInstanceOf(StringType::class, $object);
82
+
83
+		$this->assertSame(array(
84
+			'type' => 'string',
85
+		), $object->toArray());
86
+	}
87
+
88
+	/**
89
+	 * @covers StringType::__construct
90
+	 */
91
+	public function testConstructStringEmptyDefault()
92
+	{
93
+		$this->expectException(SwaggerException::class, "Unparseable string definition: 'string='");
94
+
95
+		$object = new StringType($this->parent, 'string= ');
96
+	}
97
+
98
+	/**
99
+	 * @covers StringType::__construct
100
+	 */
101
+	public function testConstructStringDefaultLengthInclusive()
102
+	{
103
+		$object = new StringType($this->parent, 'string[4,4]=word');
104
+
105
+		$this->assertInstanceOf(StringType::class, $object);
106
+
107
+		$this->assertSame(array(
108
+			'type' => 'string',
109
+			'default' => 'word',
110
+			'minLength' => 4,
111
+			'maxLength' => 4,
112
+		), $object->toArray());
113
+	}
114
+
115
+	/**
116
+	 * @covers StringType::__construct
117
+	 */
118
+	public function testConstructStringDefaultLengthExclusive()
119
+	{
120
+		$object = new StringType($this->parent, 'string<3,5>=word');
121
+
122
+		$this->assertInstanceOf(StringType::class, $object);
123
+
124
+		$this->assertSame(array(
125
+			'type' => 'string',
126
+			'default' => 'word',
127
+			'minLength' => 4,
128
+			'maxLength' => 4,
129
+		), $object->toArray());
130
+	}
131
+
132
+	/**
133
+	 * @covers StringType::__construct
134
+	 */
135
+	public function testConstructStringDefaultWithWhitespace()
136
+	{
137
+		$object = new StringType($this->parent, 'string="white space"');
138
+
139
+		$this->assertInstanceOf(StringType::class, $object);
140
+
141
+		$this->assertSame(array(
142
+			'type' => 'string',
143
+			'default' => 'white space',
144
+		), $object->toArray());
145
+	}
146
+
147
+	/**
148
+	 * @covers StringType::__construct
149
+	 */
150
+	public function testConstructBinary()
151
+	{
152
+		$object = new StringType($this->parent, 'binary');
153
+
154
+		$this->assertInstanceOf(StringType::class, $object);
155
+
156
+		$this->assertSame(array(
157
+			'type' => 'string',
158
+			'format' => 'binary',
159
+		), $object->toArray());
160
+	}
161
+
162
+	/**
163
+	 * @covers StringType::__construct
164
+	 */
165
+	public function testConstructEnumRange()
166
+	{
167
+		$this->expectException(SwaggerException::class, "Range not allowed in enumeration definition: 'enum(a,b)[,]'");
168
+
169
+		$object = new StringType($this->parent, 'enum(a,b)[,]');
170
+	}
171
+
172
+	/**
173
+	 * @covers StringType::__construct
174
+	 */
175
+	public function testConstructEnumInvalidDefault()
176
+	{
177
+		$this->expectException(SwaggerException::class, "Invalid enum default: 'c'");
178
+
179
+		$object = new StringType($this->parent, 'enum(a,b)=c');
180
+	}
181
+
182
+	/**
183
+	 * @covers StringType::__construct
184
+	 */
185
+	public function testConstructEnum()
186
+	{
187
+		$object = new StringType($this->parent, 'enum(a,b)');
188
+
189
+		$this->assertInstanceOf(StringType::class, $object);
190
+
191
+		$this->assertSame(array(
192
+			'type' => 'string',
193
+			'enum' => array('a', 'b'),
194
+		), $object->toArray());
195
+	}
196
+
197
+	/**
198
+	 * @covers StringType::__construct
199
+	 */
200
+	public function testConstructEnumWithDefault()
201
+	{
202
+		$object = new StringType($this->parent, 'enum(a,b)=a');
203
+
204
+		$this->assertInstanceOf(StringType::class, $object);
205
+
206
+		$this->assertSame(array(
207
+			'type' => 'string',
208
+			'default' => 'a',
209
+			'enum' => array('a', 'b'),
210
+		), $object->toArray());
211
+	}
212
+
213
+	/**
214
+	 * @covers StringType::__construct
215
+	 */
216
+	public function testConstructPattern()
217
+	{
218
+		$object = new StringType($this->parent, 'string([a-z])');
219
+
220
+		$this->assertInstanceOf(StringType::class, $object);
221
+
222
+		$this->assertSame(array(
223
+			'type' => 'string',
224
+			'pattern' => '[a-z]',
225
+		), $object->toArray());
226
+	}
227
+
228
+	/**
229
+	 * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
230
+	 */
231
+	public function testCommandDefaultNoValue()
232
+	{
233
+		$object = new StringType($this->parent, 'string');
234
+
235
+		$this->assertInstanceOf(StringType::class, $object);
236
+
237
+		$this->expectException(SwaggerException::class, "Empty string default");
238
+		$object->handleCommand('default', '');
239
+	}
240
+
241
+	/**
242
+	 * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
243
+	 */
244
+	public function testCommandDefault()
245
+	{
246
+		$object = new StringType($this->parent, 'string');
247
+
248
+		$this->assertInstanceOf(StringType::class, $object);
249
+
250
+		$object->handleCommand('default', 'word');
251
+
252
+		$this->assertSame(array(
253
+			'type' => 'string',
254
+			'default' => 'word',
255
+		), $object->toArray());
256
+	}
257
+
258
+	/**
259
+	 * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
260
+	 */
261
+	public function testCommandPattern()
262
+	{
263
+		$object = new StringType($this->parent, 'string');
264
+
265
+		$this->assertInstanceOf(StringType::class, $object);
266
+
267
+		$object->handleCommand('pattern', '[a-z]');
268
+
269
+		$this->assertSame(array(
270
+			'type' => 'string',
271
+			'pattern' => '[a-z]',
272
+		), $object->toArray());
273
+	}
274
+
275
+	/**
276
+	 * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
277
+	 */
278
+	public function testCommandEnumWhenRange()
279
+	{
280
+		$object = new StringType($this->parent, 'string[0,]');
281
+
282
+		$this->assertInstanceOf(StringType::class, $object);
283
+
284
+		$this->expectException(SwaggerException::class, "Enumeration not allowed in ranged string: 'red green blue'");
285
+
286
+		$object->handleCommand('enum', 'red green blue');
287
+	}
288
+
289
+	/**
290
+	 * @covers \SwaggerGen\Swagger\Type\StringType->handleCommand
291
+	 */
292
+	public function testCommandEnum()
293
+	{
294
+		$object = new StringType($this->parent, 'string');
295
+
296
+		$this->assertInstanceOf(StringType::class, $object);
297
+
298
+		$object->handleCommand('enum', 'red green blue');
299
+
300
+		$this->assertSame(array(
301
+			'type' => 'string',
302
+			'enum' => array('red', 'green', 'blue'),
303
+		), $object->toArray());
304
+	}
305
+
306
+	protected function setUp(): void
307
+	{
308
+		$this->parent = $this->getMockForAbstractClass(AbstractObject::class);
309
+	}
310
+
311
+	protected function assertPreConditions(): void
312
+	{
313
+		$this->assertInstanceOf(AbstractObject::class, $this->parent);
314
+	}
315 315
 
316 316
 }
Please login to merge, or discard this patch.