Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Entity::setMetadata() method #181

Open
f3l1x opened this issue Jul 14, 2016 · 7 comments
Open

Add Entity::setMetadata() method #181

f3l1x opened this issue Jul 14, 2016 · 7 comments
Labels

Comments

@f3l1x
Copy link

f3l1x commented Jul 14, 2016

This is probably not an issue. I would like to test my components. They are very simple just for save entity let say. I've created mock of my facade and should receive a new fresh entity in save method, so this test is without access to database, no sql queries.

But I've got this message: Nextras\Orm\InvalidStateException: MetadataStorage::get() called too early. You have to instantiate your model first..

Here is component`s code.

public function processForm(SupplierForm $form)
{
        $values = $form->getValues();
        $entity = new Foo();
        // ....
        $this->facade->save($entity);
}

Any idea how to change concept? I think my architecture isn't bad I've just wanted to know the correct solution. I can't use EntityCreater it's not a test it's real component.

Thank you.

@JanTvrdik
Copy link
Member

The error means that you created instance of entity before creating instance of IModel. Create instance of IModel first.

@f3l1x
Copy link
Author

f3l1x commented Jul 16, 2016

@JanTvrdik I know, but it's little bit tricky if you want to just create entity and verify it was passed to your mock. Don't you think?

@hrach
Copy link
Member

hrach commented Sep 12, 2016

For now, you can mock IRepository & Metadata and call onAttach. But I will probably create a Metadata setter for better test usability.

@hrach hrach added the feature label Sep 12, 2016
@hrach hrach changed the title Entity - best practice in testing Add Entity::setMetadata() method Sep 12, 2016
@f3l1x
Copy link
Author

f3l1x commented Sep 17, 2016

Setter would be nice. I get it why it's in constructor, but we might think about better solution. Perhaps setter. :-)

@f3l1x
Copy link
Author

f3l1x commented May 8, 2017

Any updates here? It's really complicated to create simple unit tests..

@dakorpar
Copy link

dakorpar commented Jan 27, 2019

Just having same issue, any news regarding this?
I'm using phpunit and in this case now I've created method in my BaseTest:

 /**
 * @var array
 */
private $_data = [];


public function createEntityMock(string $classType): MockObject
{
	$entity = $this->getMockBuilder($classType)
		->disableOriginalConstructor()
		->getMock();

	$entity->method('__set')->willReturnCallback(function($name, $value) use ($classType) {
		$this->_data[$classType][$name] = $value;
	});

	$entity->method('__get')->willReturnCallback(function($name) use ($classType) {
		return $this->_data[$classType][$name];
	});

	return $entity;
}

@hrach
Copy link
Member

hrach commented Jan 29, 2019

As mentioned in #181 (comment) you may set the Metadata by onAttach. Actually not sure adding setMetadata would be good. It would pollute interface and API for common usage.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants