From cd5abdd25bd19d7c9303ee2b133af75b6a4cd75d Mon Sep 17 00:00:00 2001 From: Timo Huether Date: Mon, 17 Jul 2017 16:45:42 +0200 Subject: [PATCH] Sample4 initial commit --- .idea/PHPUnitPlayground.iml | 2 - src/Sample4/AppController.php | 50 +++++++++++++++++++++++++ src/Sample4/ConfigurationRepository.php | 25 +++++++++++++ src/Sample4/Database.php | 39 +++++++++++++++++++ src/Sample4/Model.php | 35 +++++++++++++++++ src/Sample4/StringAcrobat.php | 22 +++++++++++ src/Sample4/index.php | 6 +++ test/TestSample4/ModelTest.php | 15 ++++++++ test/TestSample4/StringAcrobatTest.php | 21 +++++++++++ 9 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 src/Sample4/AppController.php create mode 100644 src/Sample4/ConfigurationRepository.php create mode 100644 src/Sample4/Database.php create mode 100644 src/Sample4/Model.php create mode 100644 src/Sample4/StringAcrobat.php create mode 100644 src/Sample4/index.php create mode 100644 test/TestSample4/ModelTest.php create mode 100644 test/TestSample4/StringAcrobatTest.php diff --git a/.idea/PHPUnitPlayground.iml b/.idea/PHPUnitPlayground.iml index 3449561..a2c5002 100644 --- a/.idea/PHPUnitPlayground.iml +++ b/.idea/PHPUnitPlayground.iml @@ -40,12 +40,10 @@ - - diff --git a/src/Sample4/AppController.php b/src/Sample4/AppController.php new file mode 100644 index 0000000..e81329d --- /dev/null +++ b/src/Sample4/AppController.php @@ -0,0 +1,50 @@ +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; + } +} \ No newline at end of file diff --git a/src/Sample4/ConfigurationRepository.php b/src/Sample4/ConfigurationRepository.php new file mode 100644 index 0000000..2db24b6 --- /dev/null +++ b/src/Sample4/ConfigurationRepository.php @@ -0,0 +1,25 @@ += 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; + } + +} \ No newline at end of file diff --git a/src/Sample4/Model.php b/src/Sample4/Model.php new file mode 100644 index 0000000..70460e3 --- /dev/null +++ b/src/Sample4/Model.php @@ -0,0 +1,35 @@ +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}"; + } + + + +} \ No newline at end of file diff --git a/src/Sample4/StringAcrobat.php b/src/Sample4/StringAcrobat.php new file mode 100644 index 0000000..70cf93c --- /dev/null +++ b/src/Sample4/StringAcrobat.php @@ -0,0 +1,22 @@ +run(); \ No newline at end of file diff --git a/test/TestSample4/ModelTest.php b/test/TestSample4/ModelTest.php new file mode 100644 index 0000000..9bb11a7 --- /dev/null +++ b/test/TestSample4/ModelTest.php @@ -0,0 +1,15 @@ +assertEquals("http://test.de:8080/1234567890", $m->getShortURL()); + } +} diff --git a/test/TestSample4/StringAcrobatTest.php b/test/TestSample4/StringAcrobatTest.php new file mode 100644 index 0000000..7a173dc --- /dev/null +++ b/test/TestSample4/StringAcrobatTest.php @@ -0,0 +1,21 @@ +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)); + } +}