-
Notifications
You must be signed in to change notification settings - Fork 0
Add environment-aware connection methods to Address and Ports contracts. #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cfbd2a6
feat: Add environment-aware connection methods to Address and Ports c…
gustavofreze a4a2d8c
feat: Add getPortForConnection method to Ports contract
gustavofreze c88a5c9
feat: Implement getHostForConnection in Address class
gustavofreze 96e0b1d
docs: Add environment-aware connection section to README
gustavofreze e6f4322
feat: Implement getPortForConnection in Ports class
gustavofreze 566dd82
feat: Implement getPortForConnection in Ports class
gustavofreze f4ef6d4
test: Add file_exists override for inside Docker simulation
gustavofreze 7df9fd2
test: Add AddressTest for getHostForConnection method
gustavofreze 543bc4c
test: Add PortsTest for getPortForConnection method
gustavofreze b0c4807
docs: Add use imports to README code examples
gustavofreze a95f623
docs: Update README for environment-aware methods in Address and Ports.
gustavofreze 32c7be6
docs: Update README for environment-aware methods in Address and Ports.
gustavofreze File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Unit\Internal\Containers\Address; | ||
|
|
||
| use PHPUnit\Framework\Attributes\RunInSeparateProcess; | ||
| use PHPUnit\Framework\TestCase; | ||
| use TinyBlocks\Collection\Collection; | ||
| use TinyBlocks\DockerContainer\Internal\Containers\Address\Address; | ||
| use TinyBlocks\DockerContainer\Internal\Containers\Address\Hostname; | ||
| use TinyBlocks\DockerContainer\Internal\Containers\Address\IP; | ||
| use TinyBlocks\DockerContainer\Internal\Containers\Address\Ports; | ||
|
|
||
| final class AddressTest extends TestCase | ||
| { | ||
| #[RunInSeparateProcess] | ||
| public function testGetHostForConnectionReturnsLocalhostWhenOutsideDocker(): void | ||
| { | ||
| require_once __DIR__ . '/../Overrides/file_exists_outside_docker.php'; | ||
|
|
||
| /** @Given an Address with a known hostname */ | ||
| $address = Address::from( | ||
| ip: IP::from(value: '172.17.0.2'), | ||
| ports: Ports::from( | ||
| exposedPorts: Collection::createFrom(elements: [3306]), | ||
| hostMappedPorts: Collection::createFrom(elements: [49153]) | ||
| ), | ||
| hostname: Hostname::from(value: 'my-container') | ||
| ); | ||
|
|
||
| /** @When getHostForConnection is called outside Docker */ | ||
| $host = $address->getHostForConnection(); | ||
|
|
||
| /** @Then it should return 127.0.0.1 */ | ||
| self::assertSame('127.0.0.1', $host); | ||
| } | ||
|
|
||
| #[RunInSeparateProcess] | ||
| public function testGetHostForConnectionReturnsHostnameWhenInsideDocker(): void | ||
| { | ||
| require_once __DIR__ . '/../Overrides/file_exists_inside_docker.php'; | ||
|
|
||
| /** @Given an Address with a known hostname */ | ||
| $address = Address::from( | ||
| ip: IP::from(value: '172.17.0.2'), | ||
| ports: Ports::from( | ||
| exposedPorts: Collection::createFrom(elements: [3306]), | ||
| hostMappedPorts: Collection::createFrom(elements: [49153]) | ||
| ), | ||
| hostname: Hostname::from(value: 'my-container') | ||
| ); | ||
|
|
||
| /** @When getHostForConnection is called inside Docker */ | ||
| $host = $address->getHostForConnection(); | ||
|
|
||
| /** @Then it should return the container hostname */ | ||
| self::assertSame('my-container', $host); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Test\Unit\Internal\Containers\Address; | ||
|
|
||
| use PHPUnit\Framework\Attributes\RunInSeparateProcess; | ||
| use PHPUnit\Framework\TestCase; | ||
| use TinyBlocks\Collection\Collection; | ||
| use TinyBlocks\DockerContainer\Internal\Containers\Address\Ports; | ||
|
|
||
| final class PortsTest extends TestCase | ||
| { | ||
| #[RunInSeparateProcess] | ||
| public function testGetPortForConnectionReturnsHostPortWhenOutsideDocker(): void | ||
| { | ||
| require_once __DIR__ . '/../Overrides/file_exists_outside_docker.php'; | ||
|
|
||
| /** @Given Ports with known exposed and host-mapped ports */ | ||
| $ports = Ports::from( | ||
| exposedPorts: Collection::createFrom(elements: [3306]), | ||
| hostMappedPorts: Collection::createFrom(elements: [49153]) | ||
| ); | ||
|
|
||
| /** @When getPortForConnection is called outside Docker */ | ||
| $port = $ports->getPortForConnection(); | ||
|
|
||
| /** @Then it should return the host-mapped port */ | ||
| self::assertSame(49153, $port); | ||
| } | ||
|
|
||
| #[RunInSeparateProcess] | ||
| public function testGetPortForConnectionReturnsExposedPortWhenInsideDocker(): void | ||
| { | ||
| require_once __DIR__ . '/../Overrides/file_exists_inside_docker.php'; | ||
|
|
||
| /** @Given Ports with known exposed and host-mapped ports */ | ||
| $ports = Ports::from( | ||
| exposedPorts: Collection::createFrom(elements: [3306]), | ||
| hostMappedPorts: Collection::createFrom(elements: [49153]) | ||
| ); | ||
|
|
||
| /** @When getPortForConnection is called inside Docker */ | ||
| $port = $ports->getPortForConnection(); | ||
|
|
||
| /** @Then it should return the container-internal exposed port */ | ||
| self::assertSame(3306, $port); | ||
| } | ||
| } |
10 changes: 10 additions & 0 deletions
10
tests/Unit/Internal/Containers/Overrides/file_exists_inside_docker.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace TinyBlocks\DockerContainer\Internal\Containers; | ||
|
|
||
| function file_exists(string $filename): bool | ||
| { | ||
| return true; | ||
| } | ||
|
gustavofreze marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.