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

Throw exception if no field is provided when calling toLineProtocol() #150

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## 3.5.0 [unreleased]

1. [#149](https://github.com/influxdata/influxdb-client-php/issues/149): Throw exception on empty field

## 3.4.0 [2023-07-28]

### Bug Fixes
Expand Down
5 changes: 3 additions & 2 deletions src/InfluxDB2/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,10 @@ public function time($time, $precision = null): Point
return $this;
}

/** If there is no field then return null.
/** If there is no field then throw an exception.
*
* @return string representation of the point
* @throws \InvalidArgumentException when no field is provided
*/
public function toLineProtocol()
{
Expand All @@ -135,7 +136,7 @@ public function toLineProtocol()
$fields = $this->appendFields();

if ($this->isNullOrEmptyString($fields)) {
return null;
throw new \InvalidArgumentException("Failed to write Point: At least one field is required.");
}

$lineProtocol .= $fields;
Expand Down
5 changes: 4 additions & 1 deletion tests/PointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DateTimeImmutable;
use InfluxDB2\Model\WritePrecision;
use InfluxDB2\Point;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;

class PointTest extends TestCase
Expand Down Expand Up @@ -262,11 +263,13 @@ public function testWithoutTags()

public function testWithoutFields()
{
$this->expectException(\InvalidArgumentException::class);

$point = Point::measurement('h2o')
->addTag('location', 'europe')
->time(123);

$this->assertNull($point->toLineProtocol());
$point->toLineProtocol();
}

public function testFromArrayWithoutName()
Expand Down