} } // post to a new Movement } else { if ($method === "POST") { // convert POSTed JSON to an object verifyXsrf(); $requestContent = file_get_contents("php://input"); $requestObject = json_decode($requestContent); $movementDate = new DateTime(); if (empty($requestObject->movementDate) === false) { $movementDate->setTimestamp($requestObject->movementDate / 1000); } $movement = new Movement(null, $requestObject->fromLocationId, $requestObject->toLocationId, $requestObject->productId, $requestObject->unitId, $requestObject->userId, $requestObject->cost, $movementDate, $requestObject->movementType, $requestObject->price); $movement->insert($pdo); $reply->data = "Movement created OK"; if ($requestObject->quantity !== null) { $productLocation = new ProductLocation($requestObject->toLocationId, $requestObject->productId, $requestObject->unitId, $requestObject->quantity); $productLocation->insert($pdo); $reply->data = "ProductLocation created OK"; } } } // create an exception to pass back to the RESTful caller } catch (Exception $exception) { $reply->status = $exception->getCode(); $reply->message = $exception->getMessage(); unset($reply->data); } header("Content-type: application/json"); echo json_encode($reply);
/** * Test grabbing Valid UnitOfMeasure by productId **/ public function testGetValidUnitOfMeasureByProductId() { // create a new Product $newProduct = new Product(null, $this->vendor->getVendorId(), $this->VALID_description, $this->VALID_leadTime, $this->VALID_sku, $this->VALID_title); $newProduct->insert($this->getPDO()); // create a new ProductLocation $quantity = 1.5; $newProductLocation = new ProductLocation($this->location->getLocationId(), $newProduct->getProductId(), $this->unitOfMeasure->getUnitId(), $quantity); $newProductLocation->insert($this->getPDO()); // grab the data from guzzle $response = $this->guzzle->get('https://bootcamp-coders.cnm.edu/~invtext/backend/php/api/product/?productId=' . $newProduct->getProductId() . "&getUnitOfMeasure=true"); $this->assertSame($response->getStatusCode(), 200); $body = $response->getBody(); $product = json_decode($body); $this->assertSame(200, $product->status); }
/** * test grabbing a ProductLocation by productId that does not exist **/ public function testGetInvalidProductLocationByProductId() { // grab an productId that does not exist $pdoProductLocation = ProductLocation::getProductLocationByProductId($this->getPDO(), InventoryTextTest::INVALID_KEY); foreach ($pdoProductLocation as $pdoPL) { $this->assertNull($pdoPL); } }
$product = new Product($productId, $requestObject->vendorId, $requestObject->description, $requestObject->leadTime, $requestObject->sku, $requestObject->title); $product->update($pdo); $reply->data = "Product updated OK"; // delete an existing Product } else { if ($method === "DELETE") { verifyXsrf(); $productAlerts = ProductAlert::getProductAlertByProductId($pdo, $productId); foreach ($productAlerts as $productAlert) { $productAlert->delete($pdo); } $finishedProducts = FinishedProduct::getFinishedProductByRawMaterialId($pdo, $productId); foreach ($finishedProducts as $finishedProduct) { $finishedProduct->delete($pdo); } $productLocations = ProductLocation::getProductLocationByProductId($pdo, $productId); foreach ($productLocations as $productLocation) { $productLocation->delete($pdo); } $movements = Movement::getMovementByProductId($pdo, $productId); foreach ($movements as $movement) { $movement->delete($pdo); } $product = Product::getProductByProductId($pdo, $productId); $product->delete($pdo); $reply->data = "Product deleted OK"; } } } } // create an exception to pass back to the RESTful caller
/** * test grabbing unit of measure by the product id **/ public function testGetValidUnitOfMeasurementByProductId() { // create a new product and insert to into mySQL $product = new Product(null, $this->vendor->getVendorId(), $this->VALID_description, $this->VALID_leadTime, $this->VALID_sku, $this->VALID_title); $product->insert($this->getPDO()); $quantity = 5.9; // create a new product and insert to into mySQL $productLocation = new ProductLocation($this->location->getLocationId(), $product->getProductId(), $this->unitOfMeasure->getUnitId(), $quantity); $productLocation->insert($this->getPDO()); // grab the data from mySQL and enforce the fields match our expectations $pdoUnitOfMeasureArray = Product::getUnitOfMeasureByProductId($this->getPDO(), $product->getProductId()); for ($i = 0; $i < count($pdoUnitOfMeasureArray); $i++) { if ($i === 0) { $this->assertSame($pdoUnitOfMeasureArray[$i]->getVendorId(), $this->vendor->getVendorId()); $this->assertSame($pdoUnitOfMeasureArray[$i]->getDescription(), $this->VALID_description); $this->assertSame($pdoUnitOfMeasureArray[$i]->getLeadTime(), $this->VALID_leadTime); $this->assertSame($pdoUnitOfMeasureArray[$i]->getSku(), $this->VALID_sku); $this->assertSame($pdoUnitOfMeasureArray[$i]->getTitle(), $this->VALID_title); } else { $this->assertSame($pdoUnitOfMeasureArray[$i]->getUnitId(), $this->unitOfMeasure->getUnitId()); $this->assertSame($pdoUnitOfMeasureArray[$i]->getUnitCode(), $this->unitOfMeasure->getUnitCode()); $this->assertSame($pdoUnitOfMeasureArray[$i]->getQuantity(), $this->unitOfMeasure->getQuantity()); } } }