How to use dateTime class

Best Atoum code snippet using dateTime

TimeInterval.php

Source:TimeInterval.php Github

copy

Full Screen

1<?php2/**3 * Class for time interval and numeric range handling for reports.4 *5 * @package WooCommerce Admin/Classes6 */7namespace Automattic\WooCommerce\Admin\API\Reports;8defined( 'ABSPATH' ) || exit;9/**10 * Date & time interval and numeric range handling class for Reporting API.11 */12class TimeInterval {13 /**14 * Format string for ISO DateTime formatter.15 *16 * @var string17 */18 public static $iso_datetime_format = 'Y-m-d\TH:i:s';19 /**20 * Format string for use in SQL queries.21 *22 * @var string23 */24 public static $sql_datetime_format = 'Y-m-d H:i:s';25 /**26 * Converts local datetime to GMT/UTC time.27 *28 * @param string $datetime_string String representation of local datetime.29 * @return DateTime30 */31 public static function convert_local_datetime_to_gmt( $datetime_string ) {32 $datetime = new \DateTime( $datetime_string, new \DateTimeZone( wc_timezone_string() ) );33 $datetime->setTimezone( new \DateTimeZone( 'GMT' ) );34 return $datetime;35 }36 /**37 * Returns default 'before' parameter for the reports.38 *39 * @return DateTime40 */41 public static function default_before() {42 $datetime = new \WC_DateTime();43 // Set local timezone or offset.44 if ( get_option( 'timezone_string' ) ) {45 $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );46 } else {47 $datetime->set_utc_offset( wc_timezone_offset() );48 }49 return $datetime;50 }51 /**52 * Returns default 'after' parameter for the reports.53 *54 * @return DateTime55 */56 public static function default_after() {57 $now = time();58 $week_back = $now - WEEK_IN_SECONDS;59 $datetime = new \WC_DateTime();60 $datetime->setTimestamp( $week_back );61 // Set local timezone or offset.62 if ( get_option( 'timezone_string' ) ) {63 $datetime->setTimezone( new \DateTimeZone( wc_timezone_string() ) );64 } else {65 $datetime->set_utc_offset( wc_timezone_offset() );66 }67 return $datetime;68 }69 /**70 * Returns date format to be used as grouping clause in SQL.71 *72 * @param string $time_interval Time interval.73 * @param string $table_name Name of the db table relevant for the date constraint.74 * @return mixed75 */76 public static function db_datetime_format( $time_interval, $table_name ) {77 $first_day_of_week = absint( get_option( 'start_of_week' ) );78 if ( 1 === $first_day_of_week ) {79 // Week begins on Monday, ISO 8601.80 $week_format = "DATE_FORMAT({$table_name}.date_created, '%x-%v')";81 } else {82 // Week begins on day other than specified by ISO 8601, needs to be in sync with function simple_week_number.83 $week_format = "CONCAT(YEAR({$table_name}.date_created), '-', LPAD( FLOOR( ( DAYOFYEAR({$table_name}.date_created) + ( ( DATE_FORMAT(MAKEDATE(YEAR({$table_name}.date_created),1), '%w') - $first_day_of_week + 7 ) % 7 ) - 1 ) / 7 ) + 1 , 2, '0'))";84 }85 // Whenever this is changed, double check method time_interval_id to make sure they are in sync.86 $mysql_date_format_mapping = array(87 'hour' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d %H')",88 'day' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m-%d')",89 'week' => $week_format,90 'month' => "DATE_FORMAT({$table_name}.date_created, '%Y-%m')",91 'quarter' => "CONCAT(YEAR({$table_name}.date_created), '-', QUARTER({$table_name}.date_created))",92 'year' => "YEAR({$table_name}.date_created)",93 );94 return $mysql_date_format_mapping[ $time_interval ];95 }96 /**97 * Returns quarter for the DateTime.98 *99 * @param DateTime $datetime Local date & time.100 * @return int|null101 */102 public static function quarter( $datetime ) {103 switch ( (int) $datetime->format( 'm' ) ) {104 case 1:105 case 2:106 case 3:107 return 1;108 case 4:109 case 5:110 case 6:111 return 2;112 case 7:113 case 8:114 case 9:115 return 3;116 case 10:117 case 11:118 case 12:119 return 4;120 }121 return null;122 }123 /**124 * Returns simple week number for the DateTime, for week starting on $first_day_of_week.125 *126 * The first week of the year is considered to be the week containing January 1.127 * The second week starts on the next $first_day_of_week.128 *129 * @param DateTime $datetime Local date for which the week number is to be calculated.130 * @param int $first_day_of_week 0 for Sunday to 6 for Saturday.131 * @return int132 */133 public static function simple_week_number( $datetime, $first_day_of_week ) {134 $beg_of_year_day = new \DateTime( "{$datetime->format('Y')}-01-01" );135 $adj_day_beg_of_year = ( (int) $beg_of_year_day->format( 'w' ) - $first_day_of_week + 7 ) % 7;136 $days_since_start_of_year = (int) $datetime->format( 'z' ) + 1;137 return (int) floor( ( ( $days_since_start_of_year + $adj_day_beg_of_year - 1 ) / 7 ) ) + 1;138 }139 /**140 * Returns ISO 8601 week number for the DateTime, if week starts on Monday,141 * otherwise returns simple week number.142 *143 * @see TimeInterval::simple_week_number()144 *145 * @param DateTime $datetime Local date for which the week number is to be calculated.146 * @param int $first_day_of_week 0 for Sunday to 6 for Saturday.147 * @return int148 */149 public static function week_number( $datetime, $first_day_of_week ) {150 if ( 1 === $first_day_of_week ) {151 $week_number = (int) $datetime->format( 'W' );152 } else {153 $week_number = self::simple_week_number( $datetime, $first_day_of_week );154 }155 return $week_number;156 }157 /**158 * Returns time interval id for the DateTime.159 *160 * @param string $time_interval Time interval type (week, day, etc).161 * @param DateTime $datetime Date & time.162 * @return string163 */164 public static function time_interval_id( $time_interval, $datetime ) {165 // Whenever this is changed, double check method db_datetime_format to make sure they are in sync.166 $php_time_format_for = array(167 'hour' => 'Y-m-d H',168 'day' => 'Y-m-d',169 'week' => 'o-W',170 'month' => 'Y-m',171 'quarter' => 'Y-' . self::quarter( $datetime ),172 'year' => 'Y',173 );174 // If the week does not begin on Monday.175 $first_day_of_week = absint( get_option( 'start_of_week' ) );176 if ( 'week' === $time_interval && 1 !== $first_day_of_week ) {177 $week_no = self::simple_week_number( $datetime, $first_day_of_week );178 $week_no = str_pad( $week_no, 2, '0', STR_PAD_LEFT );179 $year_no = $datetime->format( 'Y' );180 return "$year_no-$week_no";181 }182 return $datetime->format( $php_time_format_for[ $time_interval ] );183 }184 /**185 * Calculates number of time intervals between two dates, closed interval on both sides.186 *187 * @param DateTime $start_datetime Start date & time.188 * @param DateTime $end_datetime End date & time.189 * @param string $interval Time interval increment, e.g. hour, day, week.190 *191 * @return int192 */193 public static function intervals_between( $start_datetime, $end_datetime, $interval ) {194 switch ( $interval ) {195 case 'hour':196 $end_timestamp = (int) $end_datetime->format( 'U' );197 $start_timestamp = (int) $start_datetime->format( 'U' );198 $addendum = 0;199 // modulo HOUR_IN_SECONDS would normally work, but there are non-full hour timezones, e.g. Nepal.200 $start_min_sec = (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' );201 $end_min_sec = (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' );202 if ( $end_min_sec < $start_min_sec ) {203 $addendum = 1;204 }205 $diff_timestamp = $end_timestamp - $start_timestamp;206 return (int) floor( ( (int) $diff_timestamp ) / HOUR_IN_SECONDS ) + 1 + $addendum;207 case 'day':208 $end_timestamp = (int) $end_datetime->format( 'U' );209 $start_timestamp = (int) $start_datetime->format( 'U' );210 $addendum = 0;211 $end_hour_min_sec = (int) $end_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $end_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $end_datetime->format( 's' );212 $start_hour_min_sec = (int) $start_datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $start_datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $start_datetime->format( 's' );213 if ( $end_hour_min_sec < $start_hour_min_sec ) {214 $addendum = 1;215 }216 $diff_timestamp = $end_timestamp - $start_timestamp;217 return (int) floor( ( (int) $diff_timestamp ) / DAY_IN_SECONDS ) + 1 + $addendum;218 case 'week':219 // @todo Optimize? approximately day count / 7, but year end is tricky, a week can have fewer days.220 $week_count = 0;221 do {222 $start_datetime = self::next_week_start( $start_datetime );223 $week_count++;224 } while ( $start_datetime <= $end_datetime );225 return $week_count;226 case 'month':227 // Year diff in months: (end_year - start_year - 1) * 12.228 $year_diff_in_months = ( (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' ) - 1 ) * 12;229 // All the months in end_date year plus months from X to 12 in the start_date year.230 $month_diff = (int) $end_datetime->format( 'n' ) + ( 12 - (int) $start_datetime->format( 'n' ) );231 // Add months for number of years between end_date and start_date.232 $month_diff += $year_diff_in_months + 1;233 return $month_diff;234 case 'quarter':235 // Year diff in quarters: (end_year - start_year - 1) * 4.236 $year_diff_in_quarters = ( (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' ) - 1 ) * 4;237 // All the quarters in end_date year plus quarters from X to 4 in the start_date year.238 $quarter_diff = self::quarter( $end_datetime ) + ( 4 - self::quarter( $start_datetime ) );239 // Add quarters for number of years between end_date and start_date.240 $quarter_diff += $year_diff_in_quarters + 1;241 return $quarter_diff;242 case 'year':243 $year_diff = (int) $end_datetime->format( 'Y' ) - (int) $start_datetime->format( 'Y' );244 return $year_diff + 1;245 }246 return 0;247 }248 /**249 * Returns a new DateTime object representing the next hour start/previous hour end if reversed.250 *251 * @param DateTime $datetime Date and time.252 * @param bool $reversed Going backwards in time instead of forward.253 * @return DateTime254 */255 public static function next_hour_start( $datetime, $reversed = false ) {256 $hour_increment = $reversed ? 0 : 1;257 $timestamp = (int) $datetime->format( 'U' );258 $seconds_into_hour = (int) $datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $datetime->format( 's' );259 $hours_offset_timestamp = $timestamp + ( $hour_increment * HOUR_IN_SECONDS - $seconds_into_hour );260 if ( $reversed ) {261 $hours_offset_timestamp --;262 }263 $hours_offset_time = new \DateTime();264 $hours_offset_time->setTimestamp( $hours_offset_timestamp );265 $hours_offset_time->setTimezone( new \DateTimeZone( wc_timezone_string() ) );266 return $hours_offset_time;267 }268 /**269 * Returns a new DateTime object representing the next day start, or previous day end if reversed.270 *271 * @param DateTime $datetime Date and time.272 * @param bool $reversed Going backwards in time instead of forward.273 * @return DateTime274 */275 public static function next_day_start( $datetime, $reversed = false ) {276 $seconds_into_day = (int) $datetime->format( 'H' ) * HOUR_IN_SECONDS + (int) $datetime->format( 'i' ) * MINUTE_IN_SECONDS + (int) $datetime->format( 's' );277 // The day boundary is actually next midnight when going in reverse, so set it to day -1 at 23:59:59.278 if ( $reversed ) {279 $timestamp = (int) $datetime->format( 'U' );280 $next_day_timestamp = $timestamp - ( $seconds_into_day + 1 );281 } else {282 $day_increment = new \DateInterval( 'P1D' ); // Plus 1 Day.283 $next_datetime = clone $datetime;284 $next_datetime->add( $day_increment );285 $timestamp = (int) $next_datetime->format( 'U' );286 $next_day_timestamp = $timestamp - $seconds_into_day;287 }288 $next_day = new \DateTime();289 $next_day->setTimestamp( $next_day_timestamp );290 $next_day->setTimezone( new \DateTimeZone( wc_timezone_string() ) );291 return $next_day;292 }293 /**294 * Returns DateTime object representing the next week start, or previous week end if reversed.295 *296 * @param DateTime $datetime Date and time.297 * @param bool $reversed Going backwards in time instead of forward.298 * @return DateTime299 */300 public static function next_week_start( $datetime, $reversed = false ) {301 $first_day_of_week = absint( get_option( 'start_of_week' ) );302 $initial_week_no = self::week_number( $datetime, $first_day_of_week );303 do {304 $datetime = self::next_day_start( $datetime, $reversed );305 $current_week_no = self::week_number( $datetime, $first_day_of_week );306 } while ( $current_week_no === $initial_week_no );307 // The week boundary is actually next midnight when going in reverse, so set it to day -1 at 23:59:59.308 if ( $reversed ) {309 $timestamp = (int) $datetime->format( 'U' );310 $end_of_day_timestamp = floor( $timestamp / DAY_IN_SECONDS ) * DAY_IN_SECONDS + DAY_IN_SECONDS - 1;311 $datetime->setTimestamp( $end_of_day_timestamp );312 }313 return $datetime;314 }315 /**316 * Returns a new DateTime object representing the next month start, or previous month end if reversed.317 *318 * @param DateTime $datetime Date and time.319 * @param bool $reversed Going backwards in time instead of forward.320 * @return DateTime321 */322 public static function next_month_start( $datetime, $reversed = false ) {323 $month_increment = 1;324 $year = $datetime->format( 'Y' );325 $month = (int) $datetime->format( 'm' );326 if ( $reversed ) {327 $beg_of_month_datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );328 $timestamp = (int) $beg_of_month_datetime->format( 'U' );329 $end_of_prev_month_timestamp = $timestamp - 1;330 $datetime->setTimestamp( $end_of_prev_month_timestamp );331 } else {332 $month += $month_increment;333 if ( $month > 12 ) {334 $month = 1;335 $year ++;336 }337 $day = '01';338 $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );339 }340 return $datetime;341 }342 /**343 * Returns a new DateTime object representing the next quarter start, or previous quarter end if reversed.344 *345 * @param DateTime $datetime Date and time.346 * @param bool $reversed Going backwards in time instead of forward.347 * @return DateTime348 */349 public static function next_quarter_start( $datetime, $reversed = false ) {350 $year = $datetime->format( 'Y' );351 $month = (int) $datetime->format( 'n' );352 switch ( $month ) {353 case 1:354 case 2:355 case 3:356 if ( $reversed ) {357 $month = 1;358 } else {359 $month = 4;360 }361 break;362 case 4:363 case 5:364 case 6:365 if ( $reversed ) {366 $month = 4;367 } else {368 $month = 7;369 }370 break;371 case 7:372 case 8:373 case 9:374 if ( $reversed ) {375 $month = 7;376 } else {377 $month = 10;378 }379 break;380 case 10:381 case 11:382 case 12:383 if ( $reversed ) {384 $month = 10;385 } else {386 $month = 1;387 $year ++;388 }389 break;390 }391 $datetime = new \DateTime( "$year-$month-01 00:00:00", new \DateTimeZone( wc_timezone_string() ) );392 if ( $reversed ) {393 $timestamp = (int) $datetime->format( 'U' );394 $end_of_prev_month_timestamp = $timestamp - 1;395 $datetime->setTimestamp( $end_of_prev_month_timestamp );396 }397 return $datetime;398 }399 /**400 * Return a new DateTime object representing the next year start, or previous year end if reversed.401 *402 * @param DateTime $datetime Date and time.403 * @param bool $reversed Going backwards in time instead of forward.404 * @return DateTime405 */406 public static function next_year_start( $datetime, $reversed = false ) {407 $year_increment = 1;408 $year = (int) $datetime->format( 'Y' );409 $month = '01';410 $day = '01';411 if ( $reversed ) {412 $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );413 $timestamp = (int) $datetime->format( 'U' );414 $end_of_prev_year_timestamp = $timestamp - 1;415 $datetime->setTimestamp( $end_of_prev_year_timestamp );416 } else {417 $year += $year_increment;418 $datetime = new \DateTime( "$year-$month-$day 00:00:00", new \DateTimeZone( wc_timezone_string() ) );419 }420 return $datetime;421 }422 /**423 * Returns beginning of next time interval for provided DateTime.424 *425 * E.g. for current DateTime, beginning of next day, week, quarter, etc.426 *427 * @param DateTime $datetime Date and time.428 * @param string $time_interval Time interval, e.g. week, day, hour.429 * @param bool $reversed Going backwards in time instead of forward.430 * @return DateTime431 */432 public static function iterate( $datetime, $time_interval, $reversed = false ) {433 return call_user_func( array( __CLASS__, "next_{$time_interval}_start" ), $datetime, $reversed );434 }435 /**436 * Returns expected number of items on the page in case of date ordering.437 *438 * @param int $expected_interval_count Expected number of intervals in total.439 * @param int $items_per_page Number of items per page.440 * @param int $page_no Page number.441 *442 * @return float|int443 */444 public static function expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no ) {445 $total_pages = (int) ceil( $expected_interval_count / $items_per_page );446 if ( $page_no < $total_pages ) {447 return $items_per_page;448 } elseif ( $page_no === $total_pages ) {449 return $expected_interval_count - ( $page_no - 1 ) * $items_per_page;450 } else {451 return 0;452 }453 }454 /**455 * Returns true if there are any intervals that need to be filled in the response.456 *457 * @param int $expected_interval_count Expected number of intervals in total.458 * @param int $db_records Total number of records for given period in the database.459 * @param int $items_per_page Number of items per page.460 * @param int $page_no Page number.461 * @param string $order asc or desc.462 * @param string $order_by Column by which the result will be sorted.463 * @param int $intervals_count Number of records for given (possibly shortened) time interval.464 *465 * @return bool466 */467 public static function intervals_missing( $expected_interval_count, $db_records, $items_per_page, $page_no, $order, $order_by, $intervals_count ) {468 if ( $expected_interval_count <= $db_records ) {469 return false;470 }471 if ( 'date' === $order_by ) {472 $expected_intervals_on_page = self::expected_intervals_on_page( $expected_interval_count, $items_per_page, $page_no );473 return $intervals_count < $expected_intervals_on_page;474 }475 if ( 'desc' === $order ) {476 return $page_no > floor( $db_records / $items_per_page );477 }478 if ( 'asc' === $order ) {479 return $page_no <= ceil( ( $expected_interval_count - $db_records ) / $items_per_page );480 }481 // Invalid ordering.482 return false;483 }484 /**485 * Normalize "*_between" parameters to "*_min" and "*_max" for numeric values486 * and "*_after" and "*_before" for date values.487 *488 * @param array $request Query params from REST API request.489 * @param string|array $param_names One or more param names to handle. Should not include "_between" suffix.490 * @param bool $is_date Boolean if the param is date is related.491 * @return array Normalized query values.492 */493 public static function normalize_between_params( $request, $param_names, $is_date ) {494 if ( ! is_array( $param_names ) ) {495 $param_names = array( $param_names );496 }497 $normalized = array();498 foreach ( $param_names as $param_name ) {499 if ( ! is_array( $request[ $param_name . '_between' ] ) ) {500 continue;501 }502 $range = $request[ $param_name . '_between' ];503 if ( 2 !== count( $range ) ) {504 continue;505 }506 $min = $is_date ? '_after' : '_min';507 $max = $is_date ? '_before' : '_max';508 if ( $range[0] < $range[1] ) {509 $normalized[ $param_name . $min ] = $range[0];510 $normalized[ $param_name . $max ] = $range[1];511 } else {512 $normalized[ $param_name . $min ] = $range[1];513 $normalized[ $param_name . $max ] = $range[0];514 }515 }516 return $normalized;517 }518 /**519 * Validate a "*_between" range argument (an array with 2 numeric items).520 *521 * @param mixed $value Parameter value.522 * @param WP_REST_Request $request REST Request.523 * @param string $param Parameter name.524 * @return WP_Error|boolean525 */526 public static function rest_validate_between_numeric_arg( $value, $request, $param ) {527 if ( ! wp_is_numeric_array( $value ) ) {528 return new \WP_Error(529 'rest_invalid_param',530 /* translators: 1: parameter name */531 sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce' ), $param )532 );533 }534 if (535 2 !== count( $value ) ||536 ! is_numeric( $value[0] ) ||537 ! is_numeric( $value[1] )538 ) {539 return new \WP_Error(540 'rest_invalid_param',541 /* translators: %s: parameter name */542 sprintf( __( '%s must contain 2 numbers.', 'woocommerce' ), $param )543 );544 }545 return true;546 }547 /**548 * Validate a "*_between" range argument (an array with 2 date items).549 *550 * @param mixed $value Parameter value.551 * @param WP_REST_Request $request REST Request.552 * @param string $param Parameter name.553 * @return WP_Error|boolean554 */555 public static function rest_validate_between_date_arg( $value, $request, $param ) {556 if ( ! wp_is_numeric_array( $value ) ) {557 return new \WP_Error(558 'rest_invalid_param',559 /* translators: 1: parameter name */560 sprintf( __( '%1$s is not a numerically indexed array.', 'woocommerce' ), $param )561 );562 }563 if (564 2 !== count( $value ) ||565 ! rest_parse_date( $value[0] ) ||566 ! rest_parse_date( $value[1] )567 ) {568 return new \WP_Error(569 'rest_invalid_param',570 /* translators: %s: parameter name */571 sprintf( __( '%s must contain 2 valid dates.', 'woocommerce' ), $param )572 );573 }574 return true;575 }576}...

Full Screen

Full Screen

ImageSeeder.php

Source:ImageSeeder.php Github

copy

Full Screen

1<?php2use Illuminate\Database\Seeder;3class ImageSeeder extends Seeder4{5 /**6 * Run the database seeds.7 *8 * @return void9 */10 public function run()11 {12 DB::table('images')->insert([13 'name' => 'noimg',14 'location' => 'images/noimg.png',15 'size' => 12,16 'type' => 'image/png',17 'user_id' => 1,18 'created_at'=> new \DateTime(),19 'updated_at'=> new \DateTime()20 ]);21 DB::table('thumbnails')->insert([22 'name' => 'noimg_thumbnail',23 'location' => 'images/noimg_thumbnail.png',24 'size' => 12,25 'type' => 'image/png',26 'image_id' => 1,27 'created_at'=> new \DateTime(),28 'updated_at'=> new \DateTime()29 ]);30 DB::table('mediums')->insert([31 'name' => 'noimg_thumbnail',32 'location' => 'images/noimg.png',33 'size' => 12,34 'type' => 'image/png',35 'image_id' => 1,36 'created_at'=> new \DateTime(),37 'updated_at'=> new \DateTime()38 ]);39 //image 240 DB::table('images')->insert([41 'name' => 'Picture1',42 'location' => 'images/team/Picture1.png',43 'size' => 12,44 'type' => 'image/png',45 'user_id' => 1,46 'created_at'=> new \DateTime(),47 'updated_at'=> new \DateTime()48 ]);49 DB::table('thumbnails')->insert([50 'name' => 'Picture1_thumbnail',51 'location' => 'images/team/Picture1_thumbnail.png',52 'size' => 12,53 'type' => 'image/png',54 'image_id' => 2,55 'created_at'=> new \DateTime(),56 'updated_at'=> new \DateTime()57 ]);58 DB::table('mediums')->insert([59 'name' => 'Picture1_medium',60 'location' => 'images/team/Picture1_medium.png',61 'size' => 12,62 'type' => 'image/png',63 'image_id' => 2,64 'created_at'=> new \DateTime(),65 'updated_at'=> new \DateTime()66 ]);67 //image 368 DB::table('images')->insert([69 'name' => 'Picture2',70 'location' => 'images/team/Picture2.png',71 'size' => 12,72 'type' => 'image/png',73 'user_id' => 1,74 'created_at'=> new \DateTime(),75 'updated_at'=> new \DateTime()76 ]);77 DB::table('thumbnails')->insert([78 'name' => 'Picture2_thumbnail',79 'location' => 'images/team/Picture2_thumbnail.png',80 'size' => 12,81 'type' => 'image/png',82 'image_id' => 3,83 'created_at'=> new \DateTime(),84 'updated_at'=> new \DateTime()85 ]);86 DB::table('mediums')->insert([87 'name' => 'Picture2_medium',88 'location' => 'images/team/Picture2_medium.png',89 'size' => 12,90 'type' => 'image/png',91 'image_id' => 3,92 'created_at'=> new \DateTime(),93 'updated_at'=> new \DateTime()94 ]);95 //image 496 DB::table('images')->insert([97 'name' => 'Picture3',98 'location' => 'images/team/Picture3.png',99 'size' => 12,100 'type' => 'image/png',101 'user_id' => 1,102 'created_at'=> new \DateTime(),103 'updated_at'=> new \DateTime()104 ]);105 DB::table('thumbnails')->insert([106 'name' => 'Picture3_thumbnail',107 'location' => 'images/team/Picture3_thumbnail.png',108 'size' => 12,109 'type' => 'image/png',110 'image_id' => 4,111 'created_at'=> new \DateTime(),112 'updated_at'=> new \DateTime()113 ]);114 DB::table('mediums')->insert([115 'name' => 'Picture3_medium',116 'location' => 'images/team/Picture3_medium.png',117 'size' => 12,118 'type' => 'image/png',119 'image_id' => 4,120 'created_at'=> new \DateTime(),121 'updated_at'=> new \DateTime()122 ]);123 //image 5124 DB::table('images')->insert([125 'name' => 'Picture4',126 'location' => 'images/team/Picture4.png',127 'size' => 12,128 'type' => 'image/png',129 'user_id' => 1,130 'created_at'=> new \DateTime(),131 'updated_at'=> new \DateTime()132 ]);133 DB::table('thumbnails')->insert([134 'name' => 'Picture4_thumbnail',135 'location' => 'images/team/Picture4_thumbnail.png',136 'size' => 12,137 'type' => 'image/png',138 'image_id' => 5,139 'created_at'=> new \DateTime(),140 'updated_at'=> new \DateTime()141 ]);142 DB::table('mediums')->insert([143 'name' => 'Picture4_medium',144 'location' => 'images/team/Picture4_medium.png',145 'size' => 12,146 'type' => 'image/png',147 'image_id' => 5,148 'created_at'=> new \DateTime(),149 'updated_at'=> new \DateTime()150 ]);151 //image 6152 DB::table('images')->insert([153 'name' => 'Picture5',154 'location' => 'images/team/Picture5.png',155 'size' => 12,156 'type' => 'image/png',157 'user_id' => 1,158 'created_at'=> new \DateTime(),159 'updated_at'=> new \DateTime()160 ]);161 DB::table('thumbnails')->insert([162 'name' => 'Picture5_thumbnail',163 'location' => 'images/team/Picture5_thumbnail.png',164 'size' => 12,165 'type' => 'image/png',166 'image_id' => 6,167 'created_at'=> new \DateTime(),168 'updated_at'=> new \DateTime()169 ]);170 DB::table('mediums')->insert([171 'name' => 'Picture5_medium',172 'location' => 'images/team/Picture5_medium.png',173 'size' => 12,174 'type' => 'image/png',175 'image_id' => 6,176 'created_at'=> new \DateTime(),177 'updated_at'=> new \DateTime()178 ]);179 //image 7180 DB::table('images')->insert([181 'name' => 'Picture7',182 'location' => 'images/team/Picture7.png',183 'size' => 12,184 'type' => 'image/png',185 'user_id' => 1,186 'created_at'=> new \DateTime(),187 'updated_at'=> new \DateTime()188 ]);189 DB::table('thumbnails')->insert([190 'name' => 'Picture7_thumbnail',191 'location' => 'images/team/Picture7_thumbnail.png',192 'size' => 12,193 'type' => 'image/png',194 'image_id' => 7,195 'created_at'=> new \DateTime(),196 'updated_at'=> new \DateTime()197 ]);198 DB::table('mediums')->insert([199 'name' => 'Picture7_medium',200 'location' => 'images/team/Picture7_medium.png',201 'size' => 12,202 'type' => 'image/png',203 'image_id' => 7,204 'created_at'=> new \DateTime(),205 'updated_at'=> new \DateTime()206 ]);207 //image 8208 DB::table('images')->insert([209 'name' => 'Picture6',210 'location' => 'images/team/Picture6.png',211 'size' => 12,212 'type' => 'image/png',213 'user_id' => 1,214 'created_at'=> new \DateTime(),215 'updated_at'=> new \DateTime()216 ]);217 DB::table('thumbnails')->insert([218 'name' => 'Picture6_thumbnail',219 'location' => 'images/team/Picture6_thumbnail.png',220 'size' => 12,221 'type' => 'image/png',222 'image_id' => 8,223 'created_at'=> new \DateTime(),224 'updated_at'=> new \DateTime()225 ]);226 DB::table('mediums')->insert([227 'name' => 'Picture6_medium',228 'location' => 'images/team/Picture6_medium.png',229 'size' => 12,230 'type' => 'image/png',231 'image_id' => 8,232 'created_at'=> new \DateTime(),233 'updated_at'=> new \DateTime()234 ]);235 236 //image 9237 // DB::table('images')->insert([238 // 'name' => 'Pictureproject1',239 // 'location' => 'images/projects/Picture1.png',240 // 'size' => 12,241 // 'type' => 'image/png',242 // 'user_id' => 1,243 // 'created_at'=> new \DateTime(),244 // 'updated_at'=> new \DateTime()245 // ]);246 // DB::table('thumbnails')->insert([247 // 'name' => 'Pictureproject1_thumbnail',248 // 'location' => 'images/projects/Picture1_thumbnail.png',249 // 'size' => 12,250 // 'type' => 'image/png',251 // 'image_id' => 9,252 // 'created_at'=> new \DateTime(),253 // 'updated_at'=> new \DateTime()254 // ]);255 // DB::table('mediums')->insert([256 // 'name' => 'Pictureproject1_medium',257 // 'location' => 'images/projects/picture1_medium.png',258 // 'size' => 12,259 // 'type' => 'image/png',260 // 'image_id' => 9,261 // 'created_at'=> new \DateTime(),262 // 'updated_at'=> new \DateTime()263 // ]);264 //image 9265 // DB::table('images')->insert([266 // 'name' => 'Pictureproject2',267 // 'location' => 'images/projects/Picture2.png',268 // 'size' => 12,269 // 'type' => 'image/png',270 // 'user_id' => 1,271 // 'created_at'=> new \DateTime(),272 // 'updated_at'=> new \DateTime()273 // ]);274 // DB::table('thumbnails')->insert([275 // 'name' => 'Pictureproject2_thumbnail',276 // 'location' => 'images/projects/Picture2_thumbnail.png',277 // 'size' => 12,278 // 'type' => 'image/png',279 // 'image_id' => 9,280 // 'created_at'=> new \DateTime(),281 // 'updated_at'=> new \DateTime()282 // ]);283 // DB::table('mediums')->insert([284 // 'name' => 'Pictureproject2_medium',285 // 'location' => 'images/projects/Picture2_medium.png',286 // 'size' => 12,287 // 'type' => 'image/png',288 // 'image_id' => 9,289 // 'created_at'=> new \DateTime(),290 // 'updated_at'=> new \DateTime()291 // ]);292 //image 10293 // DB::table('images')->insert([294 // 'name' => 'Pictureproject3',295 // 'location' => 'images/projects/Picture3.png',296 // 'size' => 12,297 // 'type' => 'image/png',298 // 'user_id' => 1,299 // 'created_at'=> new \DateTime(),300 // 'updated_at'=> new \DateTime()301 // ]);302 // DB::table('thumbnails')->insert([303 // 'name' => 'Pictureproject3_thumbnail',304 // 'location' => 'images/projects/Picture3_thumbnail.png',305 // 'size' => 12,306 // 'type' => 'image/png',307 // 'image_id' => 10,308 // 'created_at'=> new \DateTime(),309 // 'updated_at'=> new \DateTime()310 // ]);311 // DB::table('mediums')->insert([312 // 'name' => 'Pictureproject3_medium',313 // 'location' => 'images/projects/Picture3_medium.png',314 // 'size' => 12,315 // 'type' => 'image/png',316 // 'image_id' => 10,317 // 'created_at'=> new \DateTime(),318 // 'updated_at'=> new \DateTime()319 // ]);320 //image 9321 DB::table('images')->insert([322 'name' => 'Pictureproject4',323 'location' => 'images/projects/Picture4.png',324 'size' => 12,325 'type' => 'image/png',326 'user_id' => 1,327 'created_at'=> new \DateTime(),328 'updated_at'=> new \DateTime()329 ]);330 DB::table('thumbnails')->insert([331 'name' => 'Pictureproject4_thumbnail',332 'location' => 'images/projects/Picture4_thumbnail.png',333 'size' => 12,334 'type' => 'image/png',335 'image_id' => 9,336 'created_at'=> new \DateTime(),337 'updated_at'=> new \DateTime()338 ]);339 DB::table('mediums')->insert([340 'name' => 'Pictureproject4_medium',341 'location' => 'images/projects/Picture4_medium.png',342 'size' => 12,343 'type' => 'image/png',344 'image_id' => 9,345 'created_at'=> new \DateTime(),346 'updated_at'=> new \DateTime()347 ]);348 //image 10349 DB::table('images')->insert([350 'name' => 'Pictureproject5',351 'location' => 'images/projects/Picture5.png',352 'size' => 12,353 'type' => 'image/png',354 'user_id' => 1,355 'created_at'=> new \DateTime(),356 'updated_at'=> new \DateTime()357 ]);358 DB::table('thumbnails')->insert([359 'name' => 'Pictureproject5_thumbnail',360 'location' => 'images/projects/Picture5_thumbnail.png',361 'size' => 12,362 'type' => 'image/png',363 'image_id' => 10,364 'created_at'=> new \DateTime(),365 'updated_at'=> new \DateTime()366 ]);367 DB::table('mediums')->insert([368 'name' => 'Pictureproject5_medium',369 'location' => 'images/projects/Picture5_medium.png',370 'size' => 12,371 'type' => 'image/png',372 'image_id' => 10,373 'created_at'=> new \DateTime(),374 'updated_at'=> new \DateTime()375 ]);376 //image 11377 DB::table('images')->insert([378 'name' => 'Pictureproject6',379 'location' => 'images/projects/Picture6.png',380 'size' => 12,381 'type' => 'image/png',382 'user_id' => 1,383 'created_at'=> new \DateTime(),384 'updated_at'=> new \DateTime()385 ]);386 DB::table('thumbnails')->insert([387 'name' => 'Pictureproject6_thumbnail',388 'location' => 'images/projects/Picture6_thumbnail.png',389 'size' => 12,390 'type' => 'image/png',391 'image_id' => 11,392 'created_at'=> new \DateTime(),393 'updated_at'=> new \DateTime()394 ]);395 DB::table('mediums')->insert([396 'name' => 'Pictureproject6_medium',397 'location' => 'images/projects/Picture6_medium.png',398 'size' => 12,399 'type' => 'image/png',400 'image_id' => 11,401 'created_at'=> new \DateTime(),402 'updated_at'=> new \DateTime()403 ]);404 //image 12405 DB::table('images')->insert([406 'name' => 'Pictureproject7',407 'location' => 'images/projects/Picture7.png',408 'size' => 12,409 'type' => 'image/png',410 'user_id' => 1,411 'created_at'=> new \DateTime(),412 'updated_at'=> new \DateTime()413 ]);414 DB::table('thumbnails')->insert([415 'name' => 'Pictureproject7_thumbnail',416 'location' => 'images/projects/Picture7_thumbnail.png',417 'size' => 12,418 'type' => 'image/png',419 'image_id' => 12,420 'created_at'=> new \DateTime(),421 'updated_at'=> new \DateTime()422 ]);423 DB::table('mediums')->insert([424 'name' => 'Pictureproject7_medium',425 'location' => 'images/projects/Picture7_medium.png',426 'size' => 12,427 'type' => 'image/png',428 'image_id' => 12,429 'created_at'=> new \DateTime(),430 'updated_at'=> new \DateTime()431 ]);432 //image 13433 DB::table('images')->insert([434 'name' => 'Pictureproject8',435 'location' => 'images/projects/Picture8.png',436 'size' => 12,437 'type' => 'image/png',438 'user_id' => 1,439 'created_at'=> new \DateTime(),440 'updated_at'=> new \DateTime()441 ]);442 DB::table('thumbnails')->insert([443 'name' => 'Pictureproject8_thumbnail',444 'location' => 'images/projects/Picture8_thumbnail.png',445 'size' => 12,446 'type' => 'image/png',447 'image_id' => 13,448 'created_at'=> new \DateTime(),449 'updated_at'=> new \DateTime()450 ]);451 DB::table('mediums')->insert([452 'name' => 'Pictureproject8_medium',453 'location' => 'images/projects/Picture8_medium.png',454 'size' => 12,455 'type' => 'image/png',456 'image_id' => 13,457 'created_at'=> new \DateTime(),458 'updated_at'=> new \DateTime()459 ]);460 //image 14461 DB::table('images')->insert([462 'name' => 'Pictureproject9',463 'location' => 'images/projects/Picture9.png',464 'size' => 12,465 'type' => 'image/png',466 'user_id' => 1,467 'created_at'=> new \DateTime(),468 'updated_at'=> new \DateTime()469 ]);470 DB::table('thumbnails')->insert([471 'name' => 'Pictureproject9_thumbnail',472 'location' => 'images/projects/Picture9_thumbnail.png',473 'size' => 12,474 'type' => 'image/png',475 'image_id' => 14,476 'created_at'=> new \DateTime(),477 'updated_at'=> new \DateTime()478 ]);479 DB::table('mediums')->insert([480 'name' => 'Pictureproject9_medium',481 'location' => 'images/projects/Picture9_medium.png',482 'size' => 12,483 'type' => 'image/png',484 'image_id' => 14,485 'created_at'=> new \DateTime(),486 'updated_at'=> new \DateTime()487 ]);488 }489}...

Full Screen

Full Screen

DateTimeItemTest.php

Source:DateTimeItemTest.php Github

copy

Full Screen

1<?php2namespace Drupal\Tests\datetime\Kernel;3use Drupal\Core\Field\FieldItemListInterface;4use Drupal\Core\Field\FieldItemInterface;5use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem;6use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;7use Drupal\entity_test\Entity\EntityTest;8use Drupal\field\Entity\FieldConfig;9use Drupal\Tests\field\Kernel\FieldKernelTestBase;10use Drupal\field\Entity\FieldStorageConfig;11/**12 * Tests the new entity API for the date field type.13 *14 * @group datetime15 */16class DateTimeItemTest extends FieldKernelTestBase {17 /**18 * A field storage to use in this test class.19 *20 * @var \Drupal\field\Entity\FieldStorageConfig21 */22 protected $fieldStorage;23 /**24 * The field used in this test class.25 *26 * @var \Drupal\field\Entity\FieldConfig27 */28 protected $field;29 /**30 * Modules to enable.31 *32 * @var array33 */34 public static $modules = ['datetime'];35 protected function setUp() {36 parent::setUp();37 // Create a field with settings to validate.38 $this->fieldStorage = FieldStorageConfig::create([39 'field_name' => 'field_datetime',40 'type' => 'datetime',41 'entity_type' => 'entity_test',42 'settings' => ['datetime_type' => DateTimeItem::DATETIME_TYPE_DATETIME],43 ]);44 $this->fieldStorage->save();45 $this->field = FieldConfig::create([46 'field_storage' => $this->fieldStorage,47 'bundle' => 'entity_test',48 'settings' => [49 'default_value' => 'blank',50 ],51 ]);52 $this->field->save();53 }54 /**55 * Tests using entity fields of the datetime field type.56 */57 public function testDateTime() {58 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);59 $this->fieldStorage->save();60 // Verify entity creation.61 $entity = EntityTest::create();62 $value = '2014-01-01T20:00:00';63 $entity->field_datetime = $value;64 $entity->name->value = $this->randomMachineName();65 $this->entityValidateAndSave($entity);66 // Verify entity has been created properly.67 $id = $entity->id();68 $entity = EntityTest::load($id);69 $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');70 $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');71 $this->assertEqual($entity->field_datetime->value, $value);72 $this->assertEqual($entity->field_datetime[0]->value, $value);73 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());74 // Verify changing the date value.75 $new_value = '2016-11-04T00:21:00';76 $entity->field_datetime->value = $new_value;77 $this->assertEqual($entity->field_datetime->value, $new_value);78 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());79 // Read changed entity and assert changed values.80 $this->entityValidateAndSave($entity);81 $entity = EntityTest::load($id);82 $this->assertEqual($entity->field_datetime->value, $new_value);83 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());84 // Test the generateSampleValue() method.85 $entity = EntityTest::create();86 $entity->field_datetime->generateSampleItems();87 $this->entityValidateAndSave($entity);88 }89 /**90 * Tests using entity fields of the date field type.91 */92 public function testDateOnly() {93 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);94 $this->fieldStorage->save();95 // Verify entity creation.96 $entity = EntityTest::create();97 $value = '2014-01-01';98 $entity->field_datetime = $value;99 $entity->name->value = $this->randomMachineName();100 $this->entityValidateAndSave($entity);101 // Verify entity has been created properly.102 $id = $entity->id();103 $entity = EntityTest::load($id);104 $this->assertTrue($entity->field_datetime instanceof FieldItemListInterface, 'Field implements interface.');105 $this->assertTrue($entity->field_datetime[0] instanceof FieldItemInterface, 'Field item implements interface.');106 $this->assertEqual($entity->field_datetime->value, $value);107 $this->assertEqual($entity->field_datetime[0]->value, $value);108 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());109 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));110 $entity->field_datetime->date->setDefaultDateTime();111 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));112 // Verify changing the date value.113 $new_value = '2016-11-04';114 $entity->field_datetime->value = $new_value;115 $this->assertEqual($entity->field_datetime->value, $new_value);116 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());117 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));118 $entity->field_datetime->date->setDefaultDateTime();119 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));120 // Read changed entity and assert changed values.121 $this->entityValidateAndSave($entity);122 $entity = EntityTest::load($id);123 $this->assertEqual($entity->field_datetime->value, $new_value);124 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());125 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));126 $entity->field_datetime->date->setDefaultDateTime();127 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));128 // Test the generateSampleValue() method.129 $entity = EntityTest::create();130 $entity->field_datetime->generateSampleItems();131 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));132 $entity->field_datetime->date->setDefaultDateTime();133 $this->assertEquals('12:00:00', $entity->field_datetime->date->format('H:i:s'));134 $this->entityValidateAndSave($entity);135 }136 /**137 * Tests DateTimeItem::setValue().138 */139 public function testSetValue() {140 // Test a date+time field.141 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);142 $this->fieldStorage->save();143 // Test DateTimeItem::setValue() using string.144 $entity = EntityTest::create();145 $value = '2014-01-01T20:00:00';146 $entity->get('field_datetime')->set(0, $value);147 $this->entityValidateAndSave($entity);148 // Load the entity and ensure the field was saved correctly.149 $id = $entity->id();150 $entity = EntityTest::load($id);151 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');152 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());153 // Test DateTimeItem::setValue() using property array.154 $entity = EntityTest::create();155 $value = '2014-01-01T20:00:00';156 $entity->set('field_datetime', $value);157 $this->entityValidateAndSave($entity);158 // Load the entity and ensure the field was saved correctly.159 $id = $entity->id();160 $entity = EntityTest::load($id);161 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');162 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());163 // Test a date-only field.164 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);165 $this->fieldStorage->save();166 // Test DateTimeItem::setValue() using string.167 $entity = EntityTest::create();168 $value = '2014-01-01';169 $entity->get('field_datetime')->set(0, $value);170 $this->entityValidateAndSave($entity);171 // Load the entity and ensure the field was saved correctly.172 $id = $entity->id();173 $entity = EntityTest::load($id);174 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with string value.');175 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());176 // Test DateTimeItem::setValue() using property array.177 $entity = EntityTest::create();178 $value = '2014-01-01';179 $entity->set('field_datetime', $value);180 $this->entityValidateAndSave($entity);181 // Load the entity and ensure the field was saved correctly.182 $id = $entity->id();183 $entity = EntityTest::load($id);184 $this->assertEqual($entity->field_datetime[0]->value, $value, 'DateTimeItem::setValue() works with array value.');185 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());186 }187 /**188 * Tests setting the value of the DateTimeItem directly.189 */190 public function testSetValueProperty() {191 // Test Date::setValue() with a date+time field.192 // Test a date+time field.193 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);194 $this->fieldStorage->save();195 $entity = EntityTest::create();196 $value = '2014-01-01T20:00:00';197 $entity->set('field_datetime', $value);198 $this->entityValidateAndSave($entity);199 // Load the entity and ensure the field was saved correctly.200 $id = $entity->id();201 $entity = EntityTest::load($id);202 $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');203 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());204 // Test Date::setValue() with a date-only field.205 // Test a date+time field.206 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);207 $this->fieldStorage->save();208 $entity = EntityTest::create();209 $value = '2014-01-01';210 $entity->set('field_datetime', $value);211 $this->entityValidateAndSave($entity);212 // Load the entity and ensure the field was saved correctly.213 $id = $entity->id();214 $entity = EntityTest::load($id);215 $this->assertEqual($entity->field_datetime[0]->value, $value, '"Value" property can be set directly.');216 $this->assertEquals(DateTimeItemInterface::STORAGE_TIMEZONE, $entity->field_datetime->date->getTimeZone()->getName());217 }218 /**219 * Tests the constraint validations for fields with date and time.220 *221 * @dataProvider datetimeValidationProvider222 */223 public function testDatetimeValidation($value) {224 $this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);225 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATETIME);226 $this->fieldStorage->save();227 $entity = EntityTest::create();228 $entity->set('field_datetime', $value);229 $this->entityValidateAndSave($entity);230 }231 /**232 * Provider for testDatetimeValidation().233 */234 public function datetimeValidationProvider() {235 return [236 // Valid ISO 8601 dates, but unsupported by DateTimeItem.237 ['2014-01-01T20:00:00Z'],238 ['2014-01-01T20:00:00+04:00'],239 ['2014-01-01T20:00:00+0400'],240 ['2014-01-01T20:00:00+04'],241 ['2014-01-01T20:00:00.123'],242 ['2014-01-01T200000'],243 ['2014-01-01T2000'],244 ['2014-01-01T20'],245 ['20140101T20:00:00'],246 ['2014-01T20:00:00'],247 ['2014-001T20:00:00'],248 ['2014001T20:00:00'],249 // Valid date strings, but unsupported by DateTimeItem.250 ['2016-11-03 20:52:00'],251 ['Thu, 03 Nov 2014 20:52:00 -0400'],252 ['Thursday, November 3, 2016 - 20:52'],253 ['Thu, 11/03/2016 - 20:52'],254 ['11/03/2016 - 20:52'],255 // Invalid date strings.256 ['YYYY-01-01T20:00:00'],257 ['2014-MM-01T20:00:00'],258 ['2014-01-DDT20:00:00'],259 ['2014-01-01Thh:00:00'],260 ['2014-01-01T20:mm:00'],261 ['2014-01-01T20:00:ss'],262 // Invalid dates.263 ['2014-13-13T20:00:00'],264 ['2014-01-55T20:00:00'],265 ['2014-01-01T25:00:00'],266 ['2014-01-01T00:70:00'],267 ['2014-01-01T00:00:70'],268 // Proper format for different field setting.269 ['2014-01-01'],270 // Wrong input type.271 [['2014', '01', '01', '00', '00', '00']],272 ];273 }274 /**275 * Tests the constraint validations for fields with date only.276 *277 * @dataProvider dateonlyValidationProvider278 */279 public function testDateonlyValidation($value) {280 $this->setExpectedException(\PHPUnit_Framework_AssertionFailedError::class);281 $this->fieldStorage->setSetting('datetime_type', DateTimeItem::DATETIME_TYPE_DATE);282 $this->fieldStorage->save();283 $entity = EntityTest::create();284 $entity->set('field_datetime', $value);285 $this->entityValidateAndSave($entity);286 }287 /**288 * Provider for testDatetimeValidation().289 */290 public function dateonlyValidationProvider() {291 return [292 // Valid date strings, but unsupported by DateTimeItem.293 ['Thu, 03 Nov 2014'],294 ['Thursday, November 3, 2016'],295 ['Thu, 11/03/2016'],296 ['11/03/2016'],297 // Invalid date strings.298 ['YYYY-01-01'],299 ['2014-MM-01'],300 ['2014-01-DD'],301 // Invalid dates.302 ['2014-13-01'],303 ['2014-01-55'],304 // Proper format for different field setting.305 ['2014-01-01T20:00:00'],306 // Wrong input type.307 [['2014', '01', '01']],308 ];309 }310}...

Full Screen

Full Screen

dateTime

Using AI Code Generation

copy

Full Screen

1use mageekguy\atoum\asserter\dateTime as atoumDateTime;2use mageekguy\atoum\asserters\dateTime as assertersDateTime;3use mageekguy\atoum\asserters\variable as assertersVariable;4use mageekguy\atoum\asserters\phpString as assertersPhpString;5use mageekguy\atoum\asserters\phpArray as assertersPhpArray;6use mageekguy\atoum\asserters\phpObject as assertersPhpObject;7use mageekguy\atoum\asserters\phpResource as assertersPhpResource;8use mageekguy\atoum\asserters\phpFloat as assertersPhpFloat;9use mageekguy\atoum\asserters\phpInteger as assertersPhpInteger;10use mageekguy\atoum\asserters\phpBoolean as assertersPhpBoolean;11use mageekguy\atoum\asserters\phpNull as assertersPhpNull;12use mageekguy\atoum\asserters\phpClass as assertersPhpClass;13use mageekguy\atoum\asserters\phpFunction as assertersPhpFunction;14use mageekguy\atoum\asserters\phpConstant as assertersPhpConstant;15use mageekguy\atoum\asserters\phpCall as assertersPhpCall;16use mageekguy\atoum\asserters\phpFunction as assertersPhpFunction;17use mageekguy\atoum\asserters\variable as assertersVariable;18use mageekguy\atoum\asserters\adapter as assertersAdapter;19use mageekguy\atoum\asserters\exception as assertersException;20use mageekguy\atoum\asserters\castToString as assertersCastToString;21use mageekguy\atoum\asserters\castToArray as assertersCastToArray;22use mageekguy\atoum\asserters\castToBoolean as assertersCastToBoolean;23use mageekguy\atoum\asserters\castToFloat as assertersCastToFloat;

Full Screen

Full Screen

dateTime

Using AI Code Generation

copy

Full Screen

1$dateTime = new \DateTime();2$dateTime->setTimezone(new \DateTimeZone('UTC'));3$this->dateTime = $dateTime;4$dateTime = new \DateTime();5$dateTime->setTimezone(new \DateTimeZone('UTC'));6$this->dateTime = $dateTime;7$dateTime = new \DateTime();8$dateTime->setTimezone(new \DateTimeZone('UTC'));9$this->dateTime = $dateTime;10$dateTime = new \DateTime();11$dateTime->setTimezone(new \DateTimeZone('UTC'));12$this->dateTime = $dateTime;13$dateTime = new \DateTime();14$dateTime->setTimezone(new \DateTimeZone('UTC'));15$this->dateTime = $dateTime;16$dateTime = new \DateTime();17$dateTime->setTimezone(new \DateTimeZone('UTC'));18$this->dateTime = $dateTime;19{20 public function test1()21 {22 $dateTime = new \DateTime();23 $dateTime->setTimezone(new \DateTimeZone('UTC'));24 $this->dateTime = $dateTime;25 }26 public function test2()27 {28 $dateTime = new \DateTime();29 $dateTime->setTimezone(new \DateTimeZone('UTC'));30 $this->dateTime = $dateTime;31 }32 public function test3()33 {34 $dateTime = new \DateTime();35 $dateTime->setTimezone(new \DateTimeZone('UTC'));36 $this->dateTime = $dateTime;37 }38}39require_once __DIR__ . '/vendor/autoload.php';40require_once __DIR__ . '/vendor/atoum/atoum/scripts/runner.php';

Full Screen

Full Screen

dateTime

Using AI Code Generation

copy

Full Screen

1$dateTime = new dateTime();2$dateTime->setTimezone(new dateTimeZone('America/New_York'));3$dateTime->setDate(2015, 1, 1);4$dateTime->setTime(12, 0, 0);5var_dump($dateTime->format('Y-m-d H:i:s'));6$dateTime = new dateTime();7$dateTime->setTimezone(new dateTimeZone('America/New_York'));8$dateTime->setDate(2015, 1, 1);9$dateTime->setTime(12, 0, 0);10var_dump($dateTime->format('Y-m-d H:i:s'));11string(19) "2015-01-01 12:00:00"12string(19) "2015-01-01 17:00:00"

Full Screen

Full Screen

dateTime

Using AI Code Generation

copy

Full Screen

1$dateTime = new \DateTime('now');2$dateTime->setTimezone(new \DateTimeZone('UTC'));3$this->dateTime = $dateTime;4$dateTime = new \DateTime('now');5$dateTime->setTimezone(new \DateTimeZone('UTC'));6$this->dateTime = $dateTime;7$dateTime = new \DateTime('now');8$dateTime->setTimezone(new \DateTimeZone('UTC'));9$this->dateTime = $dateTime;10$dateTime = new \DateTime('now');11$dateTime->setTimezone(new \DateTimeZone('UTC'));12$this->dateTime = $dateTime;13$dateTime = new \DateTime('now');14$dateTime->setTimezone(new \DateTimeZone('UTC'));15$this->dateTime = $dateTime;16$dateTime = new \DateTime('now');17$dateTime->setTimezone(new \DateTimeZone('UTC'));18$this->dateTime = $dateTime;19require_once 'vendor/autoload.php';20$runner = new \mageekguy\atoum\runner();21$runner->addTestsFromDirectory('tests');22$script->getRunner()->addTestsFromDirectory('tests');23$runner->run();24$runner->addExtension(new \atoum\autoloop\extension($script));25namespace tests\units;26{27 public function test1()28 {29 $dateTime = new \DateTime('now');30 $dateTime->setTimezone(new \DateTimeZone('UTC'));31 $this->dateTime = $dateTime;32 }33}

Full Screen

Full Screen

dateTime

Using AI Code Generation

copy

Full Screen

1require_once 'php/classes/dateTime.class.php';2$dateTime = new dateTime();3echo $dateTime->date('d/m/Y');4require_once 'php/classes/dateTime.class.php';5$dateTime = new dateTime();6echo $dateTime->date('d/m/Y');7require_once 'php/classes/dateTime.class.php';8$dateTime = new dateTime();9echo $dateTime->date('d/m/Y');10require_once 'php/classes/dateTime.class.php';11$dateTime = new dateTime();12echo $dateTime->date('d/m/Y');13require_once 'php/classes/dateTime.class.php';14$dateTime = new dateTime();15echo $dateTime->date('d/m/Y');16require_once 'php/classes/dateTime.class.php';17$dateTime = new dateTime();18echo $dateTime->date('d/m/Y');19require_once 'php/classes/dateTime.class.php';20$dateTime = new dateTime();21echo $dateTime->date('d/m/Y');

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Atoum automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful