We'll be using PHP 7.4 for the setup.
-
Clone the directory.
git clone git@github.com:googleapis/gapic-generator-php.git cd gapic-generator-php
-
Install dependencies.
- On Linux:
sudo apt-get install php-curl php7.4-mbstring libxml2-dev libssl-dev libcurl4-openssl-dev
-
Run
php composer.phar install
- Alternatively, install composer globally.
> php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" > php -r "if (hash_file('sha384', 'composer-setup.php') === \ '756890a4488ce9024fc62c56153228907f1545c228516cbf63f885e036d37e9a59d27d63f46af1d4d07ee0f76181c7d3') { \ echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); \ } echo PHP_EOL;" Installer verified > sudo php composer-setup.php --install-dir=/usr/local/bin --filename=composer
- Install
php-cs-fixer
globally.
> curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer > sudo chmod a+x php-cs-fixer > sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
-
Optional: Enable PHP-CS-Fixer linting in your IDE.
- Vim: Set up vim-php-cs-fixer, and apply this fix
-
Initialize the submodules
git submodule update --init --recursive
-
Set up pre-commit checks.
cp .githooks/pre-commit .git/hooks/pre-commit
-
Unit tests
./vendor/bin/phpunit --bootstrap tests/Unit/autoload.php tests/Unit
If you do not have
protoc
installed, run withUSE_TOOLS_PROTOC=true
.USE_TOOLS_PROTOC=true ./vendor/bin/phpunit --bootstrap tests/Unit/autoload.php tests/Unit
This uses the Linux-only
protoc
binary checked into the repository.If you run into an error:
Error: Call to undefined function Google\Protobuf\Internal\bccomp()
, that is because the BC Math extension is not always included by default (see tracking bug here: protocolbuffers/protobuf#4465). You can get around this by installing BC Math with the commandsudo apt install php-bcmath
. -
Bazel integration tests.
-
Running:
bazel test tests/Integration:asset
-
Running all tests:
bash tests/scripts/run_bazel_tests.sh
Note: Running
bazel
commands may require removing thecomposer.lock
andvendor/
directory. -
Debugging in
googleapis
:In
googleapis/WORKSPACE
, replace thehttp_archive
downloading the generator with alocal_repository
target pointing to the locally modified version of the generator:local_repository( name = "gapic_generator_php", path = "/absolute/path/to/local/generator", )
-
You will need to update the golden test files if you change something in the generation process that modifies the output (for example, renaming a variable).
-
Updating unit test goldens:
php tests/Unit/ProtoTests/GoldenUpdateMain.php
Then follow the prompts for which tests to update.
NOTE: If a new unit test case is added, make sure to add it to the
UNIT_TESTS
list in GoldenUpdateMain.php. -
Updating integration test goldens:
bash tests/scripts/run_bazel_updates.sh --expunge
Integration tests can be updated individually as well:
bazel run tests/Integration:asset_update
NOTE: If a new integration test case is added, make sure to add it the list in run_bazel_tests.sh and run_bazel_updates.sh.
-
Update all goldens (unit and integration)
Run the composer script
update-all-tests
to update all goldens at once:composer update-all-tests
The GitHub Actions that run the bazel
-based integration tests hold a cache of
the build in order to speed up testing pull requests that do not impact
generated surface. If proposing a change that modifies the integration test
golden files, one must also rotate the cache key using the
gh
CLI. Use the following command before opening
your pull request:
uuidgen | gh secret set CACHE_VERSION -r https://github.com/googleapis/gapic-generator-php
If you don't have the proper permissions to rotate the cache key, request that the reviewers do so and rerun the Action checks.
To update the googleapis
submodule, change into the directory and pull:
pushd googleapis
git pull origin master
popd
Then update the commit hash of googleapis
in repositories.bzl
to match.
Finally, the test golden files need to be udpated with the latest
version of the protos from googleapis
. See the instructions in Running tests
for the exact commands.
Submit all of this in standalone pull request.
Ensure that the googleapis
submodule is pinned to a commit recent enough to
include the annotation(s)/type(s) targeted for generation. If the googleapis
submodule needs to be updated, see Updating the googleapis
submodule.
From the googleapis
submodule, generate the PHP Protobuf bindings with
protoc
:
protoc -I. --php_out=../generated relative/path/to/my.proto
Note: If the newly generated file belongs to a new package, ensure that it is in scope of the existing autoload rules in the composer.json, adding it if it's not.
See steps under Updating composer.