<commit_before><?php

namespace StrictPhpTest\Aspect;

use Go\Aop\Intercept\FieldAccess;
use StdClass;
use StrictPhp\Aspect\ImmutablePropertyCheck;

/**
 * Tests for {@see \StrictPhp\Aspect\ImmutablePropertyCheck}
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT
 *
 * @group Coverage
 */
class ImmutablePropertyCheckTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenCannotGetJoinPointObject()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);

        $fieldAccess->expects($this->once())->method('getThis')->will($this->returnValue(null));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenFieldAccessIsNotWritable()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::READ));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenFieldValueIsNull()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = $this->getMockBuilder(StdClass::class)->setMethods(['setAccessible', 'getValue'])->getMock();

        $field->expects($this->once())->method('setAccessible')->willReturnSelf();
        $field->expects($this->once())->method('getValue')->will($this->returnValue(null));

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->once())->method('getField')->will($this->returnValue($field));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenNotFindImmutableTagName()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = $this->getMockBuilder(StdClass::class)->setMethods(['setAccessible', 'getValue', 'getDocComment'])->getMock();

        $field->expects($this->once())->method('setAccessible')->willReturnSelf();
        $field->expects($this->once())->method('getValue')->will($this->returnValue(true));
        $field->expects($this->once())->method('getDocComment')->will($this->returnValue(null));

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->once())->method('getField')->will($this->returnValue($field));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testRaisesExceptionWhenFieldAccessIsInvalid()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = $this->getMockBuilder(StdClass::class)
            ->setMethods([
                'setAccessible',
                'getName',
                'getValue',
                'getDocComment',
            ])->getMock();

        $field->expects($this->once())->method('setAccessible')->willReturnSelf();
        $field->expects($this->once())->method('getValue')->will($this->returnValue(true));
        $field->expects($this->once())->method('getDocComment')->will($this->returnValue('/** @immutable */'));
        $field->expects($this->any())->method('getName')->will($this->returnValue(StdClass::class));

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->once())->method('getField')->will($this->returnValue($field));

        $immutablePropertyCheck = new ImmutablePropertyCheck();

        $this->setExpectedException(\RuntimeException::class);
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);<commit_msg>Correcting test case that was relying on broken mocking logic<commit_after><?php

namespace StrictPhpTest\Aspect;

use Go\Aop\Intercept\FieldAccess;
use StdClass;
use StrictPhp\Aspect\ImmutablePropertyCheck;
use StrictPhpTestAsset\ClassWithImmutableProperty;
use ReflectionProperty;

/**
 * Tests for {@see \StrictPhp\Aspect\ImmutablePropertyCheck}
 *
 * @author <NAME> <<EMAIL>>
 * @license MIT
 *
 * @group Coverage
 */
class ImmutablePropertyCheckTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenCannotGetJoinPointObject()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);

        $fieldAccess->expects($this->once())->method('getThis')->will($this->returnValue(null));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenFieldAccessIsNotWritable()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::READ));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenFieldValueIsNull()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = $this->getMockBuilder(StdClass::class)->setMethods(['setAccessible', 'getValue'])->getMock();

        $field->expects($this->once())->method('setAccessible')->willReturnSelf();
        $field->expects($this->once())->method('getValue')->will($this->returnValue(null));

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->once())->method('getField')->will($this->returnValue($field));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testReturnProceedWhenNotFindImmutableTagName()
    {
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = $this->getMockBuilder(StdClass::class)->setMethods(['setAccessible', 'getValue', 'getDocComment'])->getMock();

        $field->expects($this->once())->method('setAccessible')->willReturnSelf();
        $field->expects($this->once())->method('getValue')->will($this->returnValue(true));
        $field->expects($this->once())->method('getDocComment')->will($this->returnValue(null));

        $fieldAccess->expects($this->once())->method('getThis')->willReturnSelf();
        $fieldAccess->expects($this->once())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->once())->method('getField')->will($this->returnValue($field));
        $fieldAccess->expects($this->once())->method('proceed');

        $immutablePropertyCheck = new ImmutablePropertyCheck();
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
    }

    /**
     * @covers \StrictPhp\Aspect\ImmutablePropertyCheck::beforePropertyAccess
     */
    public function testRaisesExceptionWhenFieldAccessIsInvalid()
    {
        $object      = new ClassWithImmutableProperty();
        /* @var $fieldAccess FieldAccess|\PHPUnit_Framework_MockObject_MockObject */
        $fieldAccess = $this->getMock(FieldAccess::class);
        $field       = new ReflectionProperty(ClassWithImmutableProperty::class, 'property');

        $object->property = 'initialized';

        $fieldAccess->expects($this->any())->method('getThis')->will($this->returnValue($object));
        $fieldAccess->expects($this->any())->method('getAccessType')->will($this->returnValue(FieldAccess::WRITE));
        $fieldAccess->expects($this->any())->method('getField')->will($this->returnValue($field));

        $immutablePropertyCheck = new ImmutablePropertyCheck();

        $this->setExpectedException(\RuntimeException::class);
        $immutablePropertyCheck->beforePropertyAccess($fieldAccess);
{'fraction_non_alphanumeric': 0.1712406015037594, 'fraction_numerical': 0.0, 'mean_word_length': 4.4597229348383784, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 249, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '825141', 'n_tokens_mistral': 2944, 'n_tokens_neox': 2926, 'n_words': 280}