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
118 changes: 118 additions & 0 deletions Tests/DictionaryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

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

class DictionaryTest extends TestCase
{
public function testCanInitializeWithElements()
{
$dictionary = new Dictionary([
'name' => 'John Doe',
'age' => 30,
]);

$this->assertEquals('John Doe', $dictionary->get('name'));
$this->assertEquals(30, $dictionary->get('age'));
}

public function testAddAndGetElements()
{
$dictionary = new Dictionary();
$dictionary->add('name', 'Alice');
$dictionary->add('age', 25);

$this->assertEquals('Alice', $dictionary->get('name'));
$this->assertEquals(25, $dictionary->get('age'));
}

public function testContainsKey()
{
$dictionary = new Dictionary([
'name' => 'Bob',
]);

$this->assertTrue($dictionary->containsKey('name'));
$this->assertFalse($dictionary->containsKey('age'));
}

public function testRemoveElement()
{
$dictionary = new Dictionary([
'name' => 'Charlie',
'age' => 40,
]);

$dictionary->remove('age');
$this->assertFalse($dictionary->containsKey('age'));
$this->assertEquals('Charlie', $dictionary->get('name'));

$this->expectException(OutOfBoundsException::class);
$dictionary->get('age'); // This should throw an exception as 'age' was removed
}

public function testGetKeysAndValues()
{
$dictionary = new Dictionary([
'name' => 'David',
'age' => 35,
'country' => 'UK',
]);

$this->assertEquals(['name', 'age', 'country'], $dictionary->getKeys());
$this->assertEquals(['David', 35, 'UK'], $dictionary->getValues());
}

public function testDictionarySize()
{
$dictionary = new Dictionary([
'name' => 'Eve',
'city' => 'Berlin',
]);

$this->assertEquals(2, $dictionary->size());

$dictionary->add('age', 28);
$this->assertEquals(3, $dictionary->size());
}

public function testClearDictionary()
{
$dictionary = new Dictionary([
'name' => 'Frank',
'job' => 'Developer',
]);

$dictionary->clear();

$this->assertEquals(0, $dictionary->size());
$this->assertFalse($dictionary->containsKey('name'));
}

public function testInvalidKeyThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage("Dictionary keys must be non-integer (string) keys.");

// Attempt to initialize with an invalid key (integer key)
new Dictionary([1 => 'Invalid']);
}

public function testGetNonExistentKeyThrowsException()
{
$dictionary = new Dictionary(['name' => 'Grace']);
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage("Key 'age' does not exist in the dictionary.");

$dictionary->get('age'); // This should throw an exception
}

public function testRemoveNonExistentKeyThrowsException()
{
$dictionary = new Dictionary(['name' => 'Hank']);
$this->expectException(OutOfBoundsException::class);
$this->expectExceptionMessage("Key 'age' does not exist in the dictionary.");

$dictionary->remove('age'); // This should throw an exception
}
}
91 changes: 91 additions & 0 deletions Tests/ListDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php


use Nejcc\PhpDatatypes\Composite\ListData;
use PHPUnit\Framework\TestCase;
class ListDataTest extends TestCase
{
public function testCanInitializeWithElements()
{
$list = new ListData(['Element 1', 'Element 2']);

$this->assertEquals('Element 1', $list->get(0));
$this->assertEquals('Element 2', $list->get(1));
}

public function testAddElementsToList()
{
$list = new ListData();
$list->add('First element');
$list->add(42);

$this->assertEquals('First element', $list->get(0));
$this->assertEquals(42, $list->get(1));
}

public function testRemoveElementFromList()
{
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);

// Remove 'Element 2'
$list->remove(1);

// After removal, 'Element 3' should move to index 1
$this->assertEquals('Element 3', $list->get(1));

// Ensure 'Element 2' no longer exists and 'Element 3' is now at the correct index
$this->expectException(OutOfBoundsException::class);
$list->get(2); // Trying to access index 2 should now throw an exception because there is no element at that index
}


public function testContainsElement()
{
$list = new ListData(['First', 'Second', 'Third']);

$this->assertTrue($list->contains('First'));
$this->assertFalse($list->contains('Fourth'));
}

public function testGetAllElements()
{
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);

$this->assertEquals(['Element 1', 'Element 2', 'Element 3'], $list->getAll());
}

public function testGetListSize()
{
$list = new ListData(['Element 1', 'Element 2']);

$this->assertEquals(2, $list->size());

$list->add('Element 3');
$this->assertEquals(3, $list->size());
}

public function testClearList()
{
$list = new ListData(['Element 1', 'Element 2', 'Element 3']);
$list->clear();

$this->assertEquals(0, $list->size());
$this->assertEmpty($list->getAll());
}

public function testGetNonExistentElementThrowsException()
{
$list = new ListData(['Element 1']);

$this->expectException(OutOfBoundsException::class);
$list->get(5); // This should throw an exception because index 5 doesn't exist
}

public function testRemoveNonExistentElementThrowsException()
{
$list = new ListData(['Element 1', 'Element 2']);

$this->expectException(OutOfBoundsException::class);
$list->remove(10); // This should throw an exception because index 10 doesn't exist
}
}
33 changes: 33 additions & 0 deletions examples/dictionary.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

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

use Nejcc\PhpDatatypes\Composite\Dictionary;

// Create a new dictionary
$dictionary = new Dictionary([
'name' => 'John Doe',
'age' => 30,
]);

// Add a new key-value pair
$dictionary->add('country', 'USA');

// Get a value by key
echo $dictionary->get('name'); // Output: John Doe

// Check if a key exists
if ($dictionary->containsKey('age')) {
echo $dictionary->get('age'); // Output: 30
}

// Remove a key
$dictionary->remove('country');

// Get the size of the dictionary
echo $dictionary->size(); // Output: 2
var_dump($dictionary);
// Clear the dictionary
$dictionary->clear();

var_dump($dictionary);
36 changes: 36 additions & 0 deletions examples/listdata.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

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

use Nejcc\PhpDatatypes\Composite\ListData;

$list = new ListData();

// Add elements to the list
$list->add('First element');
$list->add(42);
$list->add(['nested', 'array']);

// Retrieve an element by index
echo $list->get(0); // Output: First element

// Remove an element by index
$list->remove(1);

// Check if the list contains a specific element
if ($list->contains('First element')) {
echo "Element exists!";
}

// Get the entire list
print_r($list->getAll()); // Output: Array containing remaining elements

// Get the size of the list
echo $list->size(); // Output: 2
var_dump($list);

// Clear the list
$list->clear();
echo $list->size(); // Output: 0 (empty list)

var_dump($list);
Loading
Loading