Skip to content

Commit 1f3ad86

Browse files
committed
update struct
1 parent c332bb9 commit 1f3ad86

File tree

8 files changed

+294
-26
lines changed

8 files changed

+294
-26
lines changed

.idea/php-datatypes.iml

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tests/StructTest.php

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
use Nejcc\PhpDatatypes\Composite\Struct\Struct;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class StructTest extends TestCase
7+
{
8+
public function testStructSetAndGet()
9+
{
10+
// Example 1
11+
$struct = new Struct([
12+
'name' => 'string',
13+
'age' => '?int',
14+
'balance' => 'float',
15+
]);
16+
17+
// Test setting and getting field values using set/get methods
18+
$struct->set('name', 'Nejc');
19+
$struct->set('age', null); // Nullable type
20+
$struct->set('balance', 100.50);
21+
22+
// Assertions
23+
$this->assertEquals('Nejc', $struct->get('name'));
24+
$this->assertNull($struct->get('age'));
25+
$this->assertEquals(100.50, $struct->get('balance'));
26+
}
27+
28+
public function testMagicMethods()
29+
{
30+
// Example 1 with magic methods
31+
$struct = new Struct([
32+
'name' => 'string',
33+
'age' => '?int',
34+
'balance' => 'float',
35+
]);
36+
37+
// Test setting and getting field values using magic methods
38+
$struct->name = 'John';
39+
$struct->age = null;
40+
$struct->balance = 200.75;
41+
42+
// Assertions
43+
$this->assertEquals('John', $struct->name);
44+
$this->assertNull($struct->age);
45+
$this->assertEquals(200.75, $struct->balance);
46+
}
47+
48+
public function testStructHelperFunction()
49+
{
50+
// Example 2: using the `struct()` helper function (assuming it is defined)
51+
$struct = struct([
52+
'name' => 'string',
53+
'age' => '?int',
54+
'balance' => 'float',
55+
]);
56+
57+
// Test setting and getting field values using set/get methods
58+
$struct->set('name', 'Test');
59+
$struct->set('age', null);
60+
$struct->set('balance', 100.50);
61+
62+
// Assertions
63+
$this->assertEquals('Test', $struct->get('name'));
64+
$this->assertNull($struct->get('age'));
65+
$this->assertEquals(100.50, $struct->get('balance'));
66+
}
67+
68+
public function testInvalidFieldThrowsException()
69+
{
70+
$struct = new Struct([
71+
'name' => 'string',
72+
]);
73+
74+
$this->expectException(InvalidArgumentException::class);
75+
$this->expectExceptionMessage("Field 'age' does not exist in the struct.");
76+
77+
$struct->set('age', 25); // This should throw an exception
78+
}
79+
80+
public function testInvalidTypeThrowsException()
81+
{
82+
$struct = new Struct([
83+
'name' => 'string',
84+
]);
85+
86+
$this->expectException(InvalidArgumentException::class);
87+
$this->expectExceptionMessage("Field 'name' expects type 'string', but got 'int'.");
88+
89+
$struct->set('name', 123); // Invalid type
90+
}
91+
}

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
},
3838
"scripts": {
3939
"test": "vendor/bin/phpunit",
40+
"test-box": "vendor/bin/phpunit --testdox",
4041
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
4142
},
4243
"config": {

composer.lock

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/struct.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
use Nejcc\PhpDatatypes\Composite\Struct\Struct;
4+
5+
include_once __DIR__ . '/../vendor/autoload.php';
6+
7+
8+
// example 1
9+
$struct = new Struct([
10+
'name' => 'string',
11+
'age' => '?int',
12+
'balance' => 'float',
13+
]);
14+
15+
$struct->set('name', 'Nejc'); // Sets name to 'Nejc'
16+
$struct->set('age', null); // Allows nullable int
17+
$struct->set('balance', 100.50); // Sets balance to 100.50
18+
19+
echo $struct->get('name'); // Outputs 'Nejc'
20+
echo $struct->get('age'); // Outputs null
21+
echo $struct->get('balance'); // Outputs 100.50
22+
23+
// Using magic methods
24+
$struct->name = 'John'; // Equivalent to $struct->set('name', 'John');
25+
echo $struct->name; // Equivalent to $struct->get('name');
26+
27+
var_dump($struct);
28+
29+
30+
//example 2
31+
32+
$struct2 = struct([
33+
'name' => 'string',
34+
'age' => '?int',
35+
'balance' => 'float',
36+
]);
37+
38+
$struct2->set('name', 'Test'); // Sets name to 'Nejc'
39+
$struct2->set('age', null); // Allows nullable int
40+
$struct2->set('balance', 100.50); // Sets balance to 100.50
41+
42+
echo $struct2->get('name'); // Outputs 'Nejc'
43+
echo $struct2->get('age'); // Outputs null
44+
echo $struct2->get('balance'); // Outputs 100.50
45+
46+
// Using magic methods
47+
$struct2->name = 'John'; // Equivalent to $struct2->set('name', 'John');
48+
echo $struct2->name; // Equivalent to $struct2->get('name');
49+
50+
var_dump($struct);

index.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ private function initStruct(): void
172172
*/
173173
$this->struct = struct([
174174
'name' => 'string',
175-
'age' => 'int',
175+
'age' => '?int',
176176
'balance' => 'float'
177177
]);
178178

179179
// Set initial values for struct fields.
180180
$this->struct->set('name', 'Nejc');
181-
$this->struct->set('age', 30);
181+
$this->struct->set('age', null);
182182
$this->struct->set('balance', 250.75);
183183
}
184184

phpunit.xml

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit bootstrap="vendor/autoload.php"
3-
colors="true"
4-
verbose="true">
3+
colors="true">
4+
55
<testsuites>
66
<testsuite name="Default">
7-
<directory>Tests/</directory>
7+
<directory>./Tests</directory>
88
</testsuite>
99
</testsuites>
10+
11+
<logging>
12+
<!-- Example of using JUnit logging in PHPUnit 11 -->
13+
<junit outputFile="build/logs/junit.xml" />
14+
</logging>
15+
1016
</phpunit>

0 commit comments

Comments
 (0)