Skip to content
This repository has been archived by the owner on Nov 28, 2019. It is now read-only.

Commit

Permalink
Introduce ImportListeningMetaFilterList #56
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaber-de committed Feb 17, 2016
1 parent 056ca16 commit 4c167a4
Show file tree
Hide file tree
Showing 2 changed files with 236 additions and 0 deletions.
130 changes: 130 additions & 0 deletions inc/Import/Data/ImportListeningMetaFilterList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php # -*- coding: utf-8 -*-

namespace W2M\Import\Data;

use
W2M\Import\Filter,
W2M\Import\Type,
SplObjectStorage;

/**
* Class ImportListeningMetaFilterList
*
* Listens to w2m_import_meta_not_filterable and collect
* all postponed meta filters.
*
* @todo: Specify an interface for this
*
* @package W2M\Import\Data
*/
class ImportListeningMetaFilterList {

/**
* A SplObjectStorage per type
*
* @var SplObjectStorage[]
*/
private $filters = [];

/**
* Sets up internal structures
*/
public function __construct() {

$this->filters = [
'comment' => new SplObjectStorage,
'post' => new SplObjectStorage,
'term' => new SplObjectStorage,
'user' => new SplObjectStorage
];
}

/**
* @wp-hook w2m_import_meta_not_filterable
*
* @param Filter\ValueFilterableInterface $filter
* @param Type\MetaRecordIndexInterface $meta_index
* @param Type\ImportMetaInterface $meta
*/
public function record_meta_filter(
Filter\ValueFilterableInterface $filter,
Type\MetaRecordIndexInterface $meta_index,
Type\ImportMetaInterface $meta
) {

$this->register_type( $meta_index->type() );
if ( $this->filter_exists( $filter, $meta_index ) )
return;

$this->push_filter( $filter, $meta_index, $meta );
}

/**
* @param string $type
*
* @return SplObjectStorage
*/
public function get_filters( $type ) {

return isset( $this->filters[ $type ] )
? $this->filters[ $type ]
: new SplObjectStorage;
}

/**
* @param $type
*/
private function register_type( $type ) {

if ( isset( $this->filters[ $type ] ) )
return;

$this->filters[ $type ] = new SplObjectStorage;
}

/**
* @param Filter\ValueFilterableInterface $filter
* @param Type\MetaRecordIndexInterface $meta_index
*
* @return bool
*/
private function filter_exists(
Filter\ValueFilterableInterface $filter,
Type\MetaRecordIndexInterface $meta_index
) {

$storage = $this->filters[ $meta_index->type() ];
$filter_id = spl_object_hash( $filter );

if ( ! $storage->contains( $meta_index ) )
return FALSE;

$filters = $storage->offsetGet( $meta_index );

return isset( $filters[ $filter_id ] );
}

/**
* @param Filter\ValueFilterableInterface $filter
* @param Type\MetaRecordIndexInterface $meta_index
* @param Type\ImportMetaInterface $meta
*/
private function push_filter(
Filter\ValueFilterableInterface $filter,
Type\MetaRecordIndexInterface $meta_index,
Type\ImportMetaInterface $meta
) {

$storage = $this->filters[ $meta_index->type() ];
$filter_id = spl_object_hash( $filter );
$meta_filters = $storage->contains( $meta_index )
? $storage->offsetGet( $meta_index )
: [];
$meta_filters[ $filter_id ] = [
'filter' => $filter,
'meta' => $meta
];

$storage->attach( $meta_index, $meta_filters );
}
}
106 changes: 106 additions & 0 deletions tests/phpunit/Unit/Import/Data/ImportListeningMetaFilterListTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php # -*- coding: utf-8 -*-

namespace W2M\Test\Unit\Import\Data;

use
W2M\Import\Data,
W2M\Import\Filter,
W2M\Import\Type,
W2M\Test\Helper;

class ImportListeningMetaFilterListTest extends Helper\MonkeyTestCase {

public function test_record_filter() {

$type = 'post';
$filter = $this->mock_builder->filter_value_filterable_interface();
$meta_index = $this->mock_builder->type_meta_record_index_interface(
[],
[
'key' => '_thumbnail_id',
'type' => $type,
'object_id' => 42,
'index' => 0
]
);
$meta = $this->mock_builder->type_wp_import_meta();

$testee = new Data\ImportListeningMetaFilterList;
$testee->record_meta_filter( $filter, $meta_index, $meta );
$storage = $testee->get_filters( 'post' );

$this->assertTrue(
$storage->contains( $meta_index )
);
$this->assertSame(
[
'filter' => $filter,
'meta' => $meta
],
current( $storage->offsetGet( $meta_index ) )
);
}

public function test_record_multiple_filters_per_meta_index() {

$type = 'term';
$meta_index = $this->mock_builder->type_meta_record_index_interface(
[],
[
'key' => 'some_term_meta',
'type' => $type,
'object_id' => 145,
'index' => 0
]
);
$filter_mocks = [
$this->mock_builder->filter_value_filterable_interface(),
$this->mock_builder->filter_value_filterable_interface(),
$this->mock_builder->filter_value_filterable_interface()
];
$meta = $this->mock_builder->type_wp_import_meta();

$testee = new Data\ImportListeningMetaFilterList;
foreach ( $filter_mocks as $filter ) {
$testee->record_meta_filter( $filter, $meta_index, $meta );
}

$storage = $testee->get_filters( $type );

$this->assertTrue(
$storage->contains( $meta_index )
);

/**
* @var array $filter_meta_list[] {
* @var Filter\ValueFilterableInterface $filter
* @var Type\ImportMetaInterface $meta
* }
*/
$filter_meta_list = $storage->offsetGet( $meta_index );
$this->assertCount(
count( $filter_mocks ),
$filter_meta_list
);

$filter_list = [];
foreach ( $filter_meta_list as $filter_meta_pair ) {
$filter_list[] = $filter_meta_pair[ 'filter' ];
$this->assertSame(
$meta,
$filter_meta_pair[ 'meta' ]
);
}

$this->assertSame(
array_values( $filter_mocks ),
array_values( $filter_list )
);
}

public function test_record_filter_with_multiple_types() {

// Todo: write test
$this->markTestIncomplete( 'Under construction …');
}
}

0 comments on commit 4c167a4

Please sign in to comment.