<?php

/**
 * @copyright Bluz PHP Team
 * @link      https://github.com/bluzphp/framework
 */

namespace Bluz\Tests\Db;

use Bluz;
use Bluz\Db\Exception\RelationNotFoundException;
use Bluz\Db\Exception\TableNotFoundException;
use Bluz\Db\Row;
use Bluz\Db\Table;
use Bluz\Tests\Fixtures\Db;
use Bluz\Tests\FrameworkTestCase;

/**
 * Test class for Row.
 * Generated by PHPUnit on 2011-07-27 at 13:52:01.
 */
class RowTest extends FrameworkTestCase
{

    /**
     * @var Db\ConcreteRow
     */
    protected $row;

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp(): void
    {
        self::getApp();
        $this->row = new Db\ConcreteRow();
    }

    /**
     * @covers \Bluz\Db\Row::__get
     */
    public function testGet()
    {
        self::assertNull($this->row->someValue);
    }

    /**
     * @covers \Bluz\Db\Row::__set
     */
    public function testSet()
    {
        $this->row->someValue = 'foo';
        self::assertEquals('foo', $this->row->someValue);
    }

    /**
     * @covers \Bluz\Db\Row::__isset
     */
    public function testIsset()
    {
        $this->row->someValue = 'foo';

        self::assertTrue(isset($this->row->someValue));
        self::assertFalse(isset($this->row->anotherValue));
    }

    /**
     * @todo Implement testSave().
     */
    public function testSave()
    {
        // Remove the following lines when you implement this test.
        self::markTestIncomplete('This test has not been implemented yet.');
    }

    /**
     * @todo Implement testDelete().
     */
    public function testDelete()
    {
        // Remove the following lines when you implement this test.
        self::markTestIncomplete('This test has not been implemented yet.');
    }

    /**
     * @todo Implement testRefresh().
     */
    public function testRefresh()
    {
        // Remove the following lines when you implement this test.
        self::markTestIncomplete('This test has not been implemented yet.');
    }

    /**
     * @covers \Bluz\Db\Row::getTable
     */
    public function testGetTable()
    {
        $table = $this->row->getTable();
        self::assertInstanceOf(Table::class, $table);
    }

    /**
     * @covers \Bluz\Db\Row::getTable
     */
    public function testGetTableException()
    {
        $this->expectException(TableNotFoundException::class);
        $this->row = new Db\ConcreteRowWithInvalidTable();
        $this->row->getTable();
    }

    /**
     * @todo Implement testGetRelation().
     */
    public function testGetRelation()
    {
        // Remove the following lines when you implement this test.
        self::markTestIncomplete('This test has not been implemented yet.');
    }

    public function testGetRelationException()
    {
        $this->expectException(RelationNotFoundException::class);
        $this->row->getRelation('wrongRelation');
    }

    /**
     * @todo Implement testSetRelation().
     */
    public function testSetRelation()
    {
        // Remove the following lines when you implement this test.
        self::markTestIncomplete('This test has not been implemented yet.');
    }

    /**
     * Test export to array
     */
    public function testToArray()
    {
        $this->row->someValue = 'foo';
        self::assertEqualsArray(['someValue' => 'foo'], $this->row->toArray());
    }

    /**
     * Test import from array
     */
    public function testSetFromArray()
    {
        $this->row->setFromArray(['someValue' => 'foo']);
        self::assertEquals('foo', $this->row->someValue);
    }

    /**
     * Test ArrayAccess interface
     *  - offsetSet
     */
    public function testOffsetSet()
    {
        $this->row['someValue'] = 'foo';
        self::assertEquals('foo', $this->row->someValue);
    }

    /**
     * Test ArrayAccess interface
     *  - offsetExists
     */
    public function testOffsetExists()
    {
        $this->row->someValue = 'foo';
        self::assertTrue(isset($this->row['someValue']));
    }

    /**
     * Test ArrayAccess interface
     *  - offsetUnset
     */
    public function testOffsetUnset()
    {
        $this->row->someValue = 'foo';
        unset($this->row['someValue']);
        self::assertFalse(isset($this->row['someValue']));
    }

    /**
     * Test ArrayAccess interface
     *  - offsetGet
     */
    public function testOffsetGet()
    {
        $this->row->someValue = 'foo';
        self::assertEquals('foo', $this->row['someValue']);
    }
}
