Skip to content

Instantly share code, notes, and snippets.

@GKephart
Last active May 4, 2017 15:16
Show Gist options
  • Save GKephart/76ebee4fc7de003e29d9714605575ea6 to your computer and use it in GitHub Desktop.
Save GKephart/76ebee4fc7de003e29d9714605575ea6 to your computer and use it in GitHub Desktop.
easy bug creation
PHPUnit 6.0.10 by Sebastian Bergmann and contributors.
............E. 14 / 14 (100%)
Time: 14.17 seconds, Memory: 10.00MB
There was 1 error:
1) Edu\Cnm\DataDesign\Test\TweetTest::testGetValidTweetBySunDate
TypeError: Argument 2 passed to Edu\Cnm\DataDesign\Tweet::__construct() must be of the type integer, null given, called in /home/gkephart/public_html/ddc-twitter/php/classes/Tweet.php on line 427
<?php
/**
* gets an array of tweets based on its date
* (this is an optional get by method and has only been added for when specific edge cases arise in capstone projects)
*
* @param \PDO $pdo connection object
* @param \DateTime $sunriseTweetDate beginning date to search for
* @param \DateTime $sunsetTweetDate ending date to search for
* @return \SplFixedArray of tweets found
* @throws \PDOException when mySQL related errors occur
* @throws \TypeError when variables are not the correct data type
* @throws \InvalidArgumentException if either sun dates are in the wrong format
*/
public static function getTweetByTweetDate (\PDO $pdo, \DateTime $sunriseTweetDate, \DateTime $sunsetTweetDate ) : \SplFixedArray {
//enforce both date are present
if((empty ($sunriseTweetDate) === true) || (empty($sunsetTweetDate) === true)) {
throw (new \InvalidArgumentException("dates are empty of insecure"));
}
//ensure both dates are in the correct format and are secure
try {
$sunriseTweetDate = self::validateDateTime($sunriseTweetDate);
$sunsetTweetDate = self::validateDateTime($sunsetTweetDate);
} catch(\InvalidArgumentException | \RangeException $exception) {
$exceptionType = get_class($exception);
throw(new $exceptionType($exception->getMessage(), 0, $exception));
}
//create query template
$query = "SELECT tweetId, tweetProfileId, tweetContent, tweetDate from tweet WHERE tweetDate >= :sunriseTweetDate AND tweetDate <= :sunsetTweetDate";
$statement = $pdo->prepare($query);
//format the dates so that mySQL can use them
$formattedSunriseDate = $sunriseTweetDate->format("Y-m-d H:i:s");
$formattedSunsetDate = $sunsetTweetDate->format("Y-m-d H:i:s");
$parameters = ["sunriseTweetDate" => $formattedSunriseDate, "sunsetTweetDate" => $formattedSunsetDate];
$statement->execute($parameters);
//build an array of tweets
$tweets = new \SplFixedArray($statement->rowCount());
$statement->setFetchMode(\PDO::FETCH_ASSOC);
while(($row = $statement->fetch()) !== false) {
try {
$tweet = new Tweet($row["tweetId"], $row["tweetProfileId"],$row["tweetContent"], $row["tweetDate"]);
$tweets[$tweets->key()] = $tweet;
$tweets->next();
} catch(\Exception $exception) {
throw (new \PDOException($exception->getMessage(),0, $exception));
}
}
return($tweets);
}
<?php
/**
* gets an array of tweets based on its date
* (this is an optional get by method and has only been added for when specific edge cases arise in capstone projects)
*
* @param \PDO $pdo connection object
* @param \DateTime $sunriseTweetDate beginning date to search for
* @param \DateTime $sunsetTweetDate ending date to search for
* @return \SplFixedArray of tweets found
* @throws \PDOException when mySQL related errors occur
* @throws \TypeError when variables are not the correct data type
* @throws \InvalidArgumentException if either sun dates are in the wrong format
*/
public static function getTweetByTweetDate (\PDO $pdo, \DateTime $sunriseTweetDate, \DateTime $sunsetTweetDate ) : \SplFixedArray {
//enforce both date are present
if((empty ($sunriseTweetDate) === true) || (empty($sunsetTweetDate) === true)) {
throw (new \InvalidArgumentException("dates are empty of insecure"));
}
//ensure both dates are in the correct format and are secure
try {
$sunriseTweetDate = self::validateDateTime($sunriseTweetDate);
$sunsetTweetDate = self::validateDateTime($sunsetTweetDate);
} catch(\InvalidArgumentException | \RangeException $exception) {
$exceptionType = get_class($exception);
throw(new $exceptionType($exception->getMessage(), 0, $exception));
}
//create query template
$query = "SELECT tweetId, tweetProfileId, tweetContent, tweetDate from tweet WHERE tweetDate >= :sunriseTweetDate AND tweetDate <= :sunsetTweetDate";
$statement = $pdo->prepare($query);
//format the dates so that mySQL can use them
$formattedSunriseDate = $sunriseTweetDate->format("Y-m-d H:i:s");
$formattedSunsetDate = $sunsetTweetDate->format("Y-m-d H:i:s");
$parameters = ["sunriseTweetDate" => $formattedSunriseDate, "sunsetTweetDate" => $formattedSunsetDate];
$statement->execute($parameters);
//build an array of tweets
$tweets = new \SplFixedArray($statement->rowCount());
$statement->setFetchMode(\PDO::FETCH_ASSOC);
while($row = $statement->fetch() !== false) {
try {
$tweet = new Tweet($row["tweetId"], $row["tweetProfileId"],$row["tweetContent"], $row["tweetDate"]);
$tweets[$tweets->key()] = $tweet;
$tweets->next();
} catch(\Exception $exception) {
throw (new \PDOException($exception->getMessage(),0, $exception));
}
}
return($tweets);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment