Skip to content

Commit

Permalink
✅ Update tests to reflect output changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Log1x committed Mar 26, 2024
1 parent 8fd7af5 commit a7fb72c
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions tests/Unit/WordPressTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,31 @@

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(1, 'WP_Post') || is_numeric(1)) : ?><?php \$posts->put('p', is_a(1, 'WP_Post') ? (1)->ID : 1); ?><?php endif; ?><?php if (is_array(1)) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(1) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : 1; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(1, 'WP_Post') || is_numeric(1)) : ?><?php \$posts->put('p', is_a(1, 'WP_Post') ? (1)->ID : 1); ?><?php endif; ?><?php if (is_array(1)) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect(1)->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : 1; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
});

it('compiles correctly with post object', function () {
$directive = '@posts(get_post(1))';

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(get_post(1), 'WP_Post') || is_numeric(get_post(1))) : ?><?php \$posts->put('p', is_a(get_post(1), 'WP_Post') ? (get_post(1))->ID : get_post(1)); ?><?php endif; ?><?php if (is_array(get_post(1))) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(get_post(1)) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : get_post(1); ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(get_post(1), 'WP_Post') || is_numeric(get_post(1))) : ?><?php \$posts->put('p', is_a(get_post(1), 'WP_Post') ? (get_post(1))->ID : get_post(1)); ?><?php endif; ?><?php if (is_array(get_post(1))) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect(get_post(1))->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : get_post(1); ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
});

it('compiles correctly with post ID array', function () {
$directive = '@posts([1, 2, 3])';

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, 2, 3], 'WP_Post') || is_numeric([1, 2, 3])) : ?><?php \$posts->put('p', is_a([1, 2, 3], 'WP_Post') ? ([1, 2, 3])->ID : [1, 2, 3]); ?><?php endif; ?><?php if (is_array([1, 2, 3])) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect([1, 2, 3]) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, 2, 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, 2, 3], 'WP_Post') || is_numeric([1, 2, 3])) : ?><?php \$posts->put('p', is_a([1, 2, 3], 'WP_Post') ? ([1, 2, 3])->ID : [1, 2, 3]); ?><?php endif; ?><?php if (is_array([1, 2, 3])) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect([1, 2, 3])->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, 2, 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
});

it('compiles correctly with post ID array containing object', function () {
$directive = '@posts([1, get_post(2), 3])';

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, get_post(2), 3], 'WP_Post') || is_numeric([1, get_post(2), 3])) : ?><?php \$posts->put('p', is_a([1, get_post(2), 3], 'WP_Post') ? ([1, get_post(2), 3])->ID : [1, get_post(2), 3]); ?><?php endif; ?><?php if (is_array([1, get_post(2), 3])) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect([1, get_post(2), 3]) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, get_post(2), 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a([1, get_post(2), 3], 'WP_Post') || is_numeric([1, get_post(2), 3])) : ?><?php \$posts->put('p', is_a([1, get_post(2), 3], 'WP_Post') ? ([1, get_post(2), 3])->ID : [1, get_post(2), 3]); ?><?php endif; ?><?php if (is_array([1, get_post(2), 3])) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect([1, get_post(2), 3])->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : [1, get_post(2), 3]; ?><?php if (\$query->have_posts()) : \$__currentLoopData = range(1, \$query->post_count); \$__env->addLoop(\$__currentLoopData); while (\$query->have_posts()) : \$__env->incrementLoopIndices(); \$loop = \$__env->getLastLoop(); \$query->the_post(); ?>");
});
});

Expand Down Expand Up @@ -76,7 +76,7 @@

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(['post_type' => 'post'], 'WP_Post') || is_numeric(['post_type' => 'post'])) : ?><?php \$posts->put('p', is_a(['post_type' => 'post'], 'WP_Post') ? (['post_type' => 'post'])->ID : ['post_type' => 'post']); ?><?php endif; ?><?php if (is_array(['post_type' => 'post'])) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(['post_type' => 'post']) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : ['post_type' => 'post']; ?><?php if (\$query->have_posts()) : ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(['post_type' => 'post'], 'WP_Post') || is_numeric(['post_type' => 'post'])) : ?><?php \$posts->put('p', is_a(['post_type' => 'post'], 'WP_Post') ? (['post_type' => 'post'])->ID : ['post_type' => 'post']); ?><?php endif; ?><?php if (is_array(['post_type' => 'post'])) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect(['post_type' => 'post'])->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : ['post_type' => 'post']; ?><?php if (\$query->have_posts()) : ?>");
});
});

Expand Down Expand Up @@ -104,7 +104,7 @@

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(['post_type' => 'post'], 'WP_Post') || is_numeric(['post_type' => 'post'])) : ?><?php \$posts->put('p', is_a(['post_type' => 'post'], 'WP_Post') ? (['post_type' => 'post'])->ID : ['post_type' => 'post']); ?><?php endif; ?><?php if (is_array(['post_type' => 'post'])) : ?><?php \$posts ->put('ignore_sticky_posts', true) ->put('posts_per_page', -1) ->put('post__in', collect(['post_type' => 'post']) ->map(function (\$post) { return is_a(\$post, 'WP_Post') ? \$post->ID : \$post; })->all()) ->put('orderby', 'post__in'); ?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : ['post_type' => 'post']; ?><?php if (! \$query->have_posts()) : ?>");
expect($compiled)->toBe("<?php \$posts = collect(); ?><?php if (is_a(['post_type' => 'post'], 'WP_Post') || is_numeric(['post_type' => 'post'])) : ?><?php \$posts->put('p', is_a(['post_type' => 'post'], 'WP_Post') ? (['post_type' => 'post'])->ID : ['post_type' => 'post']); ?><?php endif; ?><?php if (is_array(['post_type' => 'post'])) : ?><?php \$posts->put('ignore_sticky_posts', true)->put('posts_per_page', -1)->put('post__in', collect(['post_type' => 'post'])->map(fn (\$post) => is_a(\$post, 'WP_Post') ? \$post->ID : \$post)->all())->put('orderby', 'post__in');?><?php endif; ?><?php \$query = \$posts->isNotEmpty() ? new WP_Query(\$posts->all()) : ['post_type' => 'post']; ?><?php if (! \$query->have_posts()) : ?>");
});
});

