Skip to content
Open
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
2 changes: 0 additions & 2 deletions .idea/PHPUnitPlayground.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions src/Sample4/AppController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Sample4;

/**
* Class AppController
* Runs the application
* @package FrankFleige\PHPUnitPlayground\Sample4
*/
class AppController
{

private $database;
private $config;
private $acrobat;

public function __construct()
{
$this->database = new Database();
$this->config = new ConfigurationRepository();
$this->acrobat = new StringAcrobat();
}

/**
* runs the app / runs the controller
*/
public function run()
{
// take this model, all you need
$model = new Model(
$this->config->get("host"),
$this->config->get("port"),
$this->generateUniqueId());

echo $model->getShortURL();
}

/**
* Generates an unique id
* @return string
*/
private function generateUniqueId()
{
$id = "";
do {
$id .= $this->acrobat->getRandomChar();
} while ($this->database->idExists($id));
return $id;
}
}
25 changes: 25 additions & 0 deletions src/Sample4/ConfigurationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Sample4;

/**
* Class ConfigurationRepository
* This simple configuration repository represents a dependency.
* It's possible used by ALL controllers and models in the project
* In this example we try to give the needed configuration to the model.
* @package FrankFleige\PHPUnitPlayground\Sample4
*/
class ConfigurationRepository
{
public function get(string $key)
{
switch ($key) {
case "host":
return "example.com";
case "port":
return "80";
default:
return null;
}
}
}
39 changes: 39 additions & 0 deletions src/Sample4/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Sample4;

/**
* Class Database
* This powerful database class can execute complex queries
* @package FrankFleige\PHPUnitPlayground\Sample4
*/
class Database
{

/**
* We selecting the hole database and looking for the given id
* @param string $id
* @return bool
*/
public function idExists(string $id)
{
if(strlen($id) <= 2) {
// all short ids are already assigned :-(
return true;
}

if(strlen($id) >= 12) {
// all long ids are unassigned ;-)
return false;
}

if(time()%2 === 0) {
// after a hard calculation within the database we are sure: this id already exists
return true;
}

// id was not found in the database
return false;
}

}
35 changes: 35 additions & 0 deletions src/Sample4/Model.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Sample4;

/**
* Class Model
* This model computes really hard stuff
* @package FrankFleige\PHPUnitPlayground\Sample4
*/
class Model
{
private $protocol = "http";
private $host;
private $port;
private $id;

public function __construct(string $host, string $port, string $id)
{
$this->host = $host;
$this->port = $port;
$this->id = $id;
}

/**
* returns a short url
* @return string
*/
public function getShortURL()
{
return "{$this->protocol}://{$this->host}:{$this->port}/{$this->id}";
}



}
22 changes: 22 additions & 0 deletions src/Sample4/StringAcrobat.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Sample4;

/**
* Class StringAcrobat
* The string acrobat is a helper class (model)
* @package FrankFleige\PHPUnitPlayground\Sample4
*/
class StringAcrobat
{
/**
* Returns a random char
* @return string
*/
public function getRandomChar()
{
$alphabet = array_merge(range("0","9"), range("a", "z"), range("A", "Z"));
shuffle($alphabet);
return $alphabet[0];
}
}
6 changes: 6 additions & 0 deletions src/Sample4/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

require dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . "vendor" . DIRECTORY_SEPARATOR . "autoload.php";

$app = new \FrankFleige\PHPUnitPlayground\Sample4\AppController();
$app->run();
15 changes: 15 additions & 0 deletions test/TestSample4/ModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Test\TestSample4;

use FrankFleige\PHPUnitPlayground\Sample4\Model;
use PHPUnit\Framework\TestCase;

class ModelTest extends TestCase
{
public function testGetShortUrl()
{
$m = new Model("test.de", "8080", "1234567890");
$this->assertEquals("http://test.de:8080/1234567890", $m->getShortURL());
}
}
21 changes: 21 additions & 0 deletions test/TestSample4/StringAcrobatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FrankFleige\PHPUnitPlayground\Test\TestSample4;

use FrankFleige\PHPUnitPlayground\Sample4\StringAcrobat;
use PHPUnit\Framework\TestCase;

class StringAcrobatTest extends TestCase
{

public function testGetRandomChar()
{
$sa = new StringAcrobat();
$this->assertEquals(1, strlen($sa->getRandomChar()));
$this->assertTrue(is_string($sa->getRandomChar()));
$alphabetCorrect = range("!", "z");
$this->assertTrue(in_array($sa->getRandomChar(), $alphabetCorrect));
$alphabetWrong = ["ä", "ö", "ü", "ß", "Ä", "Ü", "Ö"];
$this->assertFalse(in_array($sa->getRandomChar(), $alphabetWrong));
}
}