Skip to content

Commit e7a8f8d

Browse files
committed
Add a new getFormat method to get the instance format
1 parent 330afab commit e7a8f8d

File tree

3 files changed

+82
-115
lines changed

3 files changed

+82
-115
lines changed

src/MagicConstant.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use CuyZ\MagicConstant\Exception\InvalidValueException;
99
use ReflectionClass;
1010

11+
use function array_keys;
12+
1113
abstract class MagicConstant
1214
{
1315
/** @var mixed */
@@ -81,6 +83,24 @@ public function getKey(): string
8183
return $key;
8284
}
8385

86+
/**
87+
* Returns the current instance format.
88+
*
89+
* @return int|string|null
90+
*/
91+
public function getFormat()
92+
{
93+
$values = static::toArray();
94+
95+
foreach ($values[$this->getKey()] as $format => $value) {
96+
if ($value === $this->value) {
97+
return $format;
98+
}
99+
}
100+
101+
return null;
102+
}
103+
84104
/**
85105
* @return string
86106
*/
@@ -183,10 +203,7 @@ public static function values(string $pattern = null): array
183203
return $out;
184204
}
185205

186-
/**
187-
* @return array
188-
*/
189-
public static function toArray(): array
206+
private static function toArray(): array
190207
{
191208
if (!array_key_exists(static::class, static::$cache)) {
192209
$reflection = new ReflectionClass(static::class);
@@ -238,7 +255,7 @@ public static function isValidKey($key): bool
238255
* @param mixed $value
239256
* @return false|string
240257
*/
241-
public static function search($value)
258+
private static function search($value)
242259
{
243260
/**
244261
* @var string $constant

tests/MagicConstantTest.php

Lines changed: 52 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
class MagicConstantTest extends TestCase
1414
{
1515
/** @test */
16-
public function the_constructor_throws_for_an_invalid_value()
16+
public function the_constructor_throws_for_an_invalid_value(): void
1717
{
1818
/* *** Assertion *** */
1919
$this->expectException(InvalidValueException::class);
@@ -26,7 +26,7 @@ public function the_constructor_throws_for_an_invalid_value()
2626
}
2727

2828
/** @test */
29-
public function the_constructor_throws_for_other_magic_constant_instance()
29+
public function the_constructor_throws_for_other_magic_constant_instance(): void
3030
{
3131
/* *** Assertion *** */
3232
$this->expectException(InvalidValueException::class);
@@ -39,7 +39,7 @@ public function the_constructor_throws_for_other_magic_constant_instance()
3939
}
4040

4141
/** @test */
42-
public function throws_for_wrong_key()
42+
public function throws_for_wrong_key(): void
4343
{
4444
/* *** Assertion *** */
4545
$this->expectException(InvalidKeyException::class);
@@ -50,7 +50,7 @@ public function throws_for_wrong_key()
5050
}
5151

5252
/** @test */
53-
public function values_are_case_sensitive()
53+
public function values_are_case_sensitive(): void
5454
{
5555
/* *** Initialisation *** */
5656
$this->expectException(InvalidValueException::class);
@@ -63,7 +63,7 @@ public function values_are_case_sensitive()
6363
}
6464

6565
/** @test */
66-
public function getValue_throws_for_an_invalid_format()
66+
public function getValue_throws_for_an_invalid_format(): void
6767
{
6868
/* *** Assertion *** */
6969
$this->expectException(InvalidFormatException::class);
@@ -76,40 +76,33 @@ public function getValue_throws_for_an_invalid_format()
7676
}
7777

7878
/** @test */
79-
public function create_all_possible_values_from_the_constructor()
79+
public function create_all_possible_values_from_the_constructor(): void
8080
{
81-
/* *** Initialisation *** */
82-
$constants = FakeMagicConstant::toArray();
83-
8481
/* *** Process *** */
85-
foreach ($constants as $constant => $values) {
86-
foreach ($values as $value) {
87-
$magicConstant = new FakeMagicConstant($value);
88-
89-
/* *** Assertion *** */
90-
self::assertSame($value, $magicConstant->getValue());
91-
}
92-
}
82+
self::assertSame('foo', (new FakeMagicConstant('foo'))->getValue());
83+
self::assertSame(123, (new FakeMagicConstant(123))->getValue());
84+
self::assertSame('bar', (new FakeMagicConstant('bar'))->getValue());
85+
self::assertSame('A', (new FakeMagicConstant('A'))->getValue());
86+
self::assertSame('B', (new FakeMagicConstant('B'))->getValue());
87+
self::assertSame('C', (new FakeMagicConstant('C'))->getValue());
88+
self::assertSame('value A', (new FakeMagicConstant('value A'))->getValue());
89+
self::assertSame('value B', (new FakeMagicConstant('value B'))->getValue());
90+
self::assertSame('value C', (new FakeMagicConstant('value C'))->getValue());
9391
}
9492

9593
/** @test */
96-
public function create_all_possible_values_from_the_static_method()
94+
public function create_all_possible_values_from_the_static_method(): void
9795
{
98-
/* *** Initialisation *** */
99-
$constants = FakeMagicConstant::toArray();
100-
10196
/* *** Process *** */
102-
foreach ($constants as $constant => $values) {
103-
/** @var MagicConstant $magicConstant */
104-
$magicConstant = FakeMagicConstant::$constant();
105-
106-
/* *** Assertion *** */
107-
self::assertSame(reset($values), $magicConstant->getValue());
108-
}
97+
self::assertSame('foo', FakeMagicConstant::TYPE_STRING()->getValue());
98+
self::assertSame(123, FakeMagicConstant::TYPE_INTEGER()->getValue());
99+
self::assertSame('bar', FakeMagicConstant::TYPE_ARRAY_SINGLE()->getValue());
100+
self::assertSame('A', FakeMagicConstant::TYPE_ARRAY_MULTIPLE()->getValue());
101+
self::assertSame('value A', FakeMagicConstant::TYPE_ARRAY_FORMATS()->getValue());
109102
}
110103

111104
/** @test */
112-
public function create_instance_from_another_instance()
105+
public function create_instance_from_another_instance(): void
113106
{
114107
/* *** Initialisation *** */
115108
$constants = FakeMagicConstant::values();
@@ -128,7 +121,7 @@ public function create_instance_from_another_instance()
128121
* @dataProvider fakeMagicConstantDataProvider
129122
* @param FakeMagicConstant $magicConstant
130123
*/
131-
public function getValue_returns_the_correct_value(FakeMagicConstant $magicConstant)
124+
public function getValue_returns_the_correct_value(FakeMagicConstant $magicConstant): void
132125
{
133126
/* *** Process *** */
134127
$actualMagicConstant = new FakeMagicConstant($magicConstant->getValue());
@@ -145,7 +138,7 @@ public function getValue_returns_the_correct_value(FakeMagicConstant $magicConst
145138
* @param mixed $expectedValue
146139
* @param string|int $format
147140
*/
148-
public function getValue_returns_the_correct_value_depending_on_the_format(FakeMagicConstant $magicConstant, $key, $expectedValue, $format)
141+
public function getValue_returns_the_correct_value_depending_on_the_format(FakeMagicConstant $magicConstant, $key, $expectedValue, $format): void
149142
{
150143
/* *** Process *** */
151144
$actualMagicConstant = new FakeMagicConstant($magicConstant->getValue());
@@ -161,7 +154,7 @@ public function getValue_returns_the_correct_value_depending_on_the_format(FakeM
161154
* @param FakeMagicConstant $magicConstant
162155
* @param string $expectedKey
163156
*/
164-
public function getKey_returns_the_correct_value(FakeMagicConstant $magicConstant, string $expectedKey)
157+
public function getKey_returns_the_correct_value(FakeMagicConstant $magicConstant, string $expectedKey): void
165158
{
166159
/* *** Process *** */
167160
$actualMagicConstant = new FakeMagicConstant($magicConstant->getValue());
@@ -177,7 +170,7 @@ public function getKey_returns_the_correct_value(FakeMagicConstant $magicConstan
177170
* @param string|int $key
178171
* @param mixed $expectedValue
179172
*/
180-
public function toString_returns_the_correct_value(FakeMagicConstant $magicConstant, $key, $expectedValue)
173+
public function toString_returns_the_correct_value(FakeMagicConstant $magicConstant, $key, $expectedValue): void
181174
{
182175
/* *** Process *** */
183176
$actualValue = (string)$magicConstant;
@@ -187,7 +180,7 @@ public function toString_returns_the_correct_value(FakeMagicConstant $magicConst
187180
}
188181

189182
/** @test */
190-
public function keys_returns_the_list_of_possible_keys()
183+
public function keys_returns_the_list_of_possible_keys(): void
191184
{
192185
/* *** Initialisation *** */
193186
$expectedConstants = [
@@ -206,7 +199,7 @@ public function keys_returns_the_list_of_possible_keys()
206199
}
207200

208201
/** @test */
209-
public function values_returns_an_array_of_possible_values()
202+
public function values_returns_an_array_of_possible_values(): void
210203
{
211204
/* *** Initialisation *** */
212205
$extectedValues = [
@@ -224,40 +217,13 @@ public function values_returns_an_array_of_possible_values()
224217
self::assertEquals($extectedValues, $actualValues);
225218
}
226219

227-
/**
228-
* This test must be run in a separate process to avoid the memoization of values.
229-
* @runInSeparateProcess
230-
* @test
231-
*/
232-
public function toArray_returns_an_associative_array_of_constants_and_their_values()
233-
{
234-
/* *** Initialisation *** */
235-
$expectedValues = [
236-
'TYPE_STRING' => ['foo'],
237-
'TYPE_INTEGER' => [123],
238-
'TYPE_ARRAY_SINGLE' => ['bar'],
239-
'TYPE_ARRAY_MULTIPLE' => ['A', 'B', 'C'],
240-
'TYPE_ARRAY_FORMATS' => [
241-
FakeMagicConstant::FORMAT_A => 'value A',
242-
FakeMagicConstant::FORMAT_B => 'value B',
243-
FakeMagicConstant::FORMAT_C => 'value C',
244-
],
245-
];
246-
247-
/* *** Process *** */
248-
$actualValues = FakeMagicConstant::toArray();
249-
250-
/* *** Assertion *** */
251-
self::assertSame($expectedValues, $actualValues);
252-
}
253-
254220
/**
255221
* @test
256222
* @dataProvider isValidValueDataProvider
257223
* @param mixed $value
258224
* @param $isValid
259225
*/
260-
public function isValidValue_checks_if_a_value_is_valid($value, bool $isValid)
226+
public function isValidValue_checks_if_a_value_is_valid($value, bool $isValid): void
261227
{
262228
/* *** Process *** */
263229
$actualIsValid = FakeMagicConstant::isValidValue($value);
@@ -291,7 +257,7 @@ public function isValidValueDataProvider(): array
291257
* @param mixed $key
292258
* @param $isValid
293259
*/
294-
public function isValidKey_checks_if_a_key_is_valid($key, bool $isValid)
260+
public function isValidKey_checks_if_a_key_is_valid($key, bool $isValid): void
295261
{
296262
/* *** Process *** */
297263
$actualIsValid = FakeMagicConstant::isValidKey($key);
@@ -317,48 +283,14 @@ public function isValidKeyDataProvider(): array
317283
];
318284
}
319285

320-
/**
321-
* @test
322-
* @dataProvider searchDataProvider
323-
* @param mixed $value
324-
* @param mixed $expectedKey
325-
*/
326-
public function search_finds_the_correct_key($value, $expectedKey)
327-
{
328-
/* *** Process *** */
329-
$actualKey = FakeMagicConstant::search($value);
330-
331-
/* *** Assertion *** */
332-
self::assertSame($expectedKey, $actualKey);
333-
}
334-
335-
/**
336-
* @return array
337-
*/
338-
public function searchDataProvider(): array
339-
{
340-
return [
341-
// Valid
342-
['foo', 'TYPE_STRING'],
343-
[123, 'TYPE_INTEGER'],
344-
['bar', 'TYPE_ARRAY_SINGLE'],
345-
['A', 'TYPE_ARRAY_MULTIPLE'],
346-
['B', 'TYPE_ARRAY_MULTIPLE'],
347-
['C', 'TYPE_ARRAY_MULTIPLE'],
348-
349-
// Invalid
350-
['invalid', false],
351-
];
352-
}
353-
354286
/**
355287
* @test
356288
* @dataProvider equalsDataProvider
357289
* @param MagicConstant $magicConstantA
358290
* @param mixed $magicConstantB
359291
* @param bool $expectedResult
360292
*/
361-
public function equals_compares_values(MagicConstant $magicConstantA, $magicConstantB, bool $expectedResult)
293+
public function equals_compares_values(MagicConstant $magicConstantA, $magicConstantB, bool $expectedResult): void
362294
{
363295
/* *** Process *** */
364296
$actualResult = $magicConstantA->equals($magicConstantB);
@@ -400,7 +332,7 @@ public function equalsDataProvider(): array
400332
* @param array $values
401333
* @param bool $expectedResult
402334
*/
403-
public function in_returns_true_if_at_least_one_value_is_correct(MagicConstant $magicConstant, array $values, bool $expectedResult)
335+
public function in_returns_true_if_at_least_one_value_is_correct(MagicConstant $magicConstant, array $values, bool $expectedResult): void
404336
{
405337
self::assertSame($expectedResult, $magicConstant->in($values));
406338
self::assertSame($expectedResult, $magicConstant->in(array_reverse($values)));
@@ -447,7 +379,7 @@ public function fakeMagicConstantDataProvider(): array
447379
* @param MagicConstant $magicConstant
448380
* @param array $expectedValues
449381
*/
450-
public function getAllFormats_returns_instances_in_all_possible_formats(MagicConstant $magicConstant, array $expectedValues)
382+
public function getAllFormats_returns_instances_in_all_possible_formats(MagicConstant $magicConstant, array $expectedValues): void
451383
{
452384
/* *** Process *** */
453385
$actualValues = $magicConstant->getAllFormats();
@@ -486,7 +418,7 @@ public function allFormatsDataProvider(): array
486418
}
487419

488420
/** @test */
489-
public function get_new_instance_in_specific_format()
421+
public function get_new_instance_in_specific_format(): void
490422
{
491423
/* *** Initialisation *** */
492424
$base = FakeMagicConstant::TYPE_ARRAY_FORMATS();
@@ -502,7 +434,7 @@ public function get_new_instance_in_specific_format()
502434
self::assertSame('value C', $instanceFormatC->getValue());
503435
}
504436

505-
public function test_normalize_resets_an_instance_to_the_first_format()
437+
public function test_normalize_resets_an_instance_to_the_first_format(): void
506438
{
507439
/* *** Initialisation *** */
508440
$withoutFormats = new FakeMagicConstant('B');
@@ -551,7 +483,7 @@ public function allValuesDataProvider(): array
551483
* @param MagicConstant $magicConstant
552484
* @param array $expectedValues
553485
*/
554-
public function test_return_values_in_all_formats(MagicConstant $magicConstant, array $expectedValues)
486+
public function test_return_values_in_all_formats(MagicConstant $magicConstant, array $expectedValues): void
555487
{
556488
/* *** Process *** */
557489
$actualValues = $magicConstant->getAllValues();
@@ -560,12 +492,27 @@ public function test_return_values_in_all_formats(MagicConstant $magicConstant,
560492
self::assertSame($expectedValues, $actualValues);
561493
}
562494

563-
public function test_has_custom_value_setter()
495+
public function test_has_custom_value_setter(): void
564496
{
565497
$magicConstant1 = new CustomSetValueMagicConstant('FOO');
566498
$magicConstant2 = new CustomSetValueMagicConstant('foo');
567499

568500
self::assertSame('foo', $magicConstant1->getValue());
569501
self::assertSame('foo', $magicConstant2->getValue());
570502
}
503+
504+
public function test_getFormat_returns_the_instance_format(): void
505+
{
506+
self::assertEquals(0, (new FakeMagicConstant('foo'))->getFormat());
507+
self::assertEquals(0, (new FakeMagicConstant(123))->getFormat());
508+
self::assertEquals(0, (new FakeMagicConstant('bar'))->getFormat());
509+
510+
self::assertEquals(0, (new FakeMagicConstant('A'))->getFormat());
511+
self::assertEquals(1, (new FakeMagicConstant('B'))->getFormat());
512+
self::assertEquals(2, (new FakeMagicConstant('C'))->getFormat());
513+
514+
self::assertEquals('format A', (new FakeMagicConstant('value A'))->getFormat());
515+
self::assertEquals('format B', (new FakeMagicConstant('value B'))->getFormat());
516+
self::assertEquals('format C', (new FakeMagicConstant('value C'))->getFormat());
517+
}
571518
}

0 commit comments

Comments
 (0)