diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..d353202 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1,4 @@ +# These are supported funding model platforms + +github: [WendellAdriel] +custom: ["https://www.paypal.me/wendelladriel"] diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 0000000..ae8b5c5 --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,33 @@ +name: run-tests + +on: [push, pull_request] + +jobs: + test: + runs-on: ubuntu-latest + strategy: + matrix: + php: [8.2, 8.1] + dependency-version: [prefer-lowest, prefer-stable] + + name: PHP ${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + tools: composer:v2 + coverage: none + + - name: Install PHP dependencies + run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress + + - name: Code Style 👨‍🏭 + run: composer test:lint + + - name: Pest Tests 🧫 + run: composer test:unit diff --git a/README.md b/README.md index 4b2d4b2..052db83 100644 --- a/README.md +++ b/README.md @@ -39,29 +39,29 @@ You will need to use the `Strictus` class (`use Strictus\Strictus;`) in any clas you can then strictly type a variable with any of the below methods: -| Type | Nullable | Method | -|------------|----------|------------------------------------| -| String | No | Strictus::string($value) | -| String | Yes | Strictus::string($value, true) | -| String | Yes | Strictus::nullableString($value) | -| Integer | No | Strictus::int($value) | -| Integer | Yes | Strictus::int($value, true) | -| Integer | Yes | Strictus::nullableInt($value) | -| Float | No | Strictus::float($value) | -| Float | Yes | Strictus::float($value, true) | -| Float | Yes | Strictus::nullableFloat($value, true) | -| Boolean | No | Strictus::boolean($value) | -| Boolean | Yes | Strictus::boolean($value, true) | -| Boolean | Yes | Strictus::nullableBoolean($value) | -| Array | No | Strictus::array($value) | -| Array | Yes | Strictus::array($value, true) | -| Array | Yes | Strictus::nullableArray($value) | -| Object | No | Strictus::object($value) | -| Object | Yes | Strictus::object($value, true) | -| Object | Yes | Strictus::nullableObject($value) | -| Class Type | No | Strictus::instance($value) | -| Class Type | Yes | Strictus::instance($value, true) | -| Class Type | Yes | Strictus::nullableInstance($value) | +| Type | Nullable | Method | +|------------|----------|---------------------------------------------------| +| String | No | Strictus::string($value) | +| String | Yes | Strictus::string($value, true) | +| String | Yes | Strictus::nullableString($value) | +| Integer | No | Strictus::int($value) | +| Integer | Yes | Strictus::int($value, true) | +| Integer | Yes | Strictus::nullableInt($value) | +| Float | No | Strictus::float($value) | +| Float | Yes | Strictus::float($value, true) | +| Float | Yes | Strictus::nullableFloat($value, true) | +| Boolean | No | Strictus::boolean($value) | +| Boolean | Yes | Strictus::boolean($value, true) | +| Boolean | Yes | Strictus::nullableBoolean($value) | +| Array | No | Strictus::array($value) | +| Array | Yes | Strictus::array($value, true) | +| Array | Yes | Strictus::nullableArray($value) | +| Object | No | Strictus::object($value) | +| Object | Yes | Strictus::object($value, true) | +| Object | Yes | Strictus::nullableObject($value) | +| Class Type | No | Strictus::instance($instanceType, $value) | +| Class Type | Yes | Strictus::instance($instanceType, $value, true) | +| Class Type | Yes | Strictus::nullableInstance($instanceType, $value) | Once you have your typed variable created, you also have choices on how to use this. @@ -87,7 +87,7 @@ $myString->value = 'goodbye'; Both of these forms will work for any of the types. -## Error Handling +### Error Handling When you are using this package, the package will throw a `Strictus\Exceptions\StrictusTypeException` if you pass it a type that is not compatible with the intended conditions diff --git a/src/Strictus.php b/src/Strictus.php index 2c1257d..7a4426e 100644 --- a/src/Strictus.php +++ b/src/Strictus.php @@ -76,7 +76,7 @@ public static function nullableFloat(mixed $float): StrictusFloat * @param bool $nullable * @return StrictusBoolean */ - public static function boolean(mixed $boolean, bool $nullable = false): StrictusBoolean + public static function bool(mixed $boolean, bool $nullable = false): StrictusBoolean { return new StrictusBoolean($boolean, $nullable); } @@ -85,7 +85,7 @@ public static function boolean(mixed $boolean, bool $nullable = false): Strictus * @param mixed $boolean * @return StrictusBoolean */ - public static function nullableBoolean(mixed $boolean): StrictusBoolean + public static function nullableBool(mixed $boolean): StrictusBoolean { return new StrictusBoolean($boolean, true); } @@ -129,21 +129,23 @@ public static function nullableObject(mixed $object): StrictusObject } /** + * @param string $instanceType * @param mixed $instance * @param bool $nullable * @return StrictusInstance */ - public static function instance(mixed $instance, bool $nullable = false): StrictusInstance + public static function instance(string $instanceType, mixed $instance, bool $nullable = false): StrictusInstance { - return new StrictusInstance($instance, $nullable); + return new StrictusInstance($instanceType, $instance, $nullable); } /** + * @param string $instanceType * @param mixed $instance * @return StrictusInstance */ - public static function nullableInstance(mixed $instance): StrictusInstance + public static function nullableInstance(string $instanceType, mixed $instance): StrictusInstance { - return new StrictusInstance($instance, true); + return new StrictusInstance($instanceType, $instance, true); } } diff --git a/src/Traits/StrictusTyping.php b/src/Traits/StrictusTyping.php index d2b45bc..25a6483 100644 --- a/src/Traits/StrictusTyping.php +++ b/src/Traits/StrictusTyping.php @@ -12,7 +12,18 @@ */ trait StrictusTyping { - private mixed $value; + /** + * @param mixed $value + * @param bool $nullable + */ + public function __construct(private mixed $value, private bool $nullable) + { + if ($this->nullable) { + $this->errorMessage .= ' Or Null'; + } + + $this->validate($value); + } /** * @param mixed $value @@ -24,15 +35,7 @@ public function __invoke(mixed $value = new StrictusUndefined()): mixed return $this->value; } - if ($value === null && !$this->nullable) { - throw new StrictusTypeException($this->errorMessage); - } - - if (gettype($value) !== $this->instanceType) { - if ($this->nullable && $value !== null) { - throw new StrictusTypeException($this->errorMessage); - } - } + $this->validate($value); $this->value = $value; @@ -59,24 +62,23 @@ public function __set(string $name, mixed $value): void return; } - if ( - gettype($value) !== $this->instanceType - || ($this->nullable && $value !== null) - ) { - throw new StrictusTypeException($this->errorMessage); - } + $this->validate($value); $this->value = $value; } - public function handleInstantiation(mixed $value) + /** + * @param mixed $value + * @return void + */ + private function validate(mixed $value): void { - if (gettype($value) !== $this->instanceType) { - if (!$this->nullable) { - throw new StrictusTypeException($this->errorMessage); - } else if ($value !== null) { - throw new StrictusTypeException($this->errorMessage); - } + if ($value === null && ! $this->nullable) { + throw new StrictusTypeException($this->errorMessage); + } + + if (gettype($value) !== $this->instanceType && $value !== null) { + throw new StrictusTypeException($this->errorMessage); } } } diff --git a/src/Types/StrictusArray.php b/src/Types/StrictusArray.php index 0d39d39..2ddb414 100644 --- a/src/Types/StrictusArray.php +++ b/src/Types/StrictusArray.php @@ -17,17 +17,4 @@ final class StrictusArray implements StrictusTypeInterface private string $instanceType = 'array'; private string $errorMessage = 'Expected Array'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(private mixed $value, private bool $nullable) - { - if ($this->nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/src/Types/StrictusBoolean.php b/src/Types/StrictusBoolean.php index b5407dd..77757fa 100644 --- a/src/Types/StrictusBoolean.php +++ b/src/Types/StrictusBoolean.php @@ -17,17 +17,4 @@ final class StrictusBoolean implements StrictusTypeInterface private string $instanceType = 'boolean'; private string $errorMessage = 'Expected Boolean'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(private mixed $value, private bool $nullable) - { - if ($this->nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/src/Types/StrictusFloat.php b/src/Types/StrictusFloat.php index 2398462..3fc759e 100644 --- a/src/Types/StrictusFloat.php +++ b/src/Types/StrictusFloat.php @@ -18,17 +18,4 @@ final class StrictusFloat implements StrictusTypeInterface private string $instanceType = 'double'; private string $errorMessage = 'Expected Float'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(private mixed $value, private bool $nullable) - { - if ($nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/src/Types/StrictusInstance.php b/src/Types/StrictusInstance.php index e8e8572..bd80ede 100644 --- a/src/Types/StrictusInstance.php +++ b/src/Types/StrictusInstance.php @@ -6,75 +6,49 @@ use Strictus\Exceptions\StrictusTypeException; use Strictus\Interfaces\StrictusTypeInterface; +use Strictus\Traits\StrictusTyping; /** * @internal */ final class StrictusInstance implements StrictusTypeInterface { - private string $instanceType; + use StrictusTyping; private string $errorMessage; /** + * @param string $instanceType * @param mixed $value * @param bool $nullable */ - public function __construct(private mixed $value, private bool $nullable) + public function __construct(private string $instanceType, private mixed $value, private bool $nullable) { - $this->instanceType = $value::class; - $this->errorMessage = 'Expected Instance Of '.$this->value::class; + $this->errorMessage = 'Expected Instance Of '.$this->instanceType; if ($this->nullable) { $this->errorMessage .= ' Or Null'; } - } - - /** - * @param mixed $value - * @return mixed - */ - public function __invoke(mixed $value = new StrictusUndefined()): mixed - { - if (! $value instanceof StrictusUndefined) { - if (! $value instanceof $this->instanceType) { - if ($this->nullable && $value !== null) { - throw new StrictusTypeException($this->errorMessage); - } - } - - $this->value = $value; - - return $this; - } - return $this->value; + $this->validate($value); } /** - * @param string $value - * @return mixed - */ - public function __get(string $value): mixed - { - return $this->$value; - } - - /** - * @param string $name * @param mixed $value * @return void */ - public function __set(string $name, mixed $value): void + private function validate(mixed $value): void { - if ($name !== 'value') { - return; + if ($value === null && ! $this->nullable) { + throw new StrictusTypeException($this->errorMessage); } - if (! $value instanceof $this->instanceType) { + if ($value !== null && gettype($value) !== 'object') { throw new StrictusTypeException($this->errorMessage); } - $this->value = $value; + if ($value !== null && $value::class !== $this->instanceType) { + throw new StrictusTypeException($this->errorMessage); + } } } diff --git a/src/Types/StrictusInteger.php b/src/Types/StrictusInteger.php index a97e749..8062456 100644 --- a/src/Types/StrictusInteger.php +++ b/src/Types/StrictusInteger.php @@ -17,17 +17,4 @@ final class StrictusInteger implements StrictusTypeInterface private string $instanceType = 'integer'; private string $errorMessage = 'Expected Integer'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(private mixed $value, private bool $nullable) - { - if ($this->nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/src/Types/StrictusObject.php b/src/Types/StrictusObject.php index 7be5b01..639a6f1 100644 --- a/src/Types/StrictusObject.php +++ b/src/Types/StrictusObject.php @@ -17,17 +17,4 @@ final class StrictusObject implements StrictusTypeInterface private string $instanceType = 'object'; private string $errorMessage = 'Expected Object'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(mixed $value, private bool $nullable) - { - if ($this->nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/src/Types/StrictusString.php b/src/Types/StrictusString.php index 183a357..1d34691 100644 --- a/src/Types/StrictusString.php +++ b/src/Types/StrictusString.php @@ -17,17 +17,4 @@ final class StrictusString implements StrictusTypeInterface private string $instanceType = 'string'; private string $errorMessage = 'Expected String'; - - /** - * @param mixed $value - * @param bool $nullable - */ - public function __construct(private mixed $value, private bool $nullable) - { - if ($this->nullable) { - $this->errorMessage .= ' Or Null'; - } - - $this->handleInstantiation($value); - } } diff --git a/tests/Unit/ArrayTest.php b/tests/Unit/ArrayTest.php index 52be365..19b9ca3 100644 --- a/tests/Unit/ArrayTest.php +++ b/tests/Unit/ArrayTest.php @@ -4,52 +4,52 @@ use Strictus\Strictus; use Strictus\Types\StrictusArray; -it('instantiates an array variable', function () { - $myArray = Strictus::array([1, 2, 3]); - expect($myArray) +it('instantiates variable') + ->expect(fn () => Strictus::array([1, 2, 3])) + ->toBeInstanceOf(StrictusArray::class); + +it('instantiates a nullable variable', function () { + $value = Strictus::array(null, true); + expect($value) + ->toBeInstanceOf(StrictusArray::class); + + $value = Strictus::nullableArray(null); + expect($value) ->toBeInstanceOf(StrictusArray::class); }); -it('gets the correct array value', function () { - $myArray = Strictus::array([1, 2, 3]); +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::array(null)) + ->toThrow(StrictusTypeException::class); +}); - expect($myArray->value) - ->toEqualCanonicalizing([1, 2, 3]) - ->and($myArray()) - ->toEqualCanonicalizing([1, 2, 3]); +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::array('foo')) + ->toThrow(StrictusTypeException::class); }); -it('sets the correct array value', function () { - $myArray = Strictus::array([1, 2, 3]); +it('returns the value correctly', function () { + $value = Strictus::array([1, 2, 3]); - expect($myArray->value) + expect($value->value) ->toEqualCanonicalizing([1, 2, 3]) - ->and($myArray()) + ->and($value()) ->toEqualCanonicalizing([1, 2, 3]); - - $myArray->value = [4, 5, 6]; - expect($myArray->value)->toEqualCanonicalizing([4, 5, 6]); - - $myArray([7, 8, 9]); - expect($myArray())->toEqualCanonicalizing([7, 8, 9]); }); -it('instantiates a nullable array variable with array method', function () { - $myArray = Strictus::array(null, true); - expect($myArray) - ->toBeInstanceOf(StrictusArray::class); -}); +it('updates the value correctly', function () { + $value = Strictus::array([1, 2, 3]); -it('instantiates a nullable array variable with nullableArray method', function () { - $myArray = Strictus::nullableArray(null); - expect($myArray) - ->toBeInstanceOf(StrictusArray::class); -}); + expect($value->value) + ->toEqualCanonicalizing([1, 2, 3]) + ->and($value()) + ->toEqualCanonicalizing([1, 2, 3]); -it('throws exception when trying to instantiate an array as nullable with array method', function () { - expect(fn() => Strictus::array(null))->toThrow(StrictusTypeException::class); -}); + $value->value = [4, 5, 6]; + expect($value->value) + ->toEqualCanonicalizing([4, 5, 6]); -it('throws exception when trying to instantiate an array with wrong type', function () { - expect(fn() => Strictus::array('foo'))->toThrow(StrictusTypeException::class); + $value([7, 8, 9]); + expect($value()) + ->toEqualCanonicalizing([7, 8, 9]); }); diff --git a/tests/Unit/BooleanTest.php b/tests/Unit/BooleanTest.php index 216358b..15ed815 100644 --- a/tests/Unit/BooleanTest.php +++ b/tests/Unit/BooleanTest.php @@ -4,48 +4,52 @@ use Strictus\Strictus; use Strictus\Types\StrictusBoolean; -it('instantiates a boolean variable', function () { - $myArray = Strictus::boolean(true); - expect($myArray) - ->toBeInstanceOf(StrictusBoolean::class); -}); +it('instantiates variable') + ->expect(fn () => Strictus::bool(true)) + ->toBeInstanceOf(StrictusBoolean::class); -it('instantiates a nullable boolean variable with boolean method', function () { - $myBoolean = Strictus::boolean(null, true); - expect($myBoolean) +it('instantiates a nullable variable', function () { + $value = Strictus::bool(null, true); + expect($value) ->toBeInstanceOf(StrictusBoolean::class); -}); -it('instantiates a nullable boolean variable with nullableBoolean method', function () { - $myBoolean = Strictus::nullableBoolean(null); - expect($myBoolean) + $value = Strictus::nullableBool(null); + expect($value) ->toBeInstanceOf(StrictusBoolean::class); }); -it('throws exception when trying to instantiate a boolean as nullable with boolean method', function () { - expect(fn () => Strictus::boolean(null))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::bool(null)) + ->toThrow(StrictusTypeException::class); }); -it('throws exception when trying to instantiate a boolean with wrong type', function () { - expect(fn () => Strictus::boolean('foo'))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::bool('foo')) + ->toThrow(StrictusTypeException::class); }); -it('returns value correctly', function () { - $myBoolean = Strictus::boolean(true); +it('returns the value correctly', function () { + $value = Strictus::bool(true); - expect($myBoolean())->toBeTrue(); - expect($myBoolean->value)->toBeTrue(); + expect($value->value) + ->toBeTrue() + ->and($value()) + ->toBeTrue(); }); -it('changes value correctly', function () { - $myBoolean = Strictus::boolean(true); +it('updates the value correctly', function () { + $value = Strictus::bool(true); - $myBoolean->value = false; + expect($value->value) + ->toBeTrue() + ->and($value()) + ->toBeTrue(); - expect($myBoolean())->toBeFalse()->and($myBoolean->value)->toBeFalse(); + $value->value = false; + expect($value->value) + ->toBeFalse(); - $myBoolean2 = Strictus::boolean(true); - $myBoolean2(false); - - expect($myBoolean2())->toBeFalse()->and($myBoolean2->value)->toBeFalse(); -}); \ No newline at end of file + $value(true); + expect($value()) + ->toBeTrue(); +}); diff --git a/tests/Unit/FloatTest.php b/tests/Unit/FloatTest.php index fa49324..f3fbef5 100644 --- a/tests/Unit/FloatTest.php +++ b/tests/Unit/FloatTest.php @@ -4,47 +4,52 @@ use Strictus\Strictus; use Strictus\Types\StrictusFloat; -it('instantiates a float variable', function () { - $myFloat = Strictus::float(3.14); - expect($myFloat) - ->toBeInstanceOf(StrictusFloat::class); -}); +it('instantiates variable') + ->expect(fn () => Strictus::float(10.5)) + ->toBeInstanceOf(StrictusFloat::class); -it('instantiates a nullable float variable with float method', function () { - $myFloat = Strictus::float(null, true); - expect($myFloat) +it('instantiates a nullable variable', function () { + $value = Strictus::float(null, true); + expect($value) ->toBeInstanceOf(StrictusFloat::class); -}); -it('instantiates a nullable boolean variable with nullableBoolean method', function () { - $myFloat = Strictus::nullableFloat(null); - expect($myFloat) + $value = Strictus::nullableFloat(null); + expect($value) ->toBeInstanceOf(StrictusFloat::class); }); -it('throws exception when trying to instantiate a boolean as nullable with boolean method', function () { - expect(fn () => Strictus::float(null))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::float(null)) + ->toThrow(StrictusTypeException::class); }); -it('throws exception when trying to instantiate a boolean with wrong type', function () { - expect(fn () => Strictus::float('foo'))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::float('foo')) + ->toThrow(StrictusTypeException::class); }); -it('returns value correctly', function () { - $myFloat = Strictus::float(3.14); +it('returns the value correctly', function () { + $value = Strictus::float(10.5); - expect($myFloat())->toEqual(3.14)->and($myFloat->value)->toEqual(3.14); + expect($value->value) + ->toBe(10.5) + ->and($value()) + ->toBe(10.5); }); -it('changes value correctly', function () { - $myFloat = Strictus::float(3.14); +it('updates the value correctly', function () { + $value = Strictus::float(10.5); - $myFloat->value = 1.12; + expect($value->value) + ->toBe(10.5) + ->and($value()) + ->toBe(10.5); - expect($myFloat())->toEqual(1.12)->and($myFloat->value)->toEqual(1.12); + $value->value = 5.1; + expect($value->value) + ->toBe(5.1); - $myFloat2 = Strictus::float(3.14); - $myFloat2(1.12); - - expect($myFloat2())->toEqual(1.12)->and($myFloat2->value)->toEqual(1.12); -}); \ No newline at end of file + $value(7.55); + expect($value()) + ->toBe(7.55); +}); diff --git a/tests/Unit/InstanceTest.php b/tests/Unit/InstanceTest.php index a814366..e6cc2e9 100644 --- a/tests/Unit/InstanceTest.php +++ b/tests/Unit/InstanceTest.php @@ -1 +1,69 @@ -expect(fn () => Strictus::instance(MyClass::class, new MyClass())) + ->toBeInstanceOf(StrictusInstance::class); + +it('instantiates a nullable variable', function () { + $value = Strictus::instance(MyClass::class, null, true); + expect($value) + ->toBeInstanceOf(StrictusInstance::class); + + $value = Strictus::nullableInstance(MyClass::class, null); + expect($value) + ->toBeInstanceOf(StrictusInstance::class); +}); + +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::instance(MyClass::class, null)) + ->toThrow(StrictusTypeException::class); +}); + +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::instance(MyClass::class, 'foo')) + ->toThrow(StrictusTypeException::class) + ->and(fn () => Strictus::instance(MyClass::class, new TestClass())) + ->toThrow(StrictusTypeException::class); +}); + +it('returns the value correctly', function () { + $value = Strictus::instance(MyClass::class, new MyClass()); + + expect($value->value) + ->toBeInstanceOf(MyClass::class) + ->and($value()) + ->toBeInstanceOf(MyClass::class); +}); + +it('updates the value correctly', function () { + $value = Strictus::instance(MyClass::class, new MyClass()); + + expect($value->value) + ->toBeInstanceOf(MyClass::class) + ->and($value()) + ->toBeInstanceOf(MyClass::class); + + $value->value = new MyClass(); + expect($value->value) + ->toBeInstanceOf(MyClass::class); + + $value(new MyClass()); + expect($value()) + ->toBeInstanceOf(MyClass::class); +}); + +class MyClass +{ +} + +class TestClass +{ +} + +class FooClass +{ +} diff --git a/tests/Unit/IntegerTest.php b/tests/Unit/IntegerTest.php index 170a001..0c45f7d 100644 --- a/tests/Unit/IntegerTest.php +++ b/tests/Unit/IntegerTest.php @@ -4,47 +4,52 @@ use Strictus\Strictus; use Strictus\Types\StrictusInteger; -it('instantiates a integer variable', function () { - $myInt = Strictus::int(3); - expect($myInt) - ->toBeInstanceOf(StrictusInteger::class); -}); +it('instantiates variable') + ->expect(fn () => Strictus::int(10)) + ->toBeInstanceOf(StrictusInteger::class); -it('instantiates a nullable integer variable with integer method', function () { - $myInt = Strictus::int(null, true); - expect($myInt) +it('instantiates a nullable variable', function () { + $value = Strictus::int(null, true); + expect($value) ->toBeInstanceOf(StrictusInteger::class); -}); -it('instantiates a nullable integer variable with nullableinteger method', function () { - $myInt = Strictus::nullableInt(null); - expect($myInt) + $value = Strictus::nullableInt(null); + expect($value) ->toBeInstanceOf(StrictusInteger::class); }); -it('throws exception when trying to instantiate a integer as nullable with integer method', function () { - expect(fn () => Strictus::int(null))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::int(null)) + ->toThrow(StrictusTypeException::class); }); -it('throws exception when trying to instantiate a integer with wrong type', function () { - expect(fn () => Strictus::int('foo'))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::int('foo')) + ->toThrow(StrictusTypeException::class); }); -it('returns value correctly', function () { - $myInt = Strictus::int(3); +it('returns the value correctly', function () { + $value = Strictus::int(10); - expect($myInt())->toEqual(3)->and($myInt->value)->toEqual(3); + expect($value->value) + ->toBe(10) + ->and($value()) + ->toBe(10); }); -it('changes value correctly', function () { - $myInt = Strictus::int(3); +it('updates the value correctly', function () { + $value = Strictus::int(10); - $myInt->value = 1; + expect($value->value) + ->toBe(10) + ->and($value()) + ->toBe(10); - expect($myInt())->toEqual(1)->and($myInt->value)->toEqual(1); + $value->value = 5; + expect($value->value) + ->toBe(5); - $myInt2 = Strictus::int(3); - $myInt2(1); - - expect($myInt2())->toEqual(1)->and($myInt2->value)->toEqual(1); -}); \ No newline at end of file + $value(7); + expect($value()) + ->toBe(7); +}); diff --git a/tests/Unit/ObjectTest.php b/tests/Unit/ObjectTest.php index a814366..7eedb67 100644 --- a/tests/Unit/ObjectTest.php +++ b/tests/Unit/ObjectTest.php @@ -1 +1,55 @@ -expect(fn () => Strictus::object((object) ['foo' => 'bar'])) + ->toBeInstanceOf(StrictusObject::class); + +it('instantiates a nullable variable', function () { + $value = Strictus::object(null, true); + expect($value) + ->toBeInstanceOf(StrictusObject::class); + + $value = Strictus::nullableObject(null); + expect($value) + ->toBeInstanceOf(StrictusObject::class); +}); + +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::object(null)) + ->toThrow(StrictusTypeException::class); +}); + +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::object('foo')) + ->toThrow(StrictusTypeException::class); +}); + +it('returns the value correctly', function () { + $value = Strictus::object((object) ['foo' => 'bar']); + + expect($value->value) + ->toEqual((object) ['foo' => 'bar']) + ->and($value()) + ->toEqual((object) ['foo' => 'bar']); +}); + +it('updates the value correctly', function () { + $value = Strictus::object((object) ['foo' => 'bar']); + + expect($value->value) + ->toEqual((object) ['foo' => 'bar']) + ->and($value()) + ->toEqual((object) ['foo' => 'bar']); + + $value->value = (object) ['test' => 'testing']; + expect($value->value) + ->toEqual((object) ['test' => 'testing']); + + $value((object) ['bar' => 'foo']); + expect($value()) + ->toEqual((object) ['bar' => 'foo']); +}); diff --git a/tests/Unit/StringTest.php b/tests/Unit/StringTest.php index 446fa61..2b5fd8d 100644 --- a/tests/Unit/StringTest.php +++ b/tests/Unit/StringTest.php @@ -4,47 +4,52 @@ use Strictus\Strictus; use Strictus\Types\StrictusString; -it('instantiates a string variable', function () { - $myInt = Strictus::string('hello'); - expect($myInt) - ->toBeInstanceOf(StrictusString::class); -}); +it('instantiates variable') + ->expect(fn () => Strictus::string('foo')) + ->toBeInstanceOf(StrictusString::class); -it('instantiates a nullable string variable with string method', function () { - $myInt = Strictus::string(null, true); - expect($myInt) +it('instantiates a nullable variable', function () { + $value = Strictus::string(null, true); + expect($value) ->toBeInstanceOf(StrictusString::class); -}); -it('instantiates a nullable string variable with nullableStringeger method', function () { - $myInt = Strictus::nullableString(null); - expect($myInt) + $value = Strictus::nullableString(null); + expect($value) ->toBeInstanceOf(StrictusString::class); }); -it('throws exception when trying to instantiate a string as nullable with string method', function () { - expect(fn () => Strictus::string(null))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate non-nullable variable with null', function () { + expect(fn () => Strictus::string(null)) + ->toThrow(StrictusTypeException::class); }); -it('throws exception when trying to instantiate a string with wrong type', function () { - expect(fn () => Strictus::string(3.14))->toThrow(StrictusTypeException::class); +it('throws exception when trying to instantiate variable with wrong type', function () { + expect(fn () => Strictus::string(10)) + ->toThrow(StrictusTypeException::class); }); -it('returns value correctly', function () { - $myInt = Strictus::string('hello'); +it('returns the value correctly', function () { + $value = Strictus::string('foo'); - expect($myInt())->toEqual('hello')->and($myInt->value)->toEqual('hello'); + expect($value->value) + ->toBe('foo') + ->and($value()) + ->toBe('foo'); }); -it('changes value correctly', function () { - $myInt = Strictus::string('hello'); +it('updates the value correctly', function () { + $value = Strictus::string('foo'); - $myInt->value = 'goodbye'; + expect($value->value) + ->toBe('foo') + ->and($value()) + ->toBe('foo'); - expect($myInt())->toEqual('goodbye')->and($myInt->value)->toEqual('goodbye'); + $value->value = 'bar'; + expect($value->value) + ->toBe('bar'); - $myInt2 = Strictus::string('hello'); - $myInt2('goodbye'); - - expect($myInt2())->toEqual('goodbye')->and($myInt2->value)->toEqual('goodbye'); -}); \ No newline at end of file + $value('test'); + expect($value()) + ->toBe('test'); +});