<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[36347] trunk/tests/phpunit/includes: Build/Test Tools: Move PHP factory classes into their own files.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/36347">36347</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/36347","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>ericlewis</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-01-19 03:55:19 +0000 (Tue, 19 Jan 2016)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Build/Test Tools: Move PHP factory classes into their own files.
This makes the code easier to browse.
`factory.php` loads the new files, so this is backwards compatible in case `factory.php` is loaded directly for access to one of the classes.
See <a href="https://core.trac.wordpress.org/ticket/35492">#35492</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunitincludesfactoryphp">trunk/tests/phpunit/includes/factory.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li>trunk/tests/phpunit/includes/factory/</li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactorycallbackaftercreatephp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforattachmentphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforblogphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforcommentphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryfornetworkphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforpostphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryfortermphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforthingphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestfactoryforuserphp">trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php</a></li>
<li><a href="#trunktestsphpunitincludesfactoryclasswpunittestgeneratorsequencephp">trunk/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactorycallbackaftercreatephpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-callback-after-create.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,13 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_Callback_After_Create {
+ var $callback;
+
+ function __construct( $callback ) {
+ $this->callback = $callback;
+ }
+
+ function call( $object ) {
+ return call_user_func( $this->callback, $object );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforattachmentphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-attachment.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,37 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
+
+ function create_object( $file, $parent = 0, $args = array() ) {
+ return wp_insert_attachment( $args, $file, $parent );
+ }
+
+ function create_upload_object( $file, $parent = 0 ) {
+ $contents = file_get_contents($file);
+ $upload = wp_upload_bits(basename($file), null, $contents);
+
+ $type = '';
+ if ( ! empty($upload['type']) ) {
+ $type = $upload['type'];
+ } else {
+ $mime = wp_check_filetype( $upload['file'] );
+ if ($mime)
+ $type = $mime['type'];
+ }
+
+ $attachment = array(
+ 'post_title' => basename( $upload['file'] ),
+ 'post_content' => '',
+ 'post_type' => 'attachment',
+ 'post_parent' => $parent,
+ 'post_mime_type' => $type,
+ 'guid' => $upload[ 'url' ],
+ );
+
+ // Save the data
+ $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
+ wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
+
+ return $id;
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforblogphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-blog.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,36 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
+
+ function __construct( $factory = null ) {
+ global $current_site, $base;
+ parent::__construct( $factory );
+ $this->default_generation_definitions = array(
+ 'domain' => $current_site->domain,
+ 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
+ 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
+ 'site_id' => $current_site->id,
+ );
+ }
+
+ function create_object( $args ) {
+ global $wpdb;
+ $meta = isset( $args['meta'] ) ? $args['meta'] : array();
+ $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
+ // temp tables will trigger db errors when we attempt to reference them as new temp tables
+ $suppress = $wpdb->suppress_errors();
+ $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
+ $wpdb->suppress_errors( $suppress );
+
+ // Tell WP we're done installing.
+ wp_installing( false );
+
+ return $blog;
+ }
+
+ function update_object( $blog_id, $fields ) {}
+
+ function get_object_by_id( $blog_id ) {
+ return get_blog_details( $blog_id, false );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforcommentphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-comment.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,32 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
+
+ function __construct( $factory = null ) {
+ parent::__construct( $factory );
+ $this->default_generation_definitions = array(
+ 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
+ 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
+ 'comment_approved' => 1,
+ 'comment_content' => 'This is a comment'
+ );
+ }
+
+ function create_object( $args ) {
+ return wp_insert_comment( $this->addslashes_deep( $args ) );
+ }
+
+ function update_object( $comment_id, $fields ) {
+ $fields['comment_ID'] = $comment_id;
+ return wp_update_comment( $this->addslashes_deep( $fields ) );
+ }
+
+ function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
+ $args['comment_post_ID'] = $post_id;
+ return $this->create_many( $count, $args, $generation_definitions );
+ }
+
+ function get_object_by_id( $comment_id ) {
+ return get_comment( $comment_id );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryfornetworkphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-network.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,34 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
+
+ function __construct( $factory = null ) {
+ parent::__construct( $factory );
+ $this->default_generation_definitions = array(
+ 'domain' => WP_TESTS_DOMAIN,
+ 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
+ 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
+ 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
+ 'subdomain_install' => false,
+ );
+ }
+
+ function create_object( $args ) {
+ require_once ABSPATH . 'wp-admin/includes/upgrade.php';
+
+ if ( ! isset( $args['user'] ) ) {
+ $email = WP_TESTS_EMAIL;
+ } else {
+ $email = get_userdata( $args['user'] )->user_email;
+ }
+
+ populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
+ return $args['network_id'];
+ }
+
+ function update_object( $network_id, $fields ) {}
+
+ function get_object_by_id( $network_id ) {
+ return wp_get_network( $network_id );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforpostphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-post.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,28 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
+
+ function __construct( $factory = null ) {
+ parent::__construct( $factory );
+ $this->default_generation_definitions = array(
+ 'post_status' => 'publish',
+ 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
+ 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
+ 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
+ 'post_type' => 'post'
+ );
+ }
+
+ function create_object( $args ) {
+ return wp_insert_post( $args );
+ }
+
+ function update_object( $post_id, $fields ) {
+ $fields['ID'] = $post_id;
+ return wp_update_post( $fields );
+ }
+
+ function get_object_by_id( $post_id ) {
+ return get_post( $post_id );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryfortermphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-term.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,47 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
+
+ private $taxonomy;
+ const DEFAULT_TAXONOMY = 'post_tag';
+
+ function __construct( $factory = null, $taxonomy = null ) {
+ parent::__construct( $factory );
+ $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
+ $this->default_generation_definitions = array(
+ 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
+ 'taxonomy' => $this->taxonomy,
+ 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
+ );
+ }
+
+ function create_object( $args ) {
+ $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
+ $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
+ if ( is_wp_error( $term_id_pair ) )
+ return $term_id_pair;
+ return $term_id_pair['term_id'];
+ }
+
+ function update_object( $term, $fields ) {
+ $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
+ if ( is_object( $term ) )
+ $taxonomy = $term->taxonomy;
+ $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
+ return $term_id_pair['term_id'];
+ }
+
+ function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
+ return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
+ }
+
+ function create_and_get( $args = array(), $generation_definitions = null ) {
+ $term_id = $this->create( $args, $generation_definitions );
+ $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
+ return get_term( $term_id, $taxonomy );
+ }
+
+ function get_object_by_id( $term_id ) {
+ return get_term( $term_id, $this->taxonomy );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforthingphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-thing.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,105 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+abstract class WP_UnitTest_Factory_For_Thing {
+
+ var $default_generation_definitions;
+ var $factory;
+
+ /**
+ * Creates a new factory, which will create objects of a specific Thing
+ *
+ * @param object $factory Global factory that can be used to create other objects on the system
+ * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
+ * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
+ * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
+ */
+ function __construct( $factory, $default_generation_definitions = array() ) {
+ $this->factory = $factory;
+ $this->default_generation_definitions = $default_generation_definitions;
+ }
+
+ abstract function create_object( $args );
+ abstract function update_object( $object, $fields );
+
+ function create( $args = array(), $generation_definitions = null ) {
+ if ( is_null( $generation_definitions ) )
+ $generation_definitions = $this->default_generation_definitions;
+
+ $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
+ $created = $this->create_object( $generated_args );
+ if ( !$created || is_wp_error( $created ) )
+ return $created;
+
+ if ( $callbacks ) {
+ $updated_fields = $this->apply_callbacks( $callbacks, $created );
+ $save_result = $this->update_object( $created, $updated_fields );
+ if ( !$save_result || is_wp_error( $save_result ) )
+ return $save_result;
+ }
+ return $created;
+ }
+
+ function create_and_get( $args = array(), $generation_definitions = null ) {
+ $object_id = $this->create( $args, $generation_definitions );
+ return $this->get_object_by_id( $object_id );
+ }
+
+ abstract function get_object_by_id( $object_id );
+
+ function create_many( $count, $args = array(), $generation_definitions = null ) {
+ $results = array();
+ for ( $i = 0; $i < $count; $i++ ) {
+ $results[] = $this->create( $args, $generation_definitions );
+ }
+ return $results;
+ }
+
+ function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
+ $callbacks = array();
+ if ( is_null( $generation_definitions ) )
+ $generation_definitions = $this->default_generation_definitions;
+
+ foreach( array_keys( $generation_definitions ) as $field_name ) {
+ if ( !isset( $args[$field_name] ) ) {
+ $generator = $generation_definitions[$field_name];
+ if ( is_scalar( $generator ) )
+ $args[$field_name] = $generator;
+ elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
+ $callbacks[$field_name] = $generator;
+ } elseif ( is_object( $generator ) )
+ $args[$field_name] = $generator->next();
+ else
+ return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
+ }
+ }
+ return $args;
+ }
+
+ function apply_callbacks( $callbacks, $created ) {
+ $updated_fields = array();
+ foreach( $callbacks as $field_name => $generator ) {
+ $updated_fields[$field_name] = $generator->call( $created );
+ }
+ return $updated_fields;
+ }
+
+ function callback( $function ) {
+ return new WP_UnitTest_Factory_Callback_After_Create( $function );
+ }
+
+ function addslashes_deep($value) {
+ if ( is_array( $value ) ) {
+ $value = array_map( array( $this, 'addslashes_deep' ), $value );
+ } elseif ( is_object( $value ) ) {
+ $vars = get_object_vars( $value );
+ foreach ($vars as $key=>$data) {
+ $value->{$key} = $this->addslashes_deep( $data );
+ }
+ } elseif ( is_string( $value ) ) {
+ $value = addslashes( $value );
+ }
+
+ return $value;
+ }
+
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestfactoryforuserphpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-factory-for-user.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,26 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
+
+ function __construct( $factory = null ) {
+ parent::__construct( $factory );
+ $this->default_generation_definitions = array(
+ 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
+ 'user_pass' => 'password',
+ 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
+ );
+ }
+
+ function create_object( $args ) {
+ return wp_insert_user( $args );
+ }
+
+ function update_object( $user_id, $fields ) {
+ $fields['ID'] = $user_id;
+ return wp_update_user( $fields );
+ }
+
+ function get_object_by_id( $user_id ) {
+ return new WP_User( $user_id );
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryclasswpunittestgeneratorsequencephpfromrev36346trunktestsphpunitincludesfactoryphp"></a>
<div class="copfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Copied: trunk/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php (from rev 36346, trunk/tests/phpunit/includes/factory.php)</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php (rev 0)
+++ trunk/tests/phpunit/includes/factory/class-wp-unittest-generator-sequence.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,23 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+class WP_UnitTest_Generator_Sequence {
+ static $incr = -1;
+ public $next;
+ public $template_string;
+
+ function __construct( $template_string = '%s', $start = null ) {
+ if ( $start ) {
+ $this->next = $start;
+ } else {
+ self::$incr++;
+ $this->next = self::$incr;
+ }
+ $this->template_string = $template_string;
+ }
+
+ function next() {
+ $generated = sprintf( $this->template_string , $this->next );
+ $this->next++;
+ return $generated;
+ }
+}
</ins></span></pre></div>
<a id="trunktestsphpunitincludesfactoryphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/includes/factory.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/includes/factory.php 2016-01-19 03:02:42 UTC (rev 36346)
+++ trunk/tests/phpunit/includes/factory.php 2016-01-19 03:55:19 UTC (rev 36347)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1,5 +1,16 @@
</span><span class="cx" style="display: block; padding: 0 10px"> <?php
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-thing.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-post.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-attachment.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-user.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-comment.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-blog.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-network.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-for-term.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-generator-sequence.php' );
+require_once( dirname( __FILE__ ) . '/factory/class-wp-unittest-factory-callback-after-create.php' );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> class WP_UnitTest_Factory {
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -61,375 +72,3 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing {
-
- function __construct( $factory = null ) {
- parent::__construct( $factory );
- $this->default_generation_definitions = array(
- 'post_status' => 'publish',
- 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ),
- 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ),
- 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ),
- 'post_type' => 'post'
- );
- }
-
- function create_object( $args ) {
- return wp_insert_post( $args );
- }
-
- function update_object( $post_id, $fields ) {
- $fields['ID'] = $post_id;
- return wp_update_post( $fields );
- }
-
- function get_object_by_id( $post_id ) {
- return get_post( $post_id );
- }
-}
-
-class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post {
-
- function create_object( $file, $parent = 0, $args = array() ) {
- return wp_insert_attachment( $args, $file, $parent );
- }
-
- function create_upload_object( $file, $parent = 0 ) {
- $contents = file_get_contents($file);
- $upload = wp_upload_bits(basename($file), null, $contents);
-
- $type = '';
- if ( ! empty($upload['type']) ) {
- $type = $upload['type'];
- } else {
- $mime = wp_check_filetype( $upload['file'] );
- if ($mime)
- $type = $mime['type'];
- }
-
- $attachment = array(
- 'post_title' => basename( $upload['file'] ),
- 'post_content' => '',
- 'post_type' => 'attachment',
- 'post_parent' => $parent,
- 'post_mime_type' => $type,
- 'guid' => $upload[ 'url' ],
- );
-
- // Save the data
- $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent );
- wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) );
-
- return $id;
- }
-}
-
-class WP_UnitTest_Factory_For_User extends WP_UnitTest_Factory_For_Thing {
-
- function __construct( $factory = null ) {
- parent::__construct( $factory );
- $this->default_generation_definitions = array(
- 'user_login' => new WP_UnitTest_Generator_Sequence( 'User %s' ),
- 'user_pass' => 'password',
- 'user_email' => new WP_UnitTest_Generator_Sequence( 'user_%s@example.org' ),
- );
- }
-
- function create_object( $args ) {
- return wp_insert_user( $args );
- }
-
- function update_object( $user_id, $fields ) {
- $fields['ID'] = $user_id;
- return wp_update_user( $fields );
- }
-
- function get_object_by_id( $user_id ) {
- return new WP_User( $user_id );
- }
-}
-
-class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing {
-
- function __construct( $factory = null ) {
- parent::__construct( $factory );
- $this->default_generation_definitions = array(
- 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ),
- 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ),
- 'comment_approved' => 1,
- 'comment_content' => 'This is a comment'
- );
- }
-
- function create_object( $args ) {
- return wp_insert_comment( $this->addslashes_deep( $args ) );
- }
-
- function update_object( $comment_id, $fields ) {
- $fields['comment_ID'] = $comment_id;
- return wp_update_comment( $this->addslashes_deep( $fields ) );
- }
-
- function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) {
- $args['comment_post_ID'] = $post_id;
- return $this->create_many( $count, $args, $generation_definitions );
- }
-
- function get_object_by_id( $comment_id ) {
- return get_comment( $comment_id );
- }
-}
-
-class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing {
-
- function __construct( $factory = null ) {
- global $current_site, $base;
- parent::__construct( $factory );
- $this->default_generation_definitions = array(
- 'domain' => $current_site->domain,
- 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ),
- 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ),
- 'site_id' => $current_site->id,
- );
- }
-
- function create_object( $args ) {
- global $wpdb;
- $meta = isset( $args['meta'] ) ? $args['meta'] : array();
- $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id();
- // temp tables will trigger db errors when we attempt to reference them as new temp tables
- $suppress = $wpdb->suppress_errors();
- $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] );
- $wpdb->suppress_errors( $suppress );
-
- // Tell WP we're done installing.
- wp_installing( false );
-
- return $blog;
- }
-
- function update_object( $blog_id, $fields ) {}
-
- function get_object_by_id( $blog_id ) {
- return get_blog_details( $blog_id, false );
- }
-}
-
-
-class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing {
-
- function __construct( $factory = null ) {
- parent::__construct( $factory );
- $this->default_generation_definitions = array(
- 'domain' => WP_TESTS_DOMAIN,
- 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ),
- 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ),
- 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ),
- 'subdomain_install' => false,
- );
- }
-
- function create_object( $args ) {
- require_once ABSPATH . 'wp-admin/includes/upgrade.php';
-
- if ( ! isset( $args['user'] ) ) {
- $email = WP_TESTS_EMAIL;
- } else {
- $email = get_userdata( $args['user'] )->user_email;
- }
-
- populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] );
- return $args['network_id'];
- }
-
- function update_object( $network_id, $fields ) {}
-
- function get_object_by_id( $network_id ) {
- return wp_get_network( $network_id );
- }
-}
-
-class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing {
-
- private $taxonomy;
- const DEFAULT_TAXONOMY = 'post_tag';
-
- function __construct( $factory = null, $taxonomy = null ) {
- parent::__construct( $factory );
- $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY;
- $this->default_generation_definitions = array(
- 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ),
- 'taxonomy' => $this->taxonomy,
- 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ),
- );
- }
-
- function create_object( $args ) {
- $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args );
- $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args );
- if ( is_wp_error( $term_id_pair ) )
- return $term_id_pair;
- return $term_id_pair['term_id'];
- }
-
- function update_object( $term, $fields ) {
- $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields );
- if ( is_object( $term ) )
- $taxonomy = $term->taxonomy;
- $term_id_pair = wp_update_term( $term, $taxonomy, $fields );
- return $term_id_pair['term_id'];
- }
-
- function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) {
- return wp_set_post_terms( $post_id, $terms, $taxonomy, $append );
- }
-
- function create_and_get( $args = array(), $generation_definitions = null ) {
- $term_id = $this->create( $args, $generation_definitions );
- $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy;
- return get_term( $term_id, $taxonomy );
- }
-
- function get_object_by_id( $term_id ) {
- return get_term( $term_id, $this->taxonomy );
- }
-}
-
-abstract class WP_UnitTest_Factory_For_Thing {
-
- var $default_generation_definitions;
- var $factory;
-
- /**
- * Creates a new factory, which will create objects of a specific Thing
- *
- * @param object $factory Global factory that can be used to create other objects on the system
- * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values
- * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence},
- * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}.
- */
- function __construct( $factory, $default_generation_definitions = array() ) {
- $this->factory = $factory;
- $this->default_generation_definitions = $default_generation_definitions;
- }
-
- abstract function create_object( $args );
- abstract function update_object( $object, $fields );
-
- function create( $args = array(), $generation_definitions = null ) {
- if ( is_null( $generation_definitions ) )
- $generation_definitions = $this->default_generation_definitions;
-
- $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks );
- $created = $this->create_object( $generated_args );
- if ( !$created || is_wp_error( $created ) )
- return $created;
-
- if ( $callbacks ) {
- $updated_fields = $this->apply_callbacks( $callbacks, $created );
- $save_result = $this->update_object( $created, $updated_fields );
- if ( !$save_result || is_wp_error( $save_result ) )
- return $save_result;
- }
- return $created;
- }
-
- function create_and_get( $args = array(), $generation_definitions = null ) {
- $object_id = $this->create( $args, $generation_definitions );
- return $this->get_object_by_id( $object_id );
- }
-
- abstract function get_object_by_id( $object_id );
-
- function create_many( $count, $args = array(), $generation_definitions = null ) {
- $results = array();
- for ( $i = 0; $i < $count; $i++ ) {
- $results[] = $this->create( $args, $generation_definitions );
- }
- return $results;
- }
-
- function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) {
- $callbacks = array();
- if ( is_null( $generation_definitions ) )
- $generation_definitions = $this->default_generation_definitions;
-
- foreach( array_keys( $generation_definitions ) as $field_name ) {
- if ( !isset( $args[$field_name] ) ) {
- $generator = $generation_definitions[$field_name];
- if ( is_scalar( $generator ) )
- $args[$field_name] = $generator;
- elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) {
- $callbacks[$field_name] = $generator;
- } elseif ( is_object( $generator ) )
- $args[$field_name] = $generator->next();
- else
- return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' );
- }
- }
- return $args;
- }
-
- function apply_callbacks( $callbacks, $created ) {
- $updated_fields = array();
- foreach( $callbacks as $field_name => $generator ) {
- $updated_fields[$field_name] = $generator->call( $created );
- }
- return $updated_fields;
- }
-
- function callback( $function ) {
- return new WP_UnitTest_Factory_Callback_After_Create( $function );
- }
-
- function addslashes_deep($value) {
- if ( is_array( $value ) ) {
- $value = array_map( array( $this, 'addslashes_deep' ), $value );
- } elseif ( is_object( $value ) ) {
- $vars = get_object_vars( $value );
- foreach ($vars as $key=>$data) {
- $value->{$key} = $this->addslashes_deep( $data );
- }
- } elseif ( is_string( $value ) ) {
- $value = addslashes( $value );
- }
-
- return $value;
- }
-
-}
-
-class WP_UnitTest_Generator_Sequence {
- static $incr = -1;
- public $next;
- public $template_string;
-
- function __construct( $template_string = '%s', $start = null ) {
- if ( $start ) {
- $this->next = $start;
- } else {
- self::$incr++;
- $this->next = self::$incr;
- }
- $this->template_string = $template_string;
- }
-
- function next() {
- $generated = sprintf( $this->template_string , $this->next );
- $this->next++;
- return $generated;
- }
-}
-
-class WP_UnitTest_Factory_Callback_After_Create {
- var $callback;
-
- function __construct( $callback ) {
- $this->callback = $callback;
- }
-
- function call( $object ) {
- return call_user_func( $this->callback, $object );
- }
-}
</del></span></pre>
</div>
</div>
</body>
</html>