Skip to content

Commit

Permalink
feat: desenvolve classe para definiçao do ambiente
Browse files Browse the repository at this point in the history
  • Loading branch information
valdeir2000 committed Aug 8, 2020
1 parent d027c17 commit 8960baf
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
65 changes: 65 additions & 0 deletions upload/system/library/PagSeguro/src/Domains/Environment.php
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;
}
}
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());
}
}

0 comments on commit 8960baf

Please sign in to comment.