Is it possible to use dynamic key names from #template parameters in JSDoc? - templates

I am documenting a set of GraphQL-based APIs, which all return a response of similar structure.
I have a #typedef with #template parameters to document these, which looks like this:
/**
* #template DataType
* #typedef {Promise<{
* data: DataType | null,
* errors: ReadonlyArray<GQLErrorObject>
* }>} GQLResponse
*/
And then, I type the API #return like this:
/*
* #return {GQLResponse<{ project_createNew: Project | null }>}
*/
But I have many such APIs, and I was wondering if it was possible to #template-ify the project_createNew key name? So, for example, if I have the API project_downloadReport, I would like to be able to type it something like this:
/*
* #returns {GQLResponse<"project_downloadReport", PDFFileData}>}
*/
Is this possible at all?

Related

Doctrine prevent error on OneToOne association where record doesn't exist in database

Problem
I'm trying to create a OneToOne association in a Laravel app using Doctrine. When trying to access the association I'm getting this error.
Entity of type 'Status' for IDs clientId(1) was not found
Versions:
Doctrine: 2.7.5
Laravel: 7.30.4
Code:
Client Class
<?php
namespace App\Client;
use App\Person\Person;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Client
* #package App\Client
*
* #ORM\Entity(repositoryClass="ClientRepository")
* #ORM\Table(name="client")
*/
class Client extends Person
{
/**
* #var Status
*
* #ORM\OneToOne(targetEntity="Status", mappedBy="client")
* #ORM\JoinColumn(name="id", referencedColumnName="client_id", nullable=true)
*/
protected $status;
/**
* #return Status
*/
public function getStatus()
{
return $this->status;
}
}
Status Class:
<?php
namespace App\Client\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Class Status
* #package App\Client\Entity
*
* #ORM\Entity(readOnly=true)
* #ORM\Table(name="status_view")
*/
class Status
{
/**
* #var int
*
* #ORM\Id()
* #ORM\Column(name="client_id", type="integer")
*/
protected $clientId;
/**
* #var \App\Client\Client
*
* #ORM\OneToOne(targetEntity="App\Client\Client", inversedBy="staus")
* #ORM\JoinColumn(name="client_id", referencedColumnName="id", nullable=true)
*/
protected $client;
/**
* #var string
*
* #ORM\Column(name="status", type="string")
*/
protected $status;
/**
* #return string
*/
public function getStatus()
{
return $this->status;
}
}
Calling Code
$client->getStatus()->getStatus()
What I've tried/Answers I've looked at
Entity of type 'AppBundle\Entity\User' for IDs id(155) was not found - I'm not using a Doctrine filter, nor DQL.
https://stackoverflow.com/a/49416542/9530790 - This works, with a few tweaks, by swallowing up the exception, but it feels more like a hack when the docs say nullable should work.
https://stackoverflow.com/a/21887344/9530790 - This states nullable should work but it doesn't.
https://stackoverflow.com/a/15744449/9530790 - Same question different ans. States that Doctrine doesn't support zero-to-one associations, but nullable I believe should be what solves that, but for my problem it's not working. Also there's no link to the docs stating where Zero to one is not supported.
I believe that adding fetch="EAGER" should fix the null issue as elsewhere in our app that works, but when I add that I get an different Doctrine error spl_object_hash() expects parameter 1 to be object, null given, which again has to do with the association not existing.
"Well why aren't you experiencing the above error with your other associations". Great question! After a deep underwater excursion into the Doctrine code, I believe the reason is because those associations are nested and for some reason (I'm not sure why), when nested, the spl_object_hash function, in class UnitOfWork is not called.
Additional Notes:
This is what the object looks like when calling $client->getStatus(), before it errors on the next ->getStatus() call.
DoctrineProxies\__CG__\App\Client\Entity\Status {#2011
+__isInitialized__: false
#clientId: 4
#client: null
#status: null
…2
}
You can see it's a Client Proxy object that's created not a 'true' object, this is why it errors (with Entity of type 'Status' for IDs clientId(1) was not found) when not using fetch="EAGER", since eager loads a true object. See here
This code below in the Proxy object is the what causes the above error. Which is why I can't do a try catch in the parent ('true' Client class), since it errors before calling the parent.
/**
* {#inheritDoc}
*/
public function getStatus()
{
$this->__initializer__ && $this->__initializer__->__invoke($this, 'getStatus', []);
return parent::getStatus();
}
Question:
Why is nullable=true not working as expected, and what should/can I do to make it work?

how to add/remove an entity's mapping programmatically

I have a handler which uses the same Entity for two different kind of queries:
/**
* #ORM\Entity
* #ORM\Table(name="app_iso50k1.meter", schema="app_iso50k1")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="mettype", type="string")
* #ORM\DiscriminatorMap({"phy" = "PhyMeter"})
*/
abstract class Meter
{
const TYPE_PHYSICAL = 'PHY';
const TYPE_VIRTUAL = 'VIR';
const TYPE_LOGICAL = 'LOG';
/**
* #ORM\Column(type="string")
* #ORM\Id
*/
protected $id;
<methods>
}
/**
* #ORM\Entity
* #ORM\Entity(repositoryClass="PhyMeterRepository")
* #HasLifecycleCallbacks
*/
class PhyMeter extends Meter
{
/**
* #ORM\Column(type="integer", nullable=false)
*/
protected $idInstrum;
/**
* #ORM\Column(type="integer", nullable=false)
*/
protected $idDevice;
/**
* #ORM\Column(type="integer")
*/
protected $instrumType;
...<methods>
}
The first handler's method is performed on a legacy DB table and it would need to map all the fields annotated with #ORM\Column (id, idInstrum, idDevice, instrumType). To do that, I use a primitive query and I map the data by means of a ResultSetMapping
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Belka\Iso50k1Bundle\Entity\PhyMeter', 'mt');
$rsm->addFieldResult('mt', 'id', 'id');
...
and it works like a charm. The problem is on the the other handler's methods which need to persist data on app.meter table: what I really would like to persist are a small part of the properties (i.e. just the id, idInstrum but not instrumType so as not to have that column in my new table's schema.
I was thinking about using StaticPHPDriver but I'm not sure if it is the right way: what I really would like is manually adding/removing some ORM mapping according to my needs (i.e. different handler's functions)
Is that possible? I could remove the mappings (#ORM\column annotation) I don't need to persist, but that way I cannot map the extra properties by using ResultSetMapping, unless I can add it programmatically.
Any hint is more than welcome

Bidirectional One-To-One Relationships in Flow

Is it possible to have One-To-One Relationships in Flow without having to set the attributes twice?
I have two tables that are connected in a One-To-One Relationship, but only one of them should contain an extra column for this Relation.
Doctrine clearly supports this behavior:
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional
The class that should come with a componenttape column:
/**
* #Flow\Entity
*/
class Component{
/**
* #var \Some\Package\Domain\Model\Component\Tape
* #ORM\OneToOne(cascade={"all"}, inversedBy="component")
*/
protected $componentTape;
…
}
The class that should just be able to find the connection without an extra column:
/**
* #Flow\Entity
*/
class Tape{
/**
* #var \ Some\Package\Domain\Model\Component
* #ORM\OneToOne(mappedBy="componentTape")
*/
protected $component;
}
A doctrine update will create extra columns for both models.
This is what my workarround at the moment looks like:
class Component{
..
/**
* #param \Some\Package\Domain\Model\Component\Tape $componentTape
* #return void
*/
public function setComponentTape($componentTape) {
$this->componentTape = $componentTape;
$this->componentTape->setComponent($this);
}
The workaround will be necessary anyway to keep the relation correct at all times during a request.
But the second DB column shouldn't be necessary. Did you check if doctrine actually fills it? Maybe/Probably just the created migration is wrong and the component column in Tape can be omitted.
Does your workaround stil work for you?
In my case, I have to update the ComponentTape model on the repository by self:
class Component {
/**
* #param \Some\Package\Domain\Model\Component\Tape $componentTape
* #return void
*/
public function setComponentTape($componentTape) {
$this->componentTape = $componentTape;
$this->componentTape->setComponent($this);
$this->componentTapeRepository->update($this->componentTape);
}

How to correctly test repository with xSpec tool?

I learn TDD and I've started to use xSpec tool (language does not matter, but it's phpspec2 in my case). I write my first specification:
<?php
namespace spec\Mo\SpeechBundle\Controller;
use Doctrine\Common\Collections\Collection;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Mo\SpeechBundle\Repository\IdeaRepository;
use Mo\SpeechBundle\Repository\SpeechRepository;
use Mo\SpeechBundle\Entity\Idea;
use Mo\SpeechBundle\Entity\Speech;
class SpeechControllerSpec extends ObjectBehavior
{
function let(SpeechRepository $speechRepository, IdeaRepository $ideaRepository, EngineInterface $templating)
{
$this->beConstructedWith($speechRepository, $ideaRepository, $templating);
}
function it_is_initializable()
{
$this->shouldHaveType('Mo\SpeechBundle\Controller\SpeechController');
}
function it_responds_to_show_action(EngineInterface $templating, Speech $speech, Response $response)
{
$templating
->renderResponse('MoSpeechBundle:Speech:show.html.twig', ['speech' => $speech])
->willReturn($response)
;
$this->showAction($speech)->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
function it_responds_to_list_action(
SpeechRepository $speechRepository,
IdeaRepository $ideaRepository,
EngineInterface $templating,
Response $response
)
{
$speeches = [new Speech()];
$ideas = [new Idea()];
$speechRepository->findAll()->willReturn($speeches);
$ideaRepository->findAll()->willReturn($ideas);
$templating
->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'ideas'))
->willReturn($response)
;
$this->listAction()->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
function it_responds_list_by_idea_action(
Idea $idea,
SpeechRepository $speechRepository,
IdeaRepository $ideaRepository,
EngineInterface $templating,
Response $response
)
{
$speeches = [new Speech()];
$ideas = [new Idea()];
$speechRepository->findByIdea($idea)->willReturn($speeches);
$ideaRepository->findAll()->willReturn($ideas);
$templating
->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'idea', 'ideas'))
->willReturn($response)
;
$this->listByIdeaAction($idea)->shouldBeAnInstanceOf('Symfony\Component\HttpFoundation\Response');
}
}
For controller:
<?php
namespace Mo\SpeechBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Mo\SpeechBundle\Repository\IdeaRepository;
use Mo\SpeechBundle\Repository\SpeechRepository;
use Mo\SpeechBundle\Entity\Idea;
use Mo\SpeechBundle\Entity\Speech;
/**
* Manages speeches.
*/
class SpeechController
{
/**
* #var \Mo\SpeechBundle\Repository\SpeechRepository
*/
private $speechRepository;
/**
* #var \Mo\SpeechBundle\Repository\IdeaRepository
*/
private $ideaRepository;
/**
* #var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
*/
private $templating;
/**
* #param \Mo\SpeechBundle\Repository\SpeechRepository $speechRepository
* #param \Mo\SpeechBundle\Repository\IdeaRepository $ideaRepository
* #param \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface $templating
*/
public function __construct(SpeechRepository $speechRepository, IdeaRepository $ideaRepository, EngineInterface $templating)
{
$this->speechRepository = $speechRepository;
$this->ideaRepository = $ideaRepository;
$this->templating = $templating;
}
/**
* Shows speech.
*
* #param \Mo\SpeechBundle\Entity\Speech $speech
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function showAction(Speech $speech)
{
return $this->templating->renderResponse('MoSpeechBundle:Speech:show.html.twig', compact('speech'));
}
/**
* Shows list of speeches filtered by idea.
*
* #param \Mo\SpeechBundle\Entity\Idea $idea
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function listByIdeaAction(Idea $idea)
{
$speeches = $this->speechRepository->findByIdea($idea);
$ideas = $this->ideaRepository->findAll();
return $this->templating->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'idea', 'ideas'));
}
/**
* Shows list of all speeches.
*
* #return \Symfony\Component\HttpFoundation\Response
*/
public function listAction()
{
$speeches = $this->speechRepository->findAll();
$ideas = $this->ideaRepository->findAll();
return $this->templating->renderResponse('MoSpeechBundle:Speech:list.html.twig', compact('speeches', 'ideas'));
}
}
OK, now I'm sure that behavior of my controller is specified and I can move forward. But I have another problem.
I've used mock of repository for controller spec and now I want to write spec for repository itself:
<?php
namespace Mo\SpeechBundle\Repository;
use Doctrine\ORM\EntityRepository;
use Mo\SpeechBundle\Entity\Idea;
class SpeechRepository extends EntityRepository
{
/**
* Finds all speeches by specified idea.
*
* #param \Mo\SpeechBundle\Entity\Idea $idea
*
* #return array
*/
public function findByIdea(Idea $idea)
{
return $this
->createQueryBuilder('s')
->leftJoin('s.ideas', 'i')
->where('i = :idea')
->setParameters(compact('idea'))
->getQuery()
->getResult()
;
}
}
But specs describe behavior, as I understood. How correctly test repository that it really returns what I need, in my case speeches by ideas.
Should I consider to create functional test with xUnit tool (PHPUnit in PHP world)? Or I wri
te spec which describes that my repository correctly creates query? Or can I just use Behat for all app and don't pay attention to this problem.
I have spent a week for analysis of this question and found satisfactory answer.
The phpspec only specify behavior of our objects. Nothing more. We can't create functional tests with them.
So, we have two ways to test our functionality:
Use PHPUnit to write functional tests for modules and system itself.
Use Behat to describe features of our application.
PHPUnit, other similar framework and Behat have their pitfalls and strong sides.
What to use, can decide only a developer.
I understand your predicament completely and I have done exactly the same thing in the past. I think that the basic answer to your question is that your business logic should be separate from any framework (infrastructure code), and so you should not be testing objects of the type 'Doctrine\ORM\EntityRepository'.
I think the best approach is the have another layer within your application which will hold your business logic, and this in turn could use adapters to pass messages back and forth from the 'Doctrine\ORM\EntityRepository' type objects. That way you can fully spec your business rules (including any adapters) without having to test doctrine type objects which should not be tested anyway as for the most part this is 3rd party code.
Doing things this way also makes it easier for you to change your business rules in future.

Table already exist

I am using doctrine 2 in zend framework 2. Below is my entity file. The problem is, when I tried to validate schema using,
./vendor/bin/doctrine-module orm:validate-schema
command.
I am getting error,
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'database.opportunitycriteria' already exists.
What should I do?
namespace Administration\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* OpportunityCriteria
*
* #ORM\Table(name="OpportunityCriteria")
* #ORM\Entity
*/
class Criteria
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="criteria_id", type="integer", nullable=false)
*/
private $criteria_id;
/**
* #var string
*
* #ORM\Column(name="description", type="string", nullable=false)
*/
private $description;
}
and appropriate getter and setter methods..
I finally figured it out. OP's use case may be different, but in my case, this was because of a misconfigured bidirectional many-to-many relationship.
I had the following entities:
class Cuisine {
/**
* #ManyToMany(targetEntity="Dish")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $dishes;
}
class Dish {
/**
* #ORM\ManyToMany(targetEntity="Cuisine")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
What was missing was the inversedBy and mappedBy properties of the #ManyToMany annotations. These are only required when the association is bi-directional.
So now the correctly mapped entities look like:
class Cuisine {
/**
* #ManyToMany(targetEntity="Dish", inversedBy="cuisines")
* #ORM\JoinTable(name="CuisineDish", )
*/
protected $dishes;
}
class Dish {
/**
* #ORM\ManyToMany(targetEntity="Cuisine", mappedBy="dishes")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
And orm:validate-schema does not exit with an exception any more.
The exception message is just misleading, as the database is not altered by this operation. Furthermore, this issue is only spotted when validating the sync with the database, not when validating the mapping only (--skip-sync), where it should.
I just reported this bug.
it can cause this error message if you want to use a table name which is already used by one of the installed bundles.