Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .idea/php-datatypes.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 91 additions & 0 deletions Tests/StructTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

use Nejcc\PhpDatatypes\Composite\Struct\Struct;
use PHPUnit\Framework\TestCase;

class StructTest extends TestCase
{
public function testStructSetAndGet()
{
// Example 1
$struct = new Struct([
'name' => 'string',
'age' => '?int',
'balance' => 'float',
]);

// Test setting and getting field values using set/get methods
$struct->set('name', 'Nejc');
$struct->set('age', null); // Nullable type
$struct->set('balance', 100.50);

// Assertions
$this->assertEquals('Nejc', $struct->get('name'));
$this->assertNull($struct->get('age'));
$this->assertEquals(100.50, $struct->get('balance'));
}

public function testMagicMethods()
{
// Example 1 with magic methods
$struct = new Struct([
'name' => 'string',
'age' => '?int',
'balance' => 'float',
]);

// Test setting and getting field values using magic methods
$struct->name = 'John';
$struct->age = null;
$struct->balance = 200.75;

// Assertions
$this->assertEquals('John', $struct->name);
$this->assertNull($struct->age);
$this->assertEquals(200.75, $struct->balance);
}

public function testStructHelperFunction()
{
// Example 2: using the `struct()` helper function (assuming it is defined)
$struct = struct([
'name' => 'string',
'age' => '?int',
'balance' => 'float',
]);

// Test setting and getting field values using set/get methods
$struct->set('name', 'Test');
$struct->set('age', null);
$struct->set('balance', 100.50);

// Assertions
$this->assertEquals('Test', $struct->get('name'));
$this->assertNull($struct->get('age'));
$this->assertEquals(100.50, $struct->get('balance'));
}

public function testInvalidFieldThrowsException()
{
$struct = new Struct([
'name' => 'string',
]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Field 'age' does not exist in the struct.");

$struct->set('age', 25); // This should throw an exception
}

public function testInvalidTypeThrowsException()
{
$struct = new Struct([
'name' => 'string',
]);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Field 'name' expects type 'string', but got 'int'.");

$struct->set('name', 123); // Invalid type
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
},
"scripts": {
"test": "vendor/bin/phpunit",
"test-box": "vendor/bin/phpunit --testdox",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"config": {
Expand Down
14 changes: 7 additions & 7 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions examples/struct.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

use Nejcc\PhpDatatypes\Composite\Struct\Struct;

include_once __DIR__ . '/../vendor/autoload.php';


// example 1
$struct = new Struct([
'name' => 'string',
'age' => '?int',
'balance' => 'float',
]);

$struct->set('name', 'Nejc'); // Sets name to 'Nejc'
$struct->set('age', null); // Allows nullable int
$struct->set('balance', 100.50); // Sets balance to 100.50

echo $struct->get('name'); // Outputs 'Nejc'
echo $struct->get('age'); // Outputs null
echo $struct->get('balance'); // Outputs 100.50

// Using magic methods
$struct->name = 'John'; // Equivalent to $struct->set('name', 'John');
echo $struct->name; // Equivalent to $struct->get('name');

var_dump($struct);


//example 2

$struct2 = struct([
'name' => 'string',
'age' => '?int',
'balance' => 'float',
]);

$struct2->set('name', 'Test'); // Sets name to 'Nejc'
$struct2->set('age', null); // Allows nullable int
$struct2->set('balance', 100.50); // Sets balance to 100.50

echo $struct2->get('name'); // Outputs 'Nejc'
echo $struct2->get('age'); // Outputs null
echo $struct2->get('balance'); // Outputs 100.50

// Using magic methods
$struct2->name = 'John'; // Equivalent to $struct2->set('name', 'John');
echo $struct2->name; // Equivalent to $struct2->get('name');

var_dump($struct);
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ private function initStruct(): void
*/
$this->struct = struct([
'name' => 'string',
'age' => 'int',
'age' => '?int',
'balance' => 'float'
]);

// Set initial values for struct fields.
$this->struct->set('name', 'Nejc');
$this->struct->set('age', 30);
$this->struct->set('age', null);
$this->struct->set('balance', 250.75);
}

Expand Down
12 changes: 9 additions & 3 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
colors="true"
verbose="true">
colors="true">

<testsuites>
<testsuite name="Default">
<directory>Tests/</directory>
<directory>./Tests</directory>
</testsuite>
</testsuites>

<logging>
<!-- Example of using JUnit logging in PHPUnit 11 -->
<junit outputFile="build/logs/junit.xml" />
</logging>

</phpunit>
Loading
Loading