<?php

/**
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
 * @copyright Aimeos (aimeos.org), 2015-2025
 */


namespace Aimeos\MShop\Customer\Manager\Address;


class LaravelTest extends \PHPUnit\Framework\TestCase
{
	private $context;
	private $fixture;
	private $object;


	protected function setUp() : void
	{
		$this->context = \TestHelper::context();
		$manager = new \Aimeos\MShop\Customer\Manager\Laravel( $this->context );

		$this->fixture = array(
			'customer.address.parentid' => $manager->find( 'test@example.com' )->getId(),
			'customer.address.type' => 'delivery',
			'customer.address.company' => 'ABC GmbH',
			'customer.address.vatid' => 'DE999999999',
			'customer.address.salutation' => 'mr',
			'customer.address.title' => 'Herr',
			'customer.address.firstname' => 'firstunit',
			'customer.address.lastname' => 'lastunit',
			'customer.address.address1' => 'unit str.',
			'customer.address.address2' => ' 166',
			'customer.address.address3' => '4.OG',
			'customer.address.postal' => '22769',
			'customer.address.city' => 'Hamburg',
			'customer.address.state' => 'Hamburg',
			'customer.address.countryid' => 'de',
			'customer.address.languageid' => 'de',
			'customer.address.telephone' => '05554433221',
			'customer.address.telefax' => '05554433222',
			'customer.address.mobile' => '05554433223',
			'customer.address.email' => 'unittest@aimeos.org',
			'customer.address.website' => 'unittest.aimeos.org',
			'customer.address.longitude' => '10.0',
			'customer.address.latitude' => '50.0',
			'customer.address.position' => 1,
			'customer.address.latitude' => '2000-01-01',
			'customer.address.siteid' => $this->context->locale()->getSiteId(),
		);

		$this->object = new \Aimeos\MShop\Customer\Manager\Address\Laravel( $this->context );
	}


	protected function tearDown() : void
	{
		unset( $this->object, $this->fixture, $this->context );
	}


	public function testClear()
	{
		$this->assertInstanceOf( \Aimeos\MShop\Common\Manager\Iface::class, $this->object->clear( array( -1 ) ) );
	}


	public function testGetSearchAttributes()
	{
		foreach( $this->object->getSearchAttributes() as $attribute ) {
			$this->assertInstanceOf( '\\Aimeos\\Base\\Criteria\\Attribute\\Iface', $attribute );
		}
	}


	public function testGetSubManager()
	{
		$this->expectException( \LogicException::class );
		$this->object->getSubManager( 'unknown' );
	}


	public function testCreateItem()
	{
		$this->assertInstanceOf( \Aimeos\MShop\Customer\Item\Address\Iface::class, $this->object->create() );
	}


	public function testGetItem()
	{
		$search = $this->object->filter()->slice( 0, 1 );
		$search->setConditions( $search->compare( '~=', 'customer.address.company', 'Example company' ) );

		$item = $this->object->search( $search )->first( new \RuntimeException( 'No address item found' ) );

		$this->assertEquals( $item, $this->object->get( $item->getId() ) );
	}


	public function testSaveUpdateDeleteItem()
	{
		$item = new \Aimeos\MShop\Customer\Item\Address\Standard( 'customer.address.', $this->fixture );
		$item->setId( null );
		$resultSaved = $this->object->save( $item );
		$itemSaved = $this->object->get( $item->getId() );

		$itemExp = clone $itemSaved;
		$itemExp->setPosition( 1 );
		$itemExp->setCity( 'Berlin' );
		$itemExp->setState( 'Berlin' );
		$resultUpd = $this->object->save( $itemExp );
		$itemUpd = $this->object->get( $itemExp->getId() );

		$this->object->delete( $itemSaved->getId() );

		$this->assertTrue( $item->getId() !== null );
		$this->assertEquals( $item->getId(), $itemSaved->getId() );
		$this->assertEquals( $item->getParentId(), $itemSaved->getParentId() );
		$this->assertEquals( $item->getPosition(), $itemSaved->getPosition() );
		$this->assertEquals( $item->getType(), $itemSaved->getType() );
		$this->assertEquals( $item->getCompany(), $itemSaved->getCompany() );
		$this->assertEquals( $item->getVatID(), $itemSaved->getVatID() );
		$this->assertEquals( $item->getSalutation(), $itemSaved->getSalutation() );
		$this->assertEquals( $item->getTitle(), $itemSaved->getTitle() );
		$this->assertEquals( $item->getFirstname(), $itemSaved->getFirstname() );
		$this->assertEquals( $item->getLastname(), $itemSaved->getLastname() );
		$this->assertEquals( $item->getAddress1(), $itemSaved->getAddress1() );
		$this->assertEquals( $item->getAddress2(), $itemSaved->getAddress2() );
		$this->assertEquals( $item->getAddress3(), $itemSaved->getAddress3() );
		$this->assertEquals( $item->getPostal(), $itemSaved->getPostal() );
		$this->assertEquals( $item->getCity(), $itemSaved->getCity() );
		$this->assertEquals( $item->getState(), $itemSaved->getState() );
		$this->assertEquals( $item->getCountryId(), $itemSaved->getCountryId() );
		$this->assertEquals( $item->getLanguageId(), $itemSaved->getLanguageId() );
		$this->assertEquals( $item->getTelephone(), $itemSaved->getTelephone() );
		$this->assertEquals( $item->getMobile(), $itemSaved->getMobile() );
		$this->assertEquals( $item->getEmail(), $itemSaved->getEmail() );
		$this->assertEquals( $item->getTelefax(), $itemSaved->getTelefax() );
		$this->assertEquals( $item->getWebsite(), $itemSaved->getWebsite() );
		$this->assertEquals( $item->getLongitude(), $itemSaved->getLongitude() );
		$this->assertEquals( $item->getLatitude(), $itemSaved->getLatitude() );
		$this->assertEquals( $item->getBirthday(), $itemSaved->getBirthday() );

		$this->assertEquals( $this->context->editor(), $itemSaved->editor() );
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeCreated() );
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemSaved->getTimeModified() );

		$this->assertEquals( $itemExp->getId(), $itemUpd->getId() );
		$this->assertEquals( $itemExp->getParentId(), $itemUpd->getParentId() );
		$this->assertEquals( $itemExp->getType(), $itemUpd->getType() );
		$this->assertEquals( $itemExp->getPosition(), $itemUpd->getPosition() );
		$this->assertEquals( $itemExp->getCompany(), $itemUpd->getCompany() );
		$this->assertEquals( $itemExp->getVatID(), $itemUpd->getVatID() );
		$this->assertEquals( $itemExp->getSalutation(), $itemUpd->getSalutation() );
		$this->assertEquals( $itemExp->getTitle(), $itemUpd->getTitle() );
		$this->assertEquals( $itemExp->getFirstname(), $itemUpd->getFirstname() );
		$this->assertEquals( $itemExp->getLastname(), $itemUpd->getLastname() );
		$this->assertEquals( $itemExp->getAddress1(), $itemUpd->getAddress1() );
		$this->assertEquals( $itemExp->getAddress2(), $itemUpd->getAddress2() );
		$this->assertEquals( $itemExp->getAddress3(), $itemUpd->getAddress3() );
		$this->assertEquals( $itemExp->getPostal(), $itemUpd->getPostal() );
		$this->assertEquals( $itemExp->getCity(), $itemUpd->getCity() );
		$this->assertEquals( $itemExp->getState(), $itemUpd->getState() );
		$this->assertEquals( $itemExp->getCountryId(), $itemUpd->getCountryId() );
		$this->assertEquals( $itemExp->getLanguageId(), $itemUpd->getLanguageId() );
		$this->assertEquals( $itemExp->getTelephone(), $itemUpd->getTelephone() );
		$this->assertEquals( $itemExp->getMobile(), $itemUpd->getMobile() );
		$this->assertEquals( $itemExp->getEmail(), $itemUpd->getEmail() );
		$this->assertEquals( $itemExp->getTelefax(), $itemUpd->getTelefax() );
		$this->assertEquals( $itemExp->getWebsite(), $itemUpd->getWebsite() );
		$this->assertEquals( $itemExp->getLongitude(), $itemUpd->getLongitude() );
		$this->assertEquals( $itemExp->getLatitude(), $itemUpd->getLatitude() );
		$this->assertEquals( $itemExp->getBirthday(), $itemUpd->getBirthday() );

		$this->assertEquals( $this->context->editor(), $itemUpd->editor() );
		$this->assertEquals( $itemExp->getTimeCreated(), $itemUpd->getTimeCreated() );
		$this->assertMatchesRegularExpression( '/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/', $itemUpd->getTimeModified() );

		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultSaved );
		$this->assertInstanceOf( \Aimeos\MShop\Common\Item\Iface::class, $resultUpd );

		$this->expectException( '\\Aimeos\\MShop\\Exception' );
		$this->object->get( $itemSaved->getId() );
	}


	public function testCreateSearch()
	{
		$this->assertInstanceOf( '\\Aimeos\\Base\\Criteria\\Iface', $this->object->filter() );
	}


	public function testSearch()
	{
		$search = $this->object->filter();

		$conditions = array(
			$search->compare( '!=', 'customer.address.id', null ),
			$search->compare( '!=', 'customer.address.parentid', null ),
			$search->compare( '==', 'customer.address.type', 'delivery' ),
			$search->compare( '==', 'customer.address.company', 'Example company' ),
			$search->compare( '==', 'customer.address.vatid', 'DE999999999' ),
			$search->compare( '==', 'customer.address.salutation', 'mr' ),
			$search->compare( '==', 'customer.address.title', 'Dr' ),
			$search->compare( '==', 'customer.address.firstname', 'Our' ),
			$search->compare( '==', 'customer.address.lastname', 'Unittest' ),
			$search->compare( '==', 'customer.address.address1', 'Pickhuben' ),
			$search->compare( '==', 'customer.address.address2', '2-4' ),
			$search->compare( '==', 'customer.address.address3', '' ),
			$search->compare( '==', 'customer.address.postal', '20457' ),
			$search->compare( '==', 'customer.address.city', 'Hamburg' ),
			$search->compare( '==', 'customer.address.state', 'Hamburg' ),
			$search->compare( '==', 'customer.address.countryid', 'DE' ),
			$search->compare( '==', 'customer.address.languageid', 'de' ),
			$search->compare( '==', 'customer.address.telephone', '055544332211' ),
			$search->compare( '==', 'customer.address.telefax', '055544332212' ),
			$search->compare( '==', 'customer.address.mobile', '055544332213' ),
			$search->compare( '==', 'customer.address.email', 'test@example.com' ),
			$search->compare( '==', 'customer.address.website', 'www.example.com' ),
			$search->compare( '==', 'customer.address.longitude', '10.0' ),
			$search->compare( '==', 'customer.address.latitude', '50.0' ),
			$search->compare( '==', 'customer.address.birthday', '2000-01-01' ),
			$search->compare( '>=', 'customer.address.mtime', '1970-01-01 00:00:00' ),
			$search->compare( '>=', 'customer.address.ctime', '1970-01-01 00:00:00' ),
			$search->compare( '!=', 'customer.address.editor', '' ),
		);
		$search->setConditions( $search->and( $conditions ) );
		$this->assertEquals( 1, count( $this->object->search( $search ) ) );
	}


	public function testSearchTotal()
	{
		$total = 0;
		$search = $this->object->filter();
		$search->add( $search->compare( '~=', 'customer.address.company', 'Example company' ) )->slice( 0, 2 );

		$results = $this->object->search( $search, [], $total );

		$this->assertEquals( 2, count( $results ) );
		$this->assertEquals( 3, $total );

		foreach( $results as $id => $item ) {
			$this->assertEquals( $id, $item->getId() );
		}
	}
}
