<commit_before><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase', 
    'img.io.MetaDataReader', 
    'img.io.SOFNSegment',
    'img.io.CommentSegment',
    'img.io.XMPSegment'
  );

  /**
   * TestCase for MetaDataReader class
   *
   * @see  xp://img.io.MetaDataReader
   */
  class MetaDataReaderTest extends TestCase {
    protected $fixture= NULL;

    /**
     * Sets up this unittest 
     *
     * @throws  unittest.PrerequisitesNotMetError
     */
    public function setUp() {
      $this->fixture= new MetaDataReader();
    }

    /**
     * Returns a file for a classloader resource
     *
     * @param   string name
     * @param   string sub default NULL subpackage
     * @return  io.File
     */
    protected function resourceAsFile($name, $sub= NULL) {
      $package= $this->getClass()->getPackage();
      $container= $sub ? $package->getPackage($sub) : $package;
      return $container->getResourceAsStream($name);
    }

    /**
     * Extract from file and return the instance
     *
     * @param   string name
     * @param   string sub default NULL subpackage
     * @return  lang.Generic the instance
     */
    protected function extractFromFile($name, $sub= NULL) {
      with ($f= $this->resourceAsFile($name, $sub)); {
        return $this->fixture->read($f->getInputStream(), $name);
      }
    }

    /**
     * Test invoking reader with this class file
     *
     */
    #[@test, @expect(class= 'img.ImagingException', withMessage= '/Could not find start of image/')]
    public function this_class_file() {
      $this->extractFromFile(basename(__FILE__));
    }

    /**
     * Test an empty file
     *
     */
    #[@test, @expect(class= 'img.ImagingException', withMessage= '/Could not find start of image/')]
    public function empty_file() {
      $this->extractFromFile('empty.jpg');
    }

    /**
     * Assertion helper
     * 
     * @param  string $type The expected type
     * @param  int $size The expected size
     * @param  var $value The value
     * @throws unittest.AssertionFailedError
     */
    protected function assertArrayOf($type, $size, $value) {
      $this->assertInstanceOf(new ArrayType($type), $value);
      $this->assertEquals($size, sizeof($value));
    }

    /**
     * Test allSegments() method
     *
     */
    #[@test]
    public function all_segments() {
      $this->assertArrayOf('img.io.Segment', 9, $this->extractFromFile('1x1.jpg')->allSegments());
    }

    /**
     * Test segmentsNamed() method
     *
     */
    #[@test]
    public function segments_named_dqt() {
      $this->assertArrayOf('img.io.Segment', 2, $this->extractFromFile('1x1.jpg')->segmentsNamed('DQT'));
    }

    /**
     * Test segmentsNamed() - the 1x1 JPEG has a "SOF0" segment providing width and height
     *
     */
    #[@test]
    public function segment_named_sof0() {
      $this->assertEquals(
        array(new SOFNSegment('SOF0', array('bits' => 8, 'height' => 1, 'width' => 1, 'channels' => 3))),
        $this->extractFromFile('1x1.jpg')->segmentsNamed('SOF0')
      );
    }

    /**
     * Test segmentsOf() - the 1x1 JPEG has a "SOF0" segment providing width and height
     *
     */
    #[@test]
    public function segment_of_sofn() {
      $this->assertEquals(
        array(new SOFNSegment('SOF0', array('bits' => 8, 'height' => 1, 'width' => 1, 'channels' => 3))),
        $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.SOFNSegment')
      );
    }

    /**
     * Test the 1x1 JPEG has a "COM" segment providing width and height
     *
     */
    #[@test]
    public function com_segment() {
      $this->assertEquals(
        array(new CommentSegment('COM', 'Created with GIMP')),
        $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.CommentSegment')
      );
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function xmp_segment() {
      $segments= $this->extractFromFile('xmp.jpg')->segmentsOf('img.io.XMPSegment');
      $this->assertArrayOf('img.io.XMPSegment', 1, $segments);
      $this->assertInstanceOf('xml.Node', this($segments[0]->document()->getElementsByTagName('dc:title'), 0));
    }

    /**
     * Test imageDimensions()
     *
     */
    #[@test]
    public function dimensions_of_1x1_image() {
      $this->assertEquals(array(1, 1), $this->extractFromFile('1x1.jpg')->imageDimensions());
    }

    /**
     * Test imageDimensions()
     *
     */
    #[@test]
    public function dimensions_of_xmp_image() {
      $this->assertEquals(array(640, 480), $this->extractFromFile('canon-ixus.jpg', 'exif_org')->imageDimensions());
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function exif_data_segments() {
      $this->assertArrayOf(
        'img.io.ExifSegment', 1, 
        $this->extractFromFile('exif-only.jpg')->segmentsOf('img.io.ExifSegment')
      );
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function iptc_data_segments() {
      $this->assertArrayOf(
        'img.io.IptcSegment', 1, 
        $this->extractFromFile('iptc-only.jpg')->segmentsOf('img.io.IptcSegment')
      );
    }

    /**<commit_msg>- Add tests when no EXIF / IPTC data is in file<commit_after><?php
/* This class is part of the XP framework
 *
 * $Id$ 
 */

  uses(
    'unittest.TestCase', 
    'img.io.MetaDataReader', 
    'img.io.SOFNSegment',
    'img.io.CommentSegment',
    'img.io.XMPSegment'
  );

  /**
   * TestCase for MetaDataReader class
   *
   * @see  xp://img.io.MetaDataReader
   */
  class MetaDataReaderTest extends TestCase {
    protected $fixture= NULL;

    /**
     * Sets up this unittest 
     *
     * @throws  unittest.PrerequisitesNotMetError
     */
    public function setUp() {
      $this->fixture= new MetaDataReader();
    }

    /**
     * Returns a file for a classloader resource
     *
     * @param   string name
     * @param   string sub default NULL subpackage
     * @return  io.File
     */
    protected function resourceAsFile($name, $sub= NULL) {
      $package= $this->getClass()->getPackage();
      $container= $sub ? $package->getPackage($sub) : $package;
      return $container->getResourceAsStream($name);
    }

    /**
     * Extract from file and return the instance
     *
     * @param   string name
     * @param   string sub default NULL subpackage
     * @return  lang.Generic the instance
     */
    protected function extractFromFile($name, $sub= NULL) {
      with ($f= $this->resourceAsFile($name, $sub)); {
        return $this->fixture->read($f->getInputStream(), $name);
      }
    }

    /**
     * Test invoking reader with this class file
     *
     */
    #[@test, @expect(class= 'img.ImagingException', withMessage= '/Could not find start of image/')]
    public function this_class_file() {
      $this->extractFromFile(basename(__FILE__));
    }

    /**
     * Test an empty file
     *
     */
    #[@test, @expect(class= 'img.ImagingException', withMessage= '/Could not find start of image/')]
    public function empty_file() {
      $this->extractFromFile('empty.jpg');
    }

    /**
     * Assertion helper
     * 
     * @param  string $type The expected type
     * @param  int $size The expected size
     * @param  var $value The value
     * @throws unittest.AssertionFailedError
     */
    protected function assertArrayOf($type, $size, $value) {
      $this->assertInstanceOf(new ArrayType($type), $value);
      $this->assertEquals($size, sizeof($value));
    }

    /**
     * Test allSegments() method
     *
     */
    #[@test]
    public function all_segments() {
      $this->assertArrayOf('img.io.Segment', 9, $this->extractFromFile('1x1.jpg')->allSegments());
    }

    /**
     * Test segmentsNamed() method
     *
     */
    #[@test]
    public function segments_named_dqt() {
      $this->assertArrayOf('img.io.Segment', 2, $this->extractFromFile('1x1.jpg')->segmentsNamed('DQT'));
    }

    /**
     * Test segmentsNamed() - the 1x1 JPEG has a "SOF0" segment providing width and height
     *
     */
    #[@test]
    public function segment_named_sof0() {
      $this->assertEquals(
        array(new SOFNSegment('SOF0', array('bits' => 8, 'height' => 1, 'width' => 1, 'channels' => 3))),
        $this->extractFromFile('1x1.jpg')->segmentsNamed('SOF0')
      );
    }

    /**
     * Test segmentsOf() - the 1x1 JPEG has a "SOF0" segment providing width and height
     *
     */
    #[@test]
    public function segment_of_sofn() {
      $this->assertEquals(
        array(new SOFNSegment('SOF0', array('bits' => 8, 'height' => 1, 'width' => 1, 'channels' => 3))),
        $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.SOFNSegment')
      );
    }

    /**
     * Test the 1x1 JPEG has a "COM" segment providing width and height
     *
     */
    #[@test]
    public function com_segment() {
      $this->assertEquals(
        array(new CommentSegment('COM', 'Created with GIMP')),
        $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.CommentSegment')
      );
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function xmp_segment() {
      $segments= $this->extractFromFile('xmp.jpg')->segmentsOf('img.io.XMPSegment');
      $this->assertArrayOf('img.io.XMPSegment', 1, $segments);
      $this->assertInstanceOf('xml.Node', this($segments[0]->document()->getElementsByTagName('dc:title'), 0));
    }

    /**
     * Test imageDimensions()
     *
     */
    #[@test]
    public function dimensions_of_1x1_image() {
      $this->assertEquals(array(1, 1), $this->extractFromFile('1x1.jpg')->imageDimensions());
    }

    /**
     * Test imageDimensions()
     *
     */
    #[@test]
    public function dimensions_of_xmp_image() {
      $this->assertEquals(array(640, 480), $this->extractFromFile('canon-ixus.jpg', 'exif_org')->imageDimensions());
    }

    /**
     * Test segmentsOf()
     *
     */
    #[@test]
    public function no_exif_data_segments_in_1x1() {
      $this->assertEquals(array(), $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.ExifSegment'));
    }

    /**
     * Test segmentsOf()
     *
     */
    #[@test]
    public function no_iptc_data_segments_in_1x1() {
      $this->assertEquals(array(), $this->extractFromFile('1x1.jpg')->segmentsOf('img.io.IptcSegment'));
    }

    /**
     * Test exifData()
     *
     */
    #[@test]
    public function no_exif_data_in_1x1() {
      $this->assertNull($this->extractFromFile('1x1.jpg')->exifData());
    }

    /**
     * Test iptcData()
     *
     */
    #[@test]
    public function no_iptc_data_in_1x1() {
      $this->assertNull($this->extractFromFile('1x1.jpg')->iptcData());
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function exif_data_segments() {
      $this->assertArrayOf(
        'img.io.ExifSegment', 1, 
        $this->extractFromFile('exif-only.jpg')->segmentsOf('img.io.ExifSegment')
      );
    }

    /**
     * Test a file with exif data
     *
     */
    #[@test]
    public function iptc_data_segments() {
      $this->assertArrayOf(
        'img.io.IptcSegment', 1, 
        $this->extractFromFile('iptc-only.jpg')->segmentsOf('img.io.IptcSegment')
      );
    }

    /**
{'fraction_non_alphanumeric': 0.1608441212981025, 'fraction_numerical': 0.010108175208370278, 'mean_word_length': 2.4790252930289944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 123, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4629686', 'n_tokens_mistral': 3757, 'n_tokens_neox': 3655, 'n_words': 720}