<commit_before><?php
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

class ChannelTest extends \PHPUnit\Framework\TestCase
{
    public function setUp(): void
    {
    }

    public function tearDown(): void
    {
        if (!empty($this->channel)) {
            $this->channel->close();
        }
    }

    public function testInsecureCredentials()
    {
        $this->channel = new Grpc\Channel('localhost:50000',
            ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $this->assertSame('Grpc\Channel', get_class($this->channel));
    }

    public function testConstructorCreateSsl()
    {
        $channel = new Grpc\Channel('localhost:50033', 
            ['credentials' => \Grpc\ChannelCredentials::createSsl()]);
        $this->assertNotNull($channel);
    }

    public function testCreateXdsWithSsl()
    {
        $xdsCreds = \Grpc\ChannelCredentials::createXds(
            \Grpc\ChannelCredentials::createSsl()
        );
        $this->assertNotNull($xdsCreds);
    }

    public function testCreateXdsWithInsecure() {
        $xdsCreds = \Grpc\ChannelCredentials::createXds(
            \Grpc\ChannelCredentials::createInsecure()
        );
        $this->assertNotNull($xdsCreds);
    }

    public function testCreateXdsWithNull() {
        $this->expectException(\InvalidArgumentException::class);
        $xdsCreds = \Grpc\ChannelCredentials::createXds(null);
    }

    public function testCreateXdsWithInvalidType() {
        $this->expectException(\TypeError::class);
        $xdsCreds = \Grpc\ChannelCredentials::createXds("invalid-type");
    }

    public function testGetConnectivityState()
    {
        $this->channel = new Grpc\Channel('localhost:50001',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $state = $this->channel->getConnectivityState();
        $this->assertEquals(0, $state);
    }

    public function testGetConnectivityStateWithInt()
    {
        $this->channel = new Grpc\Channel('localhost:50002',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $state = $this->channel->getConnectivityState(123);
        $this->assertEquals(0, $state);
    }
<commit_msg>try/catch exceptions of both php7 and php8 (#25918)<commit_after><?php
/*
 *
 * Copyright 2018 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

class ChannelTest extends \PHPUnit\Framework\TestCase
{
    public function setUp(): void
    {
    }

    public function tearDown(): void
    {
        if (!empty($this->channel)) {
            $this->channel->close();
        }
    }

    public function testInsecureCredentials()
    {
        $this->channel = new Grpc\Channel('localhost:50000',
            ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $this->assertSame('Grpc\Channel', get_class($this->channel));
    }

    public function testConstructorCreateSsl()
    {
        $channel = new Grpc\Channel('localhost:50033', 
            ['credentials' => \Grpc\ChannelCredentials::createSsl()]);
        $this->assertNotNull($channel);
    }

    public function testCreateXdsWithSsl()
    {
        $xdsCreds = \Grpc\ChannelCredentials::createXds(
            \Grpc\ChannelCredentials::createSsl()
        );
        $this->assertNotNull($xdsCreds);
    }

    public function testCreateXdsWithInsecure() {
        $xdsCreds = \Grpc\ChannelCredentials::createXds(
            \Grpc\ChannelCredentials::createInsecure()
        );
        $this->assertNotNull($xdsCreds);
    }

    public function testCreateXdsWithNull() {
        $this->expectException(\InvalidArgumentException::class);
        $xdsCreds = \Grpc\ChannelCredentials::createXds(null);
    }

    public function testCreateXdsWithInvalidType()
    {
        $expected = $this->logicalOr(
            // PHP8
            new \PHPUnit\Framework\Constraint\Exception(\InvalidArgumentException::class),
            // PHP7
            new \PHPUnit\Framework\Constraint\Exception(\TypeError::class)
        );
        try {
            $xdsCreds = \Grpc\ChannelCredentials::createXds("invalid-type");
        } catch (\Throwable $exception) {
            $this->assertThat($exception, $expected);
            return;
        }
        $this->assertThat(null, $expected);
    }

    public function testGetConnectivityState()
    {
        $this->channel = new Grpc\Channel('localhost:50001',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $state = $this->channel->getConnectivityState();
        $this->assertEquals(0, $state);
    }

    public function testGetConnectivityStateWithInt()
    {
        $this->channel = new Grpc\Channel('localhost:50002',
             ['credentials' => Grpc\ChannelCredentials::createInsecure()]);
        $state = $this->channel->getConnectivityState(123);
        $this->assertEquals(0, $state);
    }

{'fraction_non_alphanumeric': 0.12437143814951392, 'fraction_numerical': 0.012571237009721757, 'mean_word_length': 2.8721609344581442, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2126483', 'n_tokens_mistral': 1678, 'n_tokens_neox': 1633, 'n_words': 357}