I have been tasked with writing a Unit Test for a method where I pass in a Node object, modify an Entity Reference field value, and then return a Node object.
Since this is a Unit test, I am unsure as to how to write an assertion.
Note: just included relevant code.
class ClassIAmTesting {
public function populateSymbols(Node $node) {
$node->field_symbols->appendItem(['target_id' => $symbol_id]);
return $node;
}
}
I have created a mock for the Entity Reference Field:
$entityReferenceMock = $this->getMockBuilder(EntityReferenceFieldItemList::class)
->disableOriginalConstructor()
->setMethods(['appendItem'])
->getMock();
I have also created a mock of the Node Object
$nodeMock = $this->getMockBuilder(Node::class)
->disableOriginalConstructor()
->getMock();
$nodeMock->expects($this->any)
->method('__get')
->with('field_symbols_in_this_story')
->willReturn($entityReferenceMock);
The method returns a Node object but how can I assert that the Entity Reference Field Value has changed?
Edit: The class with more details:
class ClassIAmTesting {
public function populateSymbols(Node $node) {
$body = $node->field_body->value;
$dom = Html::load($body);
$xpath = new \DOMXPath($dom);
foreach ($xpath->query('//span[@class="tooltip"]') as $xpathNode) {
$symbol_id = $xpathNode->getAttribute('data-symbol');
$node->field_symbols->appendItem(['target_id' => $symbol_id]);
}
return $node;
}
}