<?php
/**
 * Nextcloud - passman
 *
 * @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

use \OCA\Passman\Db\File;

/**
 * @coversDefaultClass  \OCA\Passman\Db\File
 */
class FileTest extends PHPUnit_Framework_TestCase {
	CONST TEST_DATA = [
		'id'		=> 21,
		'guid'		=> 'FC148F1A-AA67-489E-ABF5-4D7AA525F067',
		'user_id'	=> 'WolFi',
		'mimetype'	=> 'text/x-arduino',
		'filename'	=> 'eyJpdiI6ImVPcEY5WWRlT0FHcjdiekZ3V0RXSnciLCJ2IjoxLCJpdGVyIjoxMDAwLCJrcyI6MjU2LCJ0cyI6NjQsIm1vZGUiOiJjY20iLCJhZGF0YSI6IiIsImNpcGhlciI6ImFlcyIsInNhbHQiOiJtdG5CT2ZOL3hlRSIsImN0IjoialJFSHJ3S3JpK2FhaDJ5bUxUWnJRaWtKVVZIL2x4SSJ9',
		'size'		=> 4509,
		'created'	=> 1475853609,
		'file_data'	=> '[encoded data, 10932 characters]',
	];

	/**
	 * @var File
	 */
	protected $file;

	/**
	 * @after
	 */
	public function setUp() {
		$this->file = File::fromRow(self::TEST_DATA);
	}

	/**
	 * @covers ::getter
	 * @covers ::__construct
	 * @covers ::fromRow
	 */
	public function testGetters() {
		$this->assertEquals(self::TEST_DATA['id'], $this->file->getId());
		$this->assertEquals(self::TEST_DATA['guid'], $this->file->getGuid());
		$this->assertEquals(self::TEST_DATA['user_id'], $this->file->getUserId());
		$this->assertEquals(self::TEST_DATA['mimetype'], $this->file->getMimetype());
		$this->assertEquals(self::TEST_DATA['filename'], $this->file->getFilename());
		$this->assertEquals(self::TEST_DATA['size'], $this->file->getSize());
		$this->assertEquals(self::TEST_DATA['created'], $this->file->getCreated());
		$this->assertEquals(self::TEST_DATA['file_data'], $this->file->getFileData());
	}

	/**
	 * @covers ::setter
	 */
	public function testSetters() {
		/**
		 * Only testing one setter since if it works all setters should work because php magic.
		 * please, if you override a setter implement it here.
		 */
		$this->file->setMimetype('text/json');
		$this->assertEquals('text/json', $this->file->getMimetype());
	}

	/**
	 * @covers ::jsonSerialize
	 */
	public function testJsonSerialize(){
		$expected_result = [
			'file_id' => self::TEST_DATA['id'],
			'filename' => self::TEST_DATA['filename'],
			'guid' => self::TEST_DATA['guid'],
			'size' => self::TEST_DATA['size'],
			'file_data' => self::TEST_DATA['file_data'],
			'created' => self::TEST_DATA['created'],
			'mimetype' => self::TEST_DATA['mimetype'],
		];

		$actual_data = $this->file->jsonSerialize();

		$this->assertEquals($expected_result, $actual_data);
	}
}