From db3f7772a60bfe56d391f92614c75ca4d687f64c Mon Sep 17 00:00:00 2001 From: kplaczek Date: Fri, 18 May 2018 21:47:43 +0200 Subject: [PATCH] Added tests to vocabulary and ability to print by key press --- app/Paper/HtmlToPos.php | 47 +++++++++++++++---- app/Paper/Paper.php | 6 +-- app/Paper/Vocabulary.php | 88 +++++++++++++++++++++++++++++++++++ composer.json | 2 +- tests/Unit/VocabularyTest.php | 71 ++++++++++++++++++++++++++++ 5 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 app/Paper/Vocabulary.php create mode 100644 tests/Unit/VocabularyTest.php diff --git a/app/Paper/HtmlToPos.php b/app/Paper/HtmlToPos.php index 11d6d52..0b2de8e 100644 --- a/app/Paper/HtmlToPos.php +++ b/app/Paper/HtmlToPos.php @@ -12,16 +12,19 @@ class HtmlToPos return str_replace(['
', '
', '
'], "\n", $text); } - public function convert($html = '') + /** + * usuwa linijki które na początku mają ! albo # + * @param $posText + * @return string + */ + private function hideLines($posText): string { - $posText = $this->handleNewLine($html); - - /** - * usuwa linijki które na początku mają ! albo # - */ - $posText = preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText); + return preg_replace('/^\s*[!#].*?$[\r\n]?/m', '', $posText); + } + private function pickRandomElement($posText) + { /** * poniższy kod wybieraz tekstu fragmenty w podwójnych nawiasach kwadratowych, rozdziela je po przecinkach * następnie losuje element i podmienia go w miejsce oryginalnego fragmentu @@ -37,6 +40,11 @@ class HtmlToPos $pos = strpos($posText, $elements[0]); $posText = substr_replace($posText, $data[mt_rand(0, count($data) - 1)], $pos, strlen($elements[0])); } + return $posText; + } + + private function randomNumber($posText) + { /** * przeszukiwanie tekstu pod katem zawartości [ liczba, liczba2 ] i zamienianiu tego na randomową liczbę z przedziału @@ -44,7 +52,7 @@ class HtmlToPos * obojętnie czy ujemną czy nie */ $re = '/\[\s*(-?\d+)\s*\,\s*(-?\d+)\s*\]/'; - preg_match_all($re, $html, $matches, PREG_SET_ORDER, 0); + preg_match_all($re, $posText, $matches, PREG_SET_ORDER, 0); foreach ($matches as $match) { $pos = strpos($posText, $match[0]); $posText = substr_replace($posText, mt_rand($match[1], $match[2]), $pos, strlen($match[0])); @@ -52,4 +60,27 @@ class HtmlToPos return $posText; } + public function vocabularyWord($posText) + { + $voc = new Vocabulary(); + $randomWord = $voc->getRandomWord(); + $word = $voc->getWord($randomWord); + + $posText = str_replace('[vocabulary_word]', $word->word, $posText); + $posText = str_replace('[vocabulary_short]', $word->short, $posText); + $posText = str_replace('[vocabulary_long]', $word->long, $posText); + + return $posText; + } + + public function convert($html = '') + { + $posText = $this->handleNewLine($html); + $posText = $this->hideLines($posText); + $posText = $this->pickRandomElement($posText); + $posText = $this->randomNumber($posText); + $posText = $this->vocabularyWord($posText); + return $posText; + } + } \ No newline at end of file diff --git a/app/Paper/Paper.php b/app/Paper/Paper.php index 94d2587..10df14b 100644 --- a/app/Paper/Paper.php +++ b/app/Paper/Paper.php @@ -23,7 +23,7 @@ class Paper try { $this->connector = new FilePrintConnector("/dev/usb/lp0"); $this->printer = new Printer($this->connector); - }catch (Exception $e){ + } catch (Exception $e) { die($e->getMessage()); } } @@ -46,7 +46,7 @@ class Paper { if ($image) { if ($imageLocal) { - $img = EscposImage::load($this->imageDirectory . basename($image).'.png'); + $img = EscposImage::load($this->imageDirectory . basename($image) . '.png'); } else { //image not local so then remote image $extension = strtolower(pathinfo($image, PATHINFO_EXTENSION)); @@ -81,7 +81,7 @@ class Paper */ public function getIcons() { - $icons = ["address-card-o","anchor","archive-3","at","balance-scale","ban","bar-chart-o","barcode","battery-empty","battery-full","battery-half","battery-quarter","battery-three-quarters","bed","beer","bell-o","bell-slash-o","bicycle","birthday-cake","bolt","bomb","book","bug","building-o","bullhorn","bus","camera","car","chain","chat-2","check","cloud","code","coffee","cog","cutlery","dashboard","database","diamond","dollar","dribbble","envelope-o","envira","exclamation-triangle","female","file-text-o","film","fingerprint","fire-extinguisher","fire","flag-o","flask","floppy-o","folder-o","folder-open-o","frown-o","gamepad","gift","git","glass","graduation-cap","grav","group","hand-o-left","heart-o","home","lemon-o","lightbulb-o","list-alt","location-arrow","lock","male","map-1","map-marker","microchip","money","moon-o","music","paper-plane","paperclip","paw","pencil","phone","pie-chart","piggy-bank","plane","question-circle-o","rocket","search","ship","shopping-cart","smile-o","snowflake-o","steam","subway","success","support","thermometer-2","thumbs-o-down","thumbs-o-up","ticket","times","trash-o","tree","trophy","truck","umbrella","usd","warning","wifi","wpexplorer","wrench","youtube-play"]; + $icons = ["address-card-o", "anchor", "archive-3", "at", "balance-scale", "ban", "bar-chart-o", "barcode", "battery-empty", "battery-full", "battery-half", "battery-quarter", "battery-three-quarters", "bed", "beer", "bell-o", "bell-slash-o", "bicycle", "birthday-cake", "bolt", "bomb", "book", "bug", "building-o", "bullhorn", "bus", "camera", "car", "chain", "chat-2", "check", "cloud", "code", "coffee", "cog", "cutlery", "dashboard", "database", "diamond", "dollar", "dribbble", "envelope-o", "envira", "exclamation-triangle", "female", "file-text-o", "film", "fingerprint", "fire-extinguisher", "fire", "flag-o", "flask", "floppy-o", "folder-o", "folder-open-o", "frown-o", "gamepad", "gift", "git", "glass", "graduation-cap", "grav", "group", "hand-o-left", "heart-o", "home", "lemon-o", "lightbulb-o", "list-alt", "location-arrow", "lock", "male", "map-1", "map-marker", "microchip", "money", "moon-o", "music", "paper-plane", "paperclip", "paw", "pencil", "phone", "pie-chart", "piggy-bank", "plane", "question-circle-o", "rocket", "search", "ship", "shopping-cart", "smile-o", "snowflake-o", "steam", "subway", "success", "support", "thermometer-2", "thumbs-o-down", "thumbs-o-up", "ticket", "times", "trash-o", "tree", "trophy", "truck", "umbrella", "usd", "warning", "wifi", "wpexplorer", "wrench", "youtube-play"]; $iconsDatabase = []; diff --git a/app/Paper/Vocabulary.php b/app/Paper/Vocabulary.php new file mode 100644 index 0000000..4055626 --- /dev/null +++ b/app/Paper/Vocabulary.php @@ -0,0 +1,88 @@ +getWordUrl($word), true); + } + + private function getWordUrl($word) + { + $word = trim($word); + return self::URL . 'dictionary/definition.ajax?search=' . $word . '&lang=en'; + } + + private function getWordExamplesUrl($word, $maxResult = 10, $offset = 0) + { + $word = trim($word); + return sprintf('%sexamples.json?query=%s&maxResults=%d&startOffset=%d', self::EXAMPLES_URL, $word, $maxResult, $offset); + } + + public function getWord($word) + { + $document = $this->getWordData($word); + + + $wordData = new \stdClass(); + $wordPageSelector = $document->first('.wordPage'); + $wordData->lang = $wordPageSelector->attr('data-lang'); + $wordData->word = $wordPageSelector->attr('data-word'); + $wordData->next = $wordPageSelector->attr('data-next'); + $wordData->prev = $wordPageSelector->attr('data-prev'); + $wordData->short = trim($document->first('p.short')->text()); + $wordData->long = trim($document->first('p.long')->text()); + + $wordData->instances = []; + foreach ($document->find('.ordinal ') as $ordinal) { + $definition = []; + $definition['type'] = $ordinal->first('h3')->first('a')->attr('title'); + $definition['definition'] = trim($ordinal->first('//h3[@class="definition"][1]/text()[2]', Query::TYPE_XPATH)); + $definition['example'] = ($example = $ordinal->first('//div[@class="example"]', Query::TYPE_XPATH)) ? $example->text() : ''; + + foreach ($ordinal->find('.instances') as $instance) { + $in = []; + $in['type'] = str_replace(' ', '_', trim(strtolower($instance->first('dt::text()')), ': ')); + $in['words'] = $instance->find('dd a::text()'); + $in['definition'] = $instance->first('div.definition::text()'); + $definition[$in['type']] = $in; + } + + $wordData->instances[] = $definition; + } + $wordData->examples = $this->getExamples($word, 3); + + + return $wordData; + } + + private function getRandomWordUrl() + { + return self::URL . 'randomword.json'; + } + + public function getRandomWord() + { + $data = json_decode(file_get_contents($this->getRandomWordUrl())); + if ($data->status != 0) { + throw new Exception('error'); + } + return $data->result->word; + } + + public function getExamples($word, $maxResult = 10, $offset = 0) + { + return json_decode(file_get_contents($this->getWordExamplesUrl($word, $maxResult, $offset)), 1); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 9e908c5..32d3736 100755 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "license": "MIT", "type": "project", "require": { - "php": ">=5.6.4", + "php": ">=7.0.0", "imangazaliev/didom": "^1.9", "intervention/image": "^2.3", "laravel/framework": "5.4.*", diff --git a/tests/Unit/VocabularyTest.php b/tests/Unit/VocabularyTest.php new file mode 100644 index 0000000..11e9c07 --- /dev/null +++ b/tests/Unit/VocabularyTest.php @@ -0,0 +1,71 @@ +getWordData('random'); + $this->assertNotEmpty($data); + $element = $data->find('.wordPage[data-word="random"]'); + $this->assertCount(1, $element); + } + + public function testGetNonExistingWordData() + { + $nonExistingWord = time(); + $voc = new Vocabulary(); + $data = $voc->getWordData($nonExistingWord); + $this->assertNotEmpty($data); + $element = $data->find('.wordPage[data-word="random"]'); + $this->assertCount(0, $element); + + $elementNotFound = $data->find('.noresults[data-search="' . $nonExistingWord . '"]'); + $this->assertCount(1, $elementNotFound); + } + + public function testGetExistingWord() + { + $dummyWord = 'dummy'; + $vot = new Vocabulary(); + $word = $vot->getWord($dummyWord); + + $this->assertEquals('en', $word->lang); + $this->assertEquals('dummy', $word->word); + $this->assertEquals('dump', $word->next); + $this->assertEquals('dumbstruck', $word->prev); + $this->assertEquals('Have you ever seen an entertainer make a doll look like it’s talking? The entertainer is using a dummy — a doll made to look like a person.', $word->short); + $this->assertEquals("A dummy is a type of doll that looks like a person. Entertainers called ventriloquists can make dummies appear to talk. The automobile industry uses dummies in cars to study how safe cars are during a crash. A dummy can also be anything that looks real but doesn’t work: a fake. Actors in a play might use certain props that are dummies, such as a dummy laptop. Dummy is also an insult used to mean “an ignorant person.”", $word->long); + + } + + public function testGetRandomWord() + { + $vot = new Vocabulary(); + $word = $vot->getRandomWord(); + $this->assertNotNull($word); + } + + public function testGetRandomWordData() + { + $vot = new Vocabulary(); + $word = $vot->getRandomWord(); + $this->assertNotNull($word); + + $wordData = $vot->getWord($word); + + $this->assertEquals('en', $wordData->lang); + $this->assertNotEmpty($wordData->word); + $this->assertNotEmpty($wordData->next); + $this->assertNotEmpty($wordData->prev); + $this->assertNotEmpty($wordData->short); + $this->assertNotEmpty($wordData->long); + + } +}