-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: desenvolve classe para definiçao do ambiente
- Loading branch information
1 parent
d027c17
commit 8960baf
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
upload/system/library/PagSeguro/src/Domains/Environment.php
This file contains 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,65 @@ | ||
<?php | ||
|
||
namespace ValdeirPsr\PagSeguro\Domains; | ||
|
||
/** | ||
* Define o ambiente de desenvolvimento | ||
*/ | ||
class Environment | ||
{ | ||
private $isSandbox; | ||
private $email; | ||
private $token; | ||
|
||
/** | ||
* @param bool $isSandbox | ||
* @param string $email | ||
* @param string $token | ||
*/ | ||
private function __construct(bool $isSandbox, string $email, string $token) | ||
{ | ||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | ||
throw new \InvalidArgumentException("$email is invalid", 1000); | ||
} | ||
|
||
$this->isSandbox = $isSandbox; | ||
$this->email = $email; | ||
$this->token = $token; | ||
} | ||
|
||
/** | ||
* Define o ambiente como teste | ||
* | ||
* @param string $email | ||
* @param string $token | ||
* | ||
* @return self | ||
*/ | ||
public static function sandbox(string $email, string $token) | ||
{ | ||
return new self(true, $email, $token); | ||
} | ||
|
||
/** | ||
* Define o ambiente como produção | ||
* | ||
* @param string $email | ||
* @param string $token | ||
* | ||
* @return self | ||
*/ | ||
public static function production(string $email, string $token) | ||
{ | ||
return new self(false, $email, $token); | ||
} | ||
|
||
/** | ||
* Verifica se o ambiente está no modo de teste | ||
* | ||
* @return bool | ||
*/ | ||
public function isSandbox() | ||
{ | ||
return $this->isSandbox; | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
upload/system/library/PagSeguro/tests/unit/Domains/EnvironmentTest.php
This file contains 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,43 @@ | ||
<?php | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use ValdeirPsr\PagSeguro\Domains\Environment; | ||
|
||
class EnvironmentTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function newInstanceSandbox() | ||
{ | ||
$instance = Environment::sandbox('naval@sandbox.pagseguro.com.br', 'ABC123'); | ||
$this->assertInstanceOf(Environment::class, $instance); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function newInstanceProduction() | ||
{ | ||
$instance = Environment::production('naval@pagseguro.com.br', 'ABC123'); | ||
$this->assertInstanceOf(Environment::class, $instance); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function newInstanceSandboxWithInvalidArgumentShouldGiveError() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
Environment::sandbox('@sandbox.pagseguro.com.br', 'ABC123'); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function testIsSandbox() | ||
{ | ||
$instance = Environment::sandbox('naval@sandbox.pagseguro.com.br', 'ABC123'); | ||
$this->assertTrue($instance->isSandbox()); | ||
} | ||
} |