Skip to content

Commit a53053c

Browse files
walmir-silvaWalmir Silva
andauthored
Refactor Test Structure: Add PHPUnit Tests for Interfaces with Strong Typing and Mocks (#9)
* add data structure contracts and tests * feat: Add and configure PHP quality and testing libraries - Added `friendsofphp/php-cs-fixer` for code style fixing. - Added `nunomaduro/phpinsights` for code quality and insights. - Updated `phpunit/phpunit` to version 10.5 for unit testing. - Added `squizlabs/php_codesniffer` for code style checking. - Added `mockery/mockery` for mocking objects in unit tests. - Added `enlightn/security-checker` for checking security vulnerabilities in dependencies. Configured these libraries to ensure code quality, consistency, and security. ### Changes Introduced 1. **Code Style Fixing**: - Added `friendsofphp/php-cs-fixer` (^3.58) to automatically fix PHP code to follow defined coding standards. 2. **Code Quality Insights**: - Added `nunomaduro/phpinsights` (^2.11) to provide code quality and performance insights. 3. **Unit Testing**: - Updated `phpunit/phpunit` to version ^10.5 to ensure robust and comprehensive testing of the codebase. 4. **Code Style Checking**: - Added `squizlabs/php_codesniffer` (^3.10) to detect violations of defined coding standards. 5. **Mocking in Unit Tests**: - Added `mockery/mockery` (^1.6) to provide an expressive and flexible API for mocking objects in unit tests. 6. **Security Vulnerability Checking**: - Added `enlightn/security-checker` (^2.0) to scan for known security vulnerabilities in project dependencies. ### Usage - **Code Style Fixing**: ```sh vendor/bin/php-cs-fixer fix ``` - **Code Quality Insights**: ```sh vendor/bin/phpinsights ``` - **Unit Testing**: ```sh vendor/bin/phpunit ``` - **Code Style Checking**: ```sh vendor/bin/phpcs ``` - **Mocking in Unit Tests**: - Use Mockery within your PHPUnit tests to create mocks and set expectations. - **Security Vulnerability Checking**: ```sh vendor/bin/security-checker security:check ``` ### Benefits - Ensures consistent code style across the project. - Provides actionable insights to improve code quality. - Enhances testing capabilities with updated PHPUnit and Mockery. - Ensures project dependencies are free from known security vulnerabilities. ### Notes - Ensure that the required binaries are executable in your environment. - Integrate these tools into your CI/CD pipeline for automated checks. * resolve style PSR * add Translate README to Brazilian Portuguese * update ci * Update composer.json to exclude unnecessary files and directories for package distribution * adjust lock composer * adjust lock composer * adjust .env.example * update .env.example * Add .phpcs-cache to .gitignore and remove from version control * update url repository in composer file * add git attributed file * remove file exclud reference * Adjust settings for composer * normalize the semantic syntax of methods and interface names * Refactor test structure: Add PHPUnit tests for interfaces with strong typing and mocks - Add tests for Comparable interface - Add tests for Countable interface - Add tests for Indexable interface - Add tests for Iterator interface - Add tests for Sortable interface - Add tests for Collection interface - Add tests for Serializable interface - Add tests for Heap interface - Add tests for IterableCollection interface - Add tests for Map interface - Add tests for Queue interface - Add tests for Stack interface - Add tests for Tree interface Organize test files into respective directories for Behavioral, DataStructure, Structural, and Structure --------- Co-authored-by: Walmir Silva <walmir.silva@growthdev.com.br>
1 parent ee6f13e commit a53053c

29 files changed

+714
-452
lines changed

composer.lock

Lines changed: 36 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
6+
7+
/**
8+
* Interface Comparable.
9+
*
10+
* Defines the contract for objects that can be compared.
11+
*
12+
* @category Interfaces
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Comparable
20+
{
21+
/**
22+
* Compares this object with the specified object for order.
23+
*
24+
* @param mixed $other The object to compare with
25+
*
26+
* @return int a negative integer, zero, or a positive integer as this object
27+
* is less than, equal to, or greater than the specified object
28+
*/
29+
public function compareTo(mixed $other): int;
30+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
6+
7+
/**
8+
* Interface Countable.
9+
*
10+
* Defines the contract for counting elements.
11+
*
12+
* @category Interfaces
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Countable
20+
{
21+
/**
22+
* Counts the elements.
23+
*
24+
* @return int The number of elements
25+
*/
26+
public function count(): int;
27+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
6+
7+
/**
8+
* Interface Indexable.
9+
*
10+
* Defines the contract for indexed access to elements.
11+
*
12+
* @category Interfaces
13+
*
14+
* @author Walmir Silva <walmir.silva@kariricode.org>
15+
* @license MIT
16+
*
17+
* @see https://kariricode.org/
18+
*/
19+
interface Indexable
20+
{
21+
/**
22+
* Checks if an element exists at the given index.
23+
*
24+
* @param int $index The index to check
25+
*
26+
* @return bool True if the index exists, false otherwise
27+
*/
28+
public function contains(int $index): bool;
29+
30+
/**
31+
* Gets the element at the specified index.
32+
*
33+
* @param int $index The index of the element
34+
*
35+
* @return mixed The element at the specified index
36+
*/
37+
public function get(int $index): mixed;
38+
39+
/**
40+
* Sets the element at the specified index.
41+
*
42+
* @param int $index The index at which to set the element
43+
* @param mixed $element The element to set
44+
*/
45+
public function set(int $index, mixed $element): void;
46+
47+
/**
48+
* Removes the element at the specified index.
49+
*
50+
* @param int $index The index of the element to remove
51+
*
52+
* @return bool True if the element was removed, false otherwise
53+
*/
54+
public function remove(int $index): bool;
55+
}

src/DataStructure/IterableCollection.php renamed to src/DataStructure/Behavioral/IterableCollection.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,25 @@
22

33
declare(strict_types=1);
44

5-
namespace KaririCode\Contract\DataStructure;
5+
namespace KaririCode\Contract\DataStructure\Behavioral;
66

77
/**
88
* Interface IterableCollection.
99
*
1010
* Provides the ability to iterate over a collection.
1111
*
1212
* @author Walmir Silva <walmir.silva@kariricode.org>
13+
* @author Walmir Silva <walmir.silva@kariricode.org>
1314
* @license MIT
1415
*
1516
* @see https://kariricode.org/
1617
*/
17-
interface IterableCollection extends \IteratorAggregate
18+
interface IterableCollection extends \Traversable
1819
{
1920
/**
2021
* Gets an iterator for the collection.
2122
*
22-
* @return \Traversable an iterator for the collection
23+
* @return Iterator an iterator for the collection
2324
*/
24-
public function getIterator(): \Traversable;
25+
public function getIterator(): Iterator;
2526
}

0 commit comments

Comments
 (0)