Skip to content

PHPMock::defineFunctionMock not working? #12

Closed
@krodyrobi

Description

@krodyrobi

So I've read about the namespace bug and applied the recommended fix but it doesn't seem to work in this instance.
It did in another case but not here, even tried to run solo / in a different process but to no avail.
Added all the functions to the defineFunctionMock just to be safe even though most are not needed.

I've ran out of ideas, any thoughts on why this might not work?

<?php

namespace presslabs\minify;

use phpmock\phpunit\PHPMock;


class Test_PL_Minify extends \PHPUnit_Framework_TestCase {
  use PHPMock;

  public static function setUpBeforeClass() {
    # these are needed because of https://bugs.php.net/bug.php?id=68541
    # in short we use a call from an unaltered namespace then alter it which leads php confused
    PHPMock::defineFunctionMock( __NAMESPACE__, 'true_constant' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'defined' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'constant' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'pl_minify_page' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'minify_html' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'minify_css' );
    PHPMock::defineFunctionMock( __NAMESPACE__, 'minify_js' );
    # TODO refactor this duplication
  }

  function test_minify_page_minification() {
    $true_constant = $this->getFunctionMock( __NAMESPACE__, 'true_constant' );
    $true_constant->expects( $this->once() )
            ->with( 'PL_MINIFY_HTML' )
            ->willReturn( true );

    $input = 'random';

    $minify_html = $this->getFunctionMock( __NAMESPACE__, 'minify_html' );
    $minify_html->expects( $this->once() )
          ->with( $input )
          ->willReturn( 'mini' );


    $actual = pl_minify_page( $input );

    $this->assertSame( 'mini', $actual );
  }
}
namespace presslabs\minify;


function pl_minify_page( $buffer ) {
  error_log('before if' . serialize($buffer));
  if ( true_constant( 'PL_MINIFY_HTML' ) ) {
    error_log( "a" . serialize( $buffer ) );
                # gets here

    return minify_html( $buffer );
  }
  error_log('not modified');
  return $buffer;
}


function minify_css( $text ) {
  require_once( PL_PLUGINS_LIB . '/class-cssmin.php' );
  $compressor = new \CSSmin();
  $contents   = $compressor->run( $text );

  return $contents;
}


function minify_js( $text ) {
  require_once( PL_PLUGINS_LIB . '/class-JavaScriptPacker.php' );

  $packer   = new \JavaScriptPacker( $text, 'None', true, false );
  $contents = $packer->pack();

  return $contents;
}


function minify_html( $text ) {
  require_once( PL_PLUGINS_LIB . '/class-minify-html.php' );

  $buffer = \Minify_HTML::minify( $text, array(
    'cssMinifier'       => true_constant( 'PL_MINIFY_INLINE_CSS' ) ? __NAMESPACE__ . '\\minify_css' : null,
    'jsMinifier'        => true_constant( 'PL_MINIFY_INLINE_JS' ) ? __NAMESPACE__ . '\\minify_js' : null,
    'jsCleanComments'   => true_constant( 'PL_MINIFY_REMOVE_COMMENTS' ),
    'htmlCleanComments' => true_constant( 'PL_MINIFY_REMOVE_COMMENTS' )
  ) );

  return $buffer;
}


function true_constant( $name ) {
  return defined( $name ) and constant( $name ) === 'True';
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions