entityTypeManager = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityTypeManager')->disableOriginalConstructor()->getMock(); $this->utils = new Utils($this->entityTypeManager); $this->storage = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityStorageInterface')->getMock(); $this->query = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Query\\QueryInterface')->getMock(); $this->entityTypeManager->method('getStorage')->willReturn($this->storage); } public function testGetTidByNameGetsTid():void { $term_entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\EntityBase')->disableOriginalConstructor()->getMock(); $term_entity->expects($this->once())->method('id')->willReturn('3'); $this->storage->expects($this->once())->method('loadByProperties')->with(['name' => 'foo', 'vid' => 'bar']) ->willReturn([$term_entity]); $this->assertEquals($this->utils->getTidByName('foo', 'bar'), 3); } }