Expand Down Expand Up @@ -536,71 +536,71 @@

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image(1, 'thumbnail', false); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 1; ?><?php echo wp_get_attachment_image(\$__imageDirective, 'thumbnail', false); ?>");
});

it('compiles correctly with size', function () {
$directive = "@image(1, 'medium')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image(1, 'medium', false); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 1; ?><?php echo wp_get_attachment_image(\$__imageDirective, 'medium', false); ?>");
});

it('compiles correctly with size and alt tag', function () {
$directive = "@image(1, 'medium', 'Alt Text')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image(1,'medium',false,['alt' => 'Alt Text']); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 1; ?><?php echo wp_get_attachment_image(\$__imageDirective,'medium',false,['alt' => 'Alt Text']); ?>");
});

it('compiles correctly with size and options array', function () {
$directive = "@image(1, 'medium', ['alt' => 'Alt Text', 'class' => 'class'])";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image(1,'medium',false,['alt' => 'Alt Text','class' => 'class']); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 1; ?><?php echo wp_get_attachment_image(\$__imageDirective,'medium',false,['alt' => 'Alt Text','class' => 'class']); ?>");
});

it('compiles correctly as a raw URL', function () {
$directive = "@image(1, 'raw')";

$compiled = $this->compile($directive);

expect($compiled)->toBe('<?php echo wp_get_attachment_url(1); ?>');
expect($compiled)->toBe('<?php $__imageDirective = 1; ?><?php echo wp_get_attachment_url($__imageDirective); ?>');
});

it('compiles correctly using a field name', function () {
$directive = "@image('image')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image('image', 'thumbnail', false); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 'image'; ?><?php \$__imageDirective = function_exists('acf') ? (get_field(\$__imageDirective) ?? get_sub_field(\$__imageDirective) ?? get_field(\$__imageDirective, 'option') ?? \$__imageDirective): \$__imageDirective; ?><?php \$__imageDirective = is_array(\$__imageDirective) && ! empty(\$__imageDirective['id']) ? \$__imageDirective['id'] : \$__imageDirective; ?><?php echo wp_get_attachment_image(\$__imageDirective, 'thumbnail', false); ?>");
});

it('compiles correctly using a field name and size', function () {
$directive = "@image('image', 'medium')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image('image', 'medium', false); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 'image'; ?><?php \$__imageDirective = function_exists('acf') ? (get_field(\$__imageDirective) ?? get_sub_field(\$__imageDirective) ?? get_field(\$__imageDirective, 'option') ?? \$__imageDirective): \$__imageDirective; ?><?php \$__imageDirective = is_array(\$__imageDirective) && ! empty(\$__imageDirective['id']) ? \$__imageDirective['id'] : \$__imageDirective; ?><?php echo wp_get_attachment_image(\$__imageDirective, 'medium', false); ?>");
});

it('compiles correctly using a field name, size and alt tag', function () {
$directive = "@image('image', 'medium', 'Alt Text')";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image('image','medium',false,['alt' => 'Alt Text']); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 'image'; ?><?php \$__imageDirective = function_exists('acf') ? (get_field(\$__imageDirective) ?? get_sub_field(\$__imageDirective) ?? get_field(\$__imageDirective, 'option') ?? \$__imageDirective): \$__imageDirective; ?><?php \$__imageDirective = is_array(\$__imageDirective) && ! empty(\$__imageDirective['id']) ? \$__imageDirective['id'] : \$__imageDirective; ?><?php echo wp_get_attachment_image(\$__imageDirective,'medium',false,['alt' => 'Alt Text']); ?>");
});

it('compiles correctly using a field name, size and options array', function () {
$directive = "@image('image', 'medium', ['alt' => 'Alt Text', 'class' => 'class'])";

$compiled = $this->compile($directive);

expect($compiled)->toBe("<?php echo wp_get_attachment_image('image','medium',false,['alt' => 'Alt Text','class' => 'class']); ?>");
expect($compiled)->toBe("<?php \$__imageDirective = 'image'; ?><?php \$__imageDirective = function_exists('acf') ? (get_field(\$__imageDirective) ?? get_sub_field(\$__imageDirective) ?? get_field(\$__imageDirective, 'option') ?? \$__imageDirective): \$__imageDirective; ?><?php \$__imageDirective = is_array(\$__imageDirective) && ! empty(\$__imageDirective['id']) ? \$__imageDirective['id'] : \$__imageDirective; ?><?php echo wp_get_attachment_image(\$__imageDirective,'medium',false,['alt' => 'Alt Text','class' => 'class']); ?>");
});
});

Expand Down

0 comments on commit a7fb72c

Please sign in to comment.