<?php
/**
 * SeekQuarry/Yioop --
 * Open Source Pure PHP Search Engine, Crawler, and Indexer
 *
 * Copyright (C) 2009 - 2026  Chris Pollett chris@pollett.org
 *
 * LICENSE:
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 * END LICENSE
 *
 * @author Chris Pollett chris@pollett.org
 * @license https://www.gnu.org/licenses/ GPL3
 * @link https://www.seekquarry.com/
 * @copyright 2009 - 2026
 * @filesource
 */
namespace seekquarry\yioop\tests;

use seekquarry\yioop\library\processors\TextProcessor;
use seekquarry\yioop\library\UnitTest;

/**
 * Checks the small pictures a text processor makes, which are also what
 * the document and book processors fall back to and what they scale their
 * pictures with, being its children: a picture scaled down keeping its
 * shape, something that is not a picture refused, and words drawn on a
 * page when there is no picture to be had.
 */
class TextProcessorTest extends UnitTest
{
    /**
     * Where the test set-up writes its files.
     * @var string
     */
    public $where = "";
    /**
     * Makes a folder for the files a case writes.
     */
    public function setUp()
    {
        $this->where = sys_get_temp_dir() . "/thumb" . getmypid();
        if (!is_dir($this->where)) {
            mkdir($this->where);
        }
    }
    /**
     * Removes the folder and everything a case wrote into it.
     */
    public function tearDown()
    {
        foreach (glob($this->where . "/*") as $written) {
            @unlink($written);
        }
        @rmdir($this->where);
    }
    /**
     * Makes a picture of a given size to test with.
     *
     * @param int $width how wide
     * @param int $height how tall
     * @return string the picture's bytes as a jpeg
     */
    public function aPicture($width, $height)
    {
        $made = imagecreatetruecolor($width, $height);
        imagefill($made, 0, 0, imagecolorallocate($made, 30, 90, 200));
        ob_start();
        imagejpeg($made);
        $bytes = ob_get_clean();
        return $bytes;
    }
    /**
     * A picture is scaled to fit inside the wanted size and keeps its
     * shape, and one that is already small enough is not stretched.
     */
    public function pictureIsScaledKeepingItsShapeTestCase()
    {
        $path = $this->where . "/scaled.webp";
        $this->assertTrue(TextProcessor::scaleTo($this->aPicture(800, 600),
            $path, 256, 256), "a picture is scaled");
        $size = getimagesize($path);
        $this->assertEqual($size[0], 256, "it fits the width asked for");
        $this->assertEqual($size[1], 192, "and keeps its shape");
        $small = $this->where . "/small.webp";
        TextProcessor::scaleTo($this->aPicture(40, 30), $small, 256, 256);
        $size = getimagesize($small);
        $this->assertEqual($size[0], 40,
            "one already small enough is left as it is");
    }
    /**
     * Something that is not a picture at all is refused rather than
     * written as a broken file.
     */
    public function whatIsNotAPictureIsRefusedTestCase()
    {
        $path = $this->where . "/nothing.webp";
        $this->assertTrue(!TextProcessor::scaleTo("not a picture at all",
            $path, 256, 256), "text handed in as a picture is refused");
        $this->assertTrue(!file_exists($path), "and nothing is written");
    }
    /**
     * Words are drawn on a page of the size asked for, whatever their
     * length, and a document with nothing to say still gives a page.
     */
    public function wordsAreDrawnOnAPageTestCase()
    {
        $path = $this->where . "/words.webp";
        $this->assertTrue(TextProcessor::drawText(
            str_repeat("The quick brown fox jumps over the lazy dog. ", 30),
            $path, 256, 256), "words are drawn");
        $size = getimagesize($path);
        $this->assertEqual($size[0], 256, "the page is the width asked for");
        $this->assertEqual($size[1], 256, "and the height asked for");
        $empty = $this->where . "/empty.webp";
        $this->assertTrue(TextProcessor::drawText("", $empty, 128, 128),
            "a document with nothing to say still gives a page");
    }
}
