BlackAndWhiteRasterImageTest.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. use Mike42\GfxPhp\BlackAndWhiteRasterImage;
  3. use PHPUnit\Framework\TestCase;
  4. /*
  5. * Many of these tests use an example image like this-
  6. *╭────╮
  7. *│████│ ██ = 1
  8. *│░░██│ ░░ = 0
  9. *╰────╯
  10. */
  11. class BlackAndWhiteRasterImageTest extends TestCase
  12. {
  13. protected function createBlackAndWhiteTestImage() {
  14. $image = BlackAndWhiteRasterImage::create(2, 2);
  15. $image -> setPixel(0, 0, 1);
  16. $image -> setPixel(1, 0, 1);
  17. $image -> setPixel(0, 1, 0);
  18. $image -> setPixel(1, 1, 1);
  19. return $image;
  20. }
  21. public function testCreate()
  22. {
  23. $img = $this -> createBlackAndWhiteTestImage();
  24. $this -> assertEquals("▀█\n", $img -> toString());
  25. }
  26. public function testInvert() {
  27. $img = $this -> createBlackAndWhiteTestImage();
  28. $img -> invert();
  29. $this -> assertEquals("▄ \n", $img -> toString());
  30. }
  31. public function testToRgb()
  32. {
  33. $img = $this -> createBlackAndWhiteTestImage() -> toRgb() -> toBlackAndWhite();
  34. $this -> assertEquals("▀█\n", $img -> toString());
  35. }
  36. public function testToIndexed()
  37. {
  38. $img = $this -> createBlackAndWhiteTestImage() -> toIndexed() -> toBlackAndWhite();
  39. $this -> assertEquals("▀█\n", $img -> toString());
  40. }
  41. public function testToGrayscale()
  42. {
  43. $img = $this -> createBlackAndWhiteTestImage() -> toGrayscale() -> toBlackAndWhite();
  44. $this -> assertEquals("▀█\n", $img -> toString());
  45. }
  46. public function testClear()
  47. {
  48. $img = $this -> createBlackAndWhiteTestImage();
  49. $img -> clear();
  50. $this -> assertEquals(" \n", $img -> toString());
  51. }
  52. public function testScale()
  53. {
  54. $img = $this -> createBlackAndWhiteTestImage() -> scale(4, 2);
  55. $this -> assertEquals("▀▀██\n", $img -> toString());
  56. }
  57. public function testRectEmpty()
  58. {
  59. $img = BlackAndWhiteRasterImage::create(5, 5);
  60. $img -> rect(1, 1, 3, 3);
  61. $expected = " ▄▄▄ \n" .
  62. " █▄█ \n" .
  63. " \n";
  64. $this -> assertEquals($expected, $img -> toString());
  65. }
  66. public function testSubImageEmpty()
  67. {
  68. $img = BlackAndWhiteRasterImage::create(5, 5);
  69. $img -> rect(1, 1, 3, 3);
  70. $img = $img -> subImage(1, 1, 3, 3);
  71. $expected = "█▀█\n" .
  72. "▀▀▀\n";
  73. $this -> assertEquals($expected, $img -> toString());
  74. }
  75. public function testRectFilled()
  76. {
  77. $img = BlackAndWhiteRasterImage::create(5, 5);
  78. $img -> rect(1, 1, 3, 3, true);
  79. $expected = " ▄▄▄ \n" .
  80. " ███ \n" .
  81. " \n";
  82. $this -> assertEquals($expected, $img -> toString());
  83. }
  84. }