Reference documentation and code samples for the Google Cloud PHP shared dependency, providing functionality useful to all components. Client class Snippet.
Represents a single code snippet
Namespace
Google \ Cloud \ Core \ Testing \ Snippet \ ParserMethods
__construct
Create a snippet
identifier
string
The snippet ID
config
array
The snippet config
identifier
A unique identifier for the snippet.
This identifier is deterministic and will remain constant unless the snippet is modified or moved.
string
file
The file in which the snippet is found.
string
fqn
The Snippet fully-qualified name.
string
line
The line number where the snippet's method or class is declared.
Note that this is NOT the line number where the snippet is declared. It indicates the method or class which the snippet annotates.
int
content
The snippet content.
string
index
The Snippet Index
int
name
The snippet name
string
invoke
Eval the snippet and return the result.
returnVar
mixed
mixed
addLocal
Add a local variable to make available in the snippet execution scope.
name
string
The variable name
value
mixed
The variable value
void
addUse
Add a use
statement for a class.
name
string
The class name to import.
void
setLine
Replace a line with new code.
Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're overwriting the thing you are trying to test.
This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.
line
int
The line number (0-indexed) to replace.
content
string
The PHP code to inject.
void
insertAfterLine
Inject new code after a given line.
Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.
This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.
line
int
The line number (0-indexed) to write in after.
content
string
The PHP code to inject.
void
replace
Replace a string in the snippet with a new one.
Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.
This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.
old
string
The string to be replaced.
new
string
The new string to insert.
void
regexReplace
Find something with a regex and replace it.
Hopefully this is obvious, but be careful using this, and only use it when no other feasible options present themselves. It's pretty easy to make your test useless when you're modifying the thing you are trying to test.
This is provided for cases when a snippet relies on a global, or on something else which can not be overridden or mocked.
pattern
string
The regex pattern to search for.
new
string
The new string to insert.
void
jsonSerialize
Serialize to json
array