Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Be able to parse CSV from STDIN #4426

Closed
wants to merge 3 commits into from
Closed

Be able to parse CSV from STDIN #4426

wants to merge 3 commits into from

Conversation

miya0001
Copy link
Member

@miya0001 miya0001 commented Oct 6, 2017

@miya0001
Copy link
Member Author

miya0001 commented Oct 6, 2017

@wp-cli/committers
Do you know how test STDIN with phpunit?

@gitlost
Copy link
Contributor

gitlost commented Oct 6, 2017

For just unit testing Iterators\CSV, exec is your man:

<?php

class IteratorsTest extends PHPUnit_Framework_TestCase {
	
	function testIteratorsCSVStdin() {

		$php = "require '" . dirname( __DIR__ ) . "/php/WP_CLI/Iterators/CSV.php';\n";
		$php .= <<<'EOD'
$iterator = new WP_CLI\Iterators\CSV( '-' );
foreach( $iterator as $i => $line ) {
	echo "$i";
	foreach ( $line as $j => $field ) echo ":$field";
	echo "\n";
}
EOD;

		$stdin = <<<'EOD'
user_login,user_email,display_name,role
stdinuser1,bobjones@example.com,Bob Jones,contributor
stdinuser2,newuser1@example.com,New User,author
existinguser,existinguser@example.com,Existing User,administrator
EOD;

		$stdin = escapeshellarg( $stdin );
		$php = escapeshellarg( $php );

		$output = array();
		exec( "echo $stdin | php -r $php", $output );

		$this->assertSame( 3, count( $output ) );
		$this->assertSame( '0:stdinuser1:bobjones@example.com:Bob Jones:contributor', $output[0] );
	}
}

$this->filePointer = fopen( 'php://stdin', 'r' );
} else {
$this->filePointer = fopen( $filename, 'r' );
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's too magical (also known as unexpected) to interpret - as STDIN.

For the purposes of wp-cli/entity-command#96, we should simply read STDIN to memory, and then parse as CSV.

@miya0001
Copy link
Member Author

miya0001 commented Oct 7, 2017

@gitlost Thanks, you always help me a lot. 😊
@danielbachhuber OK, I opened new PR. wp-cli/entity-command#100

Thanks again!

@danielbachhuber danielbachhuber deleted the allow-stdin branch October 7, 2017 13:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants