mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-26 11:48:49 +02:00
Added some tests for important entity methods.
This commit is contained in:
parent
0663a00df8
commit
89258bc102
18 changed files with 480 additions and 160 deletions
|
@ -87,19 +87,20 @@ class AttachmentType extends StructuralDBElement
|
|||
*/
|
||||
protected $filetype_filter = "";
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
$this->attachments = new ArrayCollection();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all attachements ("Attachement" objects) with this type.
|
||||
* Get all attachments ("Attachment" objects) with this type.
|
||||
*
|
||||
* @return Collection|Attachment[] all attachements with this type, as a one-dimensional array of Attachement-objects
|
||||
* @return Collection|Attachment[] all attachements with this type, as a one-dimensional array of Attachements
|
||||
* (sorted by their names)
|
||||
*/
|
||||
public function getAttachementsForType(): Collection
|
||||
public function getAttachmentsForType(): Collection
|
||||
{
|
||||
// the attribute $this->attachements is used from class "AttachementsContainingDBELement"
|
||||
if (null === $this->attachments) {
|
||||
$this->attachments = new ArrayCollection();
|
||||
}
|
||||
|
||||
return $this->attachments;
|
||||
}
|
||||
|
||||
|
|
|
@ -73,15 +73,11 @@ abstract class NamedDBElement extends DBElement
|
|||
*********************************************************************************/
|
||||
|
||||
/**
|
||||
* Get the name.
|
||||
*
|
||||
* Get the name of this element
|
||||
* @return string the name of this element
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
/*
|
||||
//Strip HTML from Name, so no XSS injection is possible.
|
||||
return strip_tags($this->name); */
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
|
@ -94,12 +90,7 @@ abstract class NamedDBElement extends DBElement
|
|||
/**
|
||||
* Change the name of this element.
|
||||
*
|
||||
* Spaces at the begin and at the end of the string will be removed
|
||||
* automatically in NamedDBElement::check_values_validity().
|
||||
* So you don't have to do this yourself.
|
||||
*
|
||||
* @param string $new_name the new name
|
||||
*
|
||||
* @return self
|
||||
*/
|
||||
public function setName(string $new_name): self
|
||||
|
@ -115,8 +106,6 @@ abstract class NamedDBElement extends DBElement
|
|||
*
|
||||
******************************************************************************/
|
||||
|
||||
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return $this->getName();
|
||||
|
|
|
@ -116,7 +116,6 @@ class PartLot extends DBElement
|
|||
* Check if the current part lot is expired.
|
||||
* This is the case, if the expiration date is greater the the current date.
|
||||
* @return bool|null True, if the part lot is expired. Returns null, if no expiration date was set.
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function isExpired(): ?bool
|
||||
{
|
||||
|
@ -125,7 +124,7 @@ class PartLot extends DBElement
|
|||
}
|
||||
|
||||
//Check if the expiration date is bigger then current time
|
||||
return $this->expiration_date < new \DateTime();
|
||||
return $this->expiration_date < new \DateTime('now');
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -35,7 +35,7 @@ use Doctrine\Common\Collections\Collection;
|
|||
trait InstockTrait
|
||||
{
|
||||
/**
|
||||
* @var ?PartLot[]|Collection A list of part lots where this part is stored
|
||||
* @var Collection|PartLot[] A list of part lots where this part is stored
|
||||
* @ORM\OneToMany(targetEntity="PartLot", mappedBy="part", cascade={"persist", "remove"}, orphanRemoval=true)
|
||||
* @Assert\Valid()
|
||||
* @ColumnSecurity(type="collection", prefix="lots")
|
||||
|
@ -146,6 +146,7 @@ trait InstockTrait
|
|||
|
||||
/**
|
||||
* Returns the summed amount of this part (over all part lots)
|
||||
* Part Lots that have unknown value or are expired, are not used for this value
|
||||
* @return float The amount of parts given in partUnit
|
||||
*/
|
||||
public function getAmountSum() : float
|
||||
|
@ -154,7 +155,7 @@ trait InstockTrait
|
|||
$sum = 0;
|
||||
foreach ($this->getPartLots() as $lot) {
|
||||
//Dont use the instock value, if it is unkown
|
||||
if ($lot->isInstockUnknown()) {
|
||||
if ($lot->isInstockUnknown() || $lot->isExpired() ?? false) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
|
@ -102,7 +102,7 @@ class Currency extends StructuralDBElement
|
|||
{
|
||||
$tmp = $this->getExchangeRate();
|
||||
|
||||
if ($tmp === null || (float) $tmp === 0) {
|
||||
if ($tmp === null || $tmp === "0") {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -138,9 +138,9 @@ class Orderdetail extends DBElement
|
|||
/**
|
||||
* Get the part.
|
||||
*
|
||||
* @return Part the part of this orderdetails
|
||||
* @return Part|null the part of this orderdetails
|
||||
*/
|
||||
public function getPart(): Part
|
||||
public function getPart(): ?Part
|
||||
{
|
||||
return $this->part;
|
||||
}
|
||||
|
@ -235,12 +235,13 @@ class Orderdetail extends DBElement
|
|||
}
|
||||
|
||||
/**
|
||||
* Get the pricedetail for a specific quantity.
|
||||
* Find the pricedetail that is correct for the desired amount (the one with the greatest discount value with a
|
||||
* minimum order amount of the wished quantity)
|
||||
* @param float $quantity this is the quantity to choose the correct pricedetails
|
||||
*
|
||||
* @return Pricedetail|null: the price as a bcmath string. Null if there are no orderdetails for the given quantity
|
||||
*/
|
||||
public function getPrice(float $quantity = 1) : ?Pricedetail
|
||||
public function findPriceForQty(float $quantity = 1) : ?Pricedetail
|
||||
{
|
||||
if ($quantity <= 0) {
|
||||
return null;
|
||||
|
|
|
@ -130,25 +130,15 @@ class Pricedetail extends DBElement
|
|||
*********************************************************************************/
|
||||
|
||||
/**
|
||||
* Get the orderdetails of this pricedetails.
|
||||
* Get the orderdetail to which this pricedetail belongs to this pricedetails.
|
||||
*
|
||||
* @return Orderdetail the orderdetails object
|
||||
* @return Orderdetail The orderdetail this price belongs to.
|
||||
*/
|
||||
public function getOrderdetails(): Orderdetail
|
||||
public function getOrderdetail(): Orderdetail
|
||||
{
|
||||
return $this->orderdetail;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the price associated with this pricedetail.
|
||||
* It is given in current currency and for the price related quantity.
|
||||
* @return float
|
||||
*/
|
||||
public function getPriceFloat() : float
|
||||
{
|
||||
return (float) $this->price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the price associated with this pricedetail.
|
||||
* It is given in current currency and for the price related quantity.
|
||||
|
@ -159,16 +149,6 @@ class Pricedetail extends DBElement
|
|||
return $this->price;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the price associated with this pricedetail as integer.
|
||||
* It is given in current currency and for the price related quantity, in parts of 0.00001 (5 digits)
|
||||
* @return int
|
||||
*/
|
||||
public function getPriceInt() : int
|
||||
{
|
||||
return (int) str_replace('.', '', $this->price);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the price for a single unit in the currency associated with this price detail.
|
||||
*
|
||||
|
@ -179,7 +159,6 @@ class Pricedetail extends DBElement
|
|||
* in the database, you have to pass the "price_related_quantity" count as $multiplier.
|
||||
*
|
||||
* @return string the price as a bcmath string
|
||||
|
||||
*/
|
||||
public function getPricePerUnit($multiplier = 1.0) : string
|
||||
{
|
||||
|
@ -201,7 +180,7 @@ class Pricedetail extends DBElement
|
|||
*/
|
||||
public function getPriceRelatedQuantity(): float
|
||||
{
|
||||
if ($this->orderdetail && $this->orderdetail->getPart() && !$this->orderdetail->getPart()->useFloatAmount()) {
|
||||
if ($this->orderdetail && $this->orderdetail->getPart() && !$this->orderdetail->getPart()->useFloatAmount()) {
|
||||
$tmp = round($this->price_related_quantity);
|
||||
return $tmp < 1 ? 1 : $tmp;
|
||||
}
|
||||
|
|
|
@ -406,12 +406,11 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
|
|||
*/
|
||||
public function getFullName(bool $including_username = false): string
|
||||
{
|
||||
$str = $this->getFirstName().' '.$this->getLastName();
|
||||
if ($including_username) {
|
||||
$str .= ' ('.$this->getName().')';
|
||||
return sprintf('%s %s (%s)', $this->getFirstName(), $this->getLastName(), $this->getName());
|
||||
}
|
||||
|
||||
return $str;
|
||||
return sprintf('%s %s', $this->getFirstName(), $this->getLastName());
|
||||
}
|
||||
|
||||
public function setName(string $new_name): NamedDBElement
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue