Skip to content

Commit e4ff518

Browse files
Merge pull request #6 from php-strictus/feature/test-suite
further work on the test suite
2 parents c41432b + 5e205b6 commit e4ff518

12 files changed

+226
-35
lines changed

src/Traits/StrictusTyping.php

+23-12
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@
1212
*/
1313
trait StrictusTyping
1414
{
15-
public mixed $value;
15+
private mixed $value;
1616

1717
/**
1818
* @param mixed $value
1919
* @return mixed
2020
*/
2121
public function __invoke(mixed $value = new StrictusUndefined()): mixed
2222
{
23-
if (($value === null || $value instanceof StrictusUndefined) && ! $this->nullable) {
23+
if ($value instanceof StrictusUndefined) {
24+
return $this->value;
25+
}
26+
27+
if ($value === null && !$this->nullable) {
2428
throw new StrictusTypeException($this->errorMessage);
2529
}
2630

27-
if (! ($value instanceof StrictusUndefined)) {
28-
if (gettype($value) !== $this->instanceType) {
29-
if ($this->nullable && $value !== null) {
30-
throw new StrictusTypeException($this->errorMessage);
31-
}
31+
if (gettype($value) !== $this->instanceType) {
32+
if ($this->nullable && $value !== null) {
33+
throw new StrictusTypeException($this->errorMessage);
3234
}
33-
34-
$this->value = $value;
35-
36-
return $this;
3735
}
3836

39-
return $this->value;
37+
$this->value = $value;
38+
39+
return $this;
4040
}
4141

4242
/**
@@ -68,4 +68,15 @@ public function __set(string $name, mixed $value): void
6868

6969
$this->value = $value;
7070
}
71+
72+
public function handleInstantiation(mixed $value)
73+
{
74+
if (gettype($value) !== $this->instanceType) {
75+
if (!$this->nullable) {
76+
throw new StrictusTypeException($this->errorMessage);
77+
} else if ($value !== null) {
78+
throw new StrictusTypeException($this->errorMessage);
79+
}
80+
}
81+
}
7182
}

src/Types/StrictusArray.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ final class StrictusArray implements StrictusTypeInterface
2222
* @param mixed $value
2323
* @param bool $nullable
2424
*/
25-
public function __construct(mixed $value, private bool $nullable)
25+
public function __construct(private mixed $value, private bool $nullable)
2626
{
2727
if ($this->nullable) {
2828
$this->errorMessage .= ' Or Null';
2929
}
30+
31+
$this->handleInstantiation($value);
3032
}
3133
}

src/Types/StrictusBoolean.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ final class StrictusBoolean implements StrictusTypeInterface
2222
* @param mixed $value
2323
* @param bool $nullable
2424
*/
25-
public function __construct(mixed $value, private bool $nullable)
25+
public function __construct(private mixed $value, private bool $nullable)
2626
{
2727
if ($this->nullable) {
2828
$this->errorMessage .= ' Or Null';
2929
}
30+
31+
$this->handleInstantiation($value);
3032
}
3133
}

src/Types/StrictusFloat.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,21 @@ final class StrictusFloat implements StrictusTypeInterface
1414
{
1515
use StrictusTyping;
1616

17-
private string $instanceType = 'float';
17+
//this is the internal type name given to what we call float
18+
private string $instanceType = 'double';
1819

1920
private string $errorMessage = 'Expected Float';
2021

2122
/**
2223
* @param mixed $value
2324
* @param bool $nullable
2425
*/
25-
public function __construct(mixed $value, private bool $nullable)
26+
public function __construct(private mixed $value, private bool $nullable)
2627
{
2728
if ($nullable) {
2829
$this->errorMessage .= ' Or Null';
2930
}
31+
32+
$this->handleInstantiation($value);
3033
}
3134
}

src/Types/StrictusInteger.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ final class StrictusInteger implements StrictusTypeInterface
2222
* @param mixed $value
2323
* @param bool $nullable
2424
*/
25-
public function __construct(mixed $value, private bool $nullable)
25+
public function __construct(private mixed $value, private bool $nullable)
2626
{
2727
if ($this->nullable) {
2828
$this->errorMessage .= ' Or Null';
2929
}
30+
31+
$this->handleInstantiation($value);
3032
}
3133
}

src/Types/StrictusObject.php

+2
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,7 @@ public function __construct(mixed $value, private bool $nullable)
2727
if ($this->nullable) {
2828
$this->errorMessage .= ' Or Null';
2929
}
30+
31+
$this->handleInstantiation($value);
3032
}
3133
}

src/Types/StrictusString.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@ final class StrictusString implements StrictusTypeInterface
2222
* @param mixed $value
2323
* @param bool $nullable
2424
*/
25-
public function __construct(mixed $value, private bool $nullable)
25+
public function __construct(private mixed $value, private bool $nullable)
2626
{
2727
if ($this->nullable) {
2828
$this->errorMessage .= ' Or Null';
2929
}
30+
31+
$this->handleInstantiation($value);
3032
}
3133
}

tests/Unit/ArrayTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
$myArray = Strictus::array([1, 2, 3]);
1515

1616
expect($myArray->value)
17-
->toBe([1, 2, 3])
17+
->toEqualCanonicalizing([1, 2, 3])
1818
->and($myArray())
19-
->toBe([1, 2, 3]);
19+
->toEqualCanonicalizing([1, 2, 3]);
2020
});
2121

2222
it('sets the correct array value', function () {
2323
$myArray = Strictus::array([1, 2, 3]);
2424

2525
expect($myArray->value)
26-
->toBe([1, 2, 3])
26+
->toEqualCanonicalizing([1, 2, 3])
2727
->and($myArray())
28-
->toBe([1, 2, 3]);
28+
->toEqualCanonicalizing([1, 2, 3]);
2929

3030
$myArray->value = [4, 5, 6];
31-
expect($myArray)->toBe([4, 5, 6]);
31+
expect($myArray->value)->toEqualCanonicalizing([4, 5, 6]);
3232

3333
$myArray([7, 8, 9]);
34-
expect($myArray)->toBe([7, 8, 9]);
34+
expect($myArray())->toEqualCanonicalizing([7, 8, 9]);
3535
});
3636

3737
it('instantiates a nullable array variable with array method', function () {
@@ -47,9 +47,9 @@
4747
});
4848

4949
it('throws exception when trying to instantiate an array as nullable with array method', function () {
50-
expect(Strictus::array(null))->toThrow(StrictusTypeException::class);
50+
expect(fn() => Strictus::array(null))->toThrow(StrictusTypeException::class);
5151
});
5252

5353
it('throws exception when trying to instantiate an array with wrong type', function () {
54-
expect(Strictus::array('foo'))->toThrow(StrictusTypeException::class);
54+
expect(fn() => Strictus::array('foo'))->toThrow(StrictusTypeException::class);
5555
});

tests/Unit/BooleanTest.php

+26-6
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,41 @@
1111
});
1212

1313
it('instantiates a nullable boolean variable with boolean method', function () {
14-
$myArray = Strictus::boolean(null, true);
15-
expect($myArray)
14+
$myBoolean = Strictus::boolean(null, true);
15+
expect($myBoolean)
1616
->toBeInstanceOf(StrictusBoolean::class);
1717
});
1818

1919
it('instantiates a nullable boolean variable with nullableBoolean method', function () {
20-
$myArray = Strictus::nullableBoolean(null);
21-
expect($myArray)
20+
$myBoolean = Strictus::nullableBoolean(null);
21+
expect($myBoolean)
2222
->toBeInstanceOf(StrictusBoolean::class);
2323
});
2424

2525
it('throws exception when trying to instantiate a boolean as nullable with boolean method', function () {
26-
expect(Strictus::boolean(null))->toThrow(StrictusTypeException::class);
26+
expect(fn () => Strictus::boolean(null))->toThrow(StrictusTypeException::class);
2727
});
2828

2929
it('throws exception when trying to instantiate a boolean with wrong type', function () {
30-
expect(Strictus::boolean('foo'))->toThrow(StrictusTypeException::class);
30+
expect(fn () => Strictus::boolean('foo'))->toThrow(StrictusTypeException::class);
31+
});
32+
33+
it('returns value correctly', function () {
34+
$myBoolean = Strictus::boolean(true);
35+
36+
expect($myBoolean())->toBeTrue();
37+
expect($myBoolean->value)->toBeTrue();
3138
});
39+
40+
it('changes value correctly', function () {
41+
$myBoolean = Strictus::boolean(true);
42+
43+
$myBoolean->value = false;
44+
45+
expect($myBoolean())->toBeFalse()->and($myBoolean->value)->toBeFalse();
46+
47+
$myBoolean2 = Strictus::boolean(true);
48+
$myBoolean2(false);
49+
50+
expect($myBoolean2())->toBeFalse()->and($myBoolean2->value)->toBeFalse();
51+
});

tests/Unit/FloatTest.php

+50-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
<?php
1+
<?php
2+
3+
use Strictus\Exceptions\StrictusTypeException;
4+
use Strictus\Strictus;
5+
use Strictus\Types\StrictusFloat;
6+
7+
it('instantiates a float variable', function () {
8+
$myFloat = Strictus::float(3.14);
9+
expect($myFloat)
10+
->toBeInstanceOf(StrictusFloat::class);
11+
});
12+
13+
it('instantiates a nullable float variable with float method', function () {
14+
$myFloat = Strictus::float(null, true);
15+
expect($myFloat)
16+
->toBeInstanceOf(StrictusFloat::class);
17+
});
18+
19+
it('instantiates a nullable boolean variable with nullableBoolean method', function () {
20+
$myFloat = Strictus::nullableFloat(null);
21+
expect($myFloat)
22+
->toBeInstanceOf(StrictusFloat::class);
23+
});
24+
25+
it('throws exception when trying to instantiate a boolean as nullable with boolean method', function () {
26+
expect(fn () => Strictus::float(null))->toThrow(StrictusTypeException::class);
27+
});
28+
29+
it('throws exception when trying to instantiate a boolean with wrong type', function () {
30+
expect(fn () => Strictus::float('foo'))->toThrow(StrictusTypeException::class);
31+
});
32+
33+
it('returns value correctly', function () {
34+
$myFloat = Strictus::float(3.14);
35+
36+
expect($myFloat())->toEqual(3.14)->and($myFloat->value)->toEqual(3.14);
37+
});
38+
39+
it('changes value correctly', function () {
40+
$myFloat = Strictus::float(3.14);
41+
42+
$myFloat->value = 1.12;
43+
44+
expect($myFloat())->toEqual(1.12)->and($myFloat->value)->toEqual(1.12);
45+
46+
$myFloat2 = Strictus::float(3.14);
47+
$myFloat2(1.12);
48+
49+
expect($myFloat2())->toEqual(1.12)->and($myFloat2->value)->toEqual(1.12);
50+
});

tests/Unit/IntegerTest.php

+50-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
<?php
1+
<?php
2+
3+
use Strictus\Exceptions\StrictusTypeException;
4+
use Strictus\Strictus;
5+
use Strictus\Types\StrictusInteger;
6+
7+
it('instantiates a integer variable', function () {
8+
$myInt = Strictus::int(3);
9+
expect($myInt)
10+
->toBeInstanceOf(StrictusInteger::class);
11+
});
12+
13+
it('instantiates a nullable integer variable with integer method', function () {
14+
$myInt = Strictus::int(null, true);
15+
expect($myInt)
16+
->toBeInstanceOf(StrictusInteger::class);
17+
});
18+
19+
it('instantiates a nullable integer variable with nullableinteger method', function () {
20+
$myInt = Strictus::nullableInt(null);
21+
expect($myInt)
22+
->toBeInstanceOf(StrictusInteger::class);
23+
});
24+
25+
it('throws exception when trying to instantiate a integer as nullable with integer method', function () {
26+
expect(fn () => Strictus::int(null))->toThrow(StrictusTypeException::class);
27+
});
28+
29+
it('throws exception when trying to instantiate a integer with wrong type', function () {
30+
expect(fn () => Strictus::int('foo'))->toThrow(StrictusTypeException::class);
31+
});
32+
33+
it('returns value correctly', function () {
34+
$myInt = Strictus::int(3);
35+
36+
expect($myInt())->toEqual(3)->and($myInt->value)->toEqual(3);
37+
});
38+
39+
it('changes value correctly', function () {
40+
$myInt = Strictus::int(3);
41+
42+
$myInt->value = 1;
43+
44+
expect($myInt())->toEqual(1)->and($myInt->value)->toEqual(1);
45+
46+
$myInt2 = Strictus::int(3);
47+
$myInt2(1);
48+
49+
expect($myInt2())->toEqual(1)->and($myInt2->value)->toEqual(1);
50+
});

0 commit comments

Comments
 (0)