Exemplo n.º 1
0
 /**
  * test inserting a Statistic  and re grabbing it from mySQL
  **/
 public function testGetValidStatisticByStatisticId()
 {
     //count the number of rows and save
     $numRows = $this->getConnection()->getRowCount("statistic");
     //create a new Statistic adn insert into mySQL
     $statistic = new Statistic(null, $this->VALID_STATISTICNAME);
     $statistic->insert($this->getPDO());
     //grab data from mySQL and enforce  the fields to expectations
     $pdoStatistic = Statistic::getStatisticByStatisticId($this->getPDO(), $statistic->getStatisticId());
     $this->assertEquals($numRows + 1, $this->getConnection()->getRowCount("statistic"));
     $this->assertEquals($pdoStatistic->getStatisticName(), $this->VALID_STATISTICNAME);
 }
Exemplo n.º 2
0
 $method = array_key_exists("HTTP_X_HTTP_METHOD", $_SERVER) ? $_SERVER["HTTP_X_HTTP_METHOD"] : $_SERVER["REQUEST_METHOD"];
 //sanitize inputs
 $id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);
 //make sure the id is valid for methods that require it
 if (($method === "DELETE" || $method === "PUT") && (empty($id) === true || $id < 0)) {
     throw new InvalidArgumentException("id can not be empty or negitive", 405);
 }
 //Sanitize and trim other fields
 $statisticId = filter_input(INPUT_GET, "statisticId", FILTER_VALIDATE_INT);
 $statisticName = filter_input(INPUT_GET, "statisticName", FILTER_SANITIZE_STRING);
 //handle REST calls, while only allowing administrators to access database-modifying methods
 if ($method === "GET") {
     //set XSRF cookie
     setXsrfCookie("/");
     if (empty($id) === false) {
         $statistic = Statistic::getStatisticByStatisticId($pdo, $id);
         if ($statistic !== null && $statistic->getStatisticId() === $_SESSION["statistic"]->getStatisticId()) {
             $reply->data = $statistic;
         }
     } else {
         if (empty($name) === false) {
             $statistic = Statistic::getStatisticByStatisticName($pdo, $statisticName);
             if ($statistic !== null && $statistic->getStatisticId() === $_SESSION["statistic"]->getStatisticId()) {
                 $reply->data = $statistic;
             }
         }
     }
 }
 if ($method === "GET") {
     setXsrfCookie("/");
     $statistics = Statistic::getAllStatistic($pdo)->toArray